Skip to content

Commit c11183c

Browse files
bpo-36097: Use only public C-API in the_xxsubinterpreters module (adding as necessary). (gh-12359)
1 parent 842a2f0 commit c11183c

12 files changed

+483
-393
lines changed

Include/cpython/interpreteridobject.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef Py_CPYTHON_INTERPRETERIDOBJECT_H
2+
# error "this header file must not be included directly"
3+
#endif
4+
5+
#ifdef __cplusplus
6+
extern "C" {
7+
#endif
8+
9+
/* Interpreter ID Object */
10+
11+
PyAPI_DATA(PyTypeObject) _PyInterpreterID_Type;
12+
13+
PyAPI_FUNC(PyObject *) _PyInterpreterID_New(int64_t);
14+
PyAPI_FUNC(PyObject *) _PyInterpreterState_GetIDObject(PyInterpreterState *);
15+
PyAPI_FUNC(PyInterpreterState *) _PyInterpreterID_LookUp(PyObject *);
16+
17+
PyAPI_FUNC(int64_t) _Py_CoerceID(PyObject *);
18+
19+
#ifdef __cplusplus
20+
}
21+
#endif

Include/cpython/pystate.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ typedef struct {
3030
(_PyMainInterpreterConfig){.install_signal_handlers = -1}
3131
/* Note: _PyMainInterpreterConfig_INIT sets other fields to 0/NULL */
3232

33+
PyAPI_FUNC(int) _PyInterpreterState_RequiresIDRef(PyInterpreterState *);
34+
PyAPI_FUNC(void) _PyInterpreterState_RequireIDRef(PyInterpreterState *, int);
35+
3336
PyAPI_FUNC(_PyCoreConfig *) _PyInterpreterState_GetCoreConfig(PyInterpreterState *);
3437
PyAPI_FUNC(_PyMainInterpreterConfig *) _PyInterpreterState_GetMainConfig(PyInterpreterState *);
3538

39+
PyAPI_FUNC(PyObject *) _PyInterpreterState_GetMainModule(PyInterpreterState *);
3640

3741
/* State unique per thread */
3842

@@ -214,6 +218,65 @@ PyAPI_FUNC(PyThreadState *) PyThreadState_Next(PyThreadState *);
214218

215219
typedef struct _frame *(*PyThreadFrameGetter)(PyThreadState *self_);
216220

221+
/* cross-interpreter data */
222+
223+
struct _xid;
224+
225+
// _PyCrossInterpreterData is similar to Py_buffer as an effectively
226+
// opaque struct that holds data outside the object machinery. This
227+
// is necessary to pass safely between interpreters in the same process.
228+
typedef struct _xid {
229+
// data is the cross-interpreter-safe derivation of a Python object
230+
// (see _PyObject_GetCrossInterpreterData). It will be NULL if the
231+
// new_object func (below) encodes the data.
232+
void *data;
233+
// obj is the Python object from which the data was derived. This
234+
// is non-NULL only if the data remains bound to the object in some
235+
// way, such that the object must be "released" (via a decref) when
236+
// the data is released. In that case the code that sets the field,
237+
// likely a registered "crossinterpdatafunc", is responsible for
238+
// ensuring it owns the reference (i.e. incref).
239+
PyObject *obj;
240+
// interp is the ID of the owning interpreter of the original
241+
// object. It corresponds to the active interpreter when
242+
// _PyObject_GetCrossInterpreterData() was called. This should only
243+
// be set by the cross-interpreter machinery.
244+
//
245+
// We use the ID rather than the PyInterpreterState to avoid issues
246+
// with deleted interpreters. Note that IDs are never re-used, so
247+
// each one will always correspond to a specific interpreter
248+
// (whether still alive or not).
249+
int64_t interp;
250+
// new_object is a function that returns a new object in the current
251+
// interpreter given the data. The resulting object (a new
252+
// reference) will be equivalent to the original object. This field
253+
// is required.
254+
PyObject *(*new_object)(struct _xid *);
255+
// free is called when the data is released. If it is NULL then
256+
// nothing will be done to free the data. For some types this is
257+
// okay (e.g. bytes) and for those types this field should be set
258+
// to NULL. However, for most the data was allocated just for
259+
// cross-interpreter use, so it must be freed when
260+
// _PyCrossInterpreterData_Release is called or the memory will
261+
// leak. In that case, at the very least this field should be set
262+
// to PyMem_RawFree (the default if not explicitly set to NULL).
263+
// The call will happen with the original interpreter activated.
264+
void (*free)(void *);
265+
} _PyCrossInterpreterData;
266+
267+
PyAPI_FUNC(int) _PyObject_GetCrossInterpreterData(PyObject *, _PyCrossInterpreterData *);
268+
PyAPI_FUNC(PyObject *) _PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *);
269+
PyAPI_FUNC(void) _PyCrossInterpreterData_Release(_PyCrossInterpreterData *);
270+
271+
PyAPI_FUNC(int) _PyObject_CheckCrossInterpreterData(PyObject *);
272+
273+
/* cross-interpreter data registry */
274+
275+
typedef int (*crossinterpdatafunc)(PyObject *, struct _xid *);
276+
277+
PyAPI_FUNC(int) _PyCrossInterpreterData_RegisterClass(PyTypeObject *, crossinterpdatafunc);
278+
PyAPI_FUNC(crossinterpdatafunc) _PyCrossInterpreterData_Lookup(PyObject *);
279+
217280
#ifdef __cplusplus
218281
}
219282
#endif

Include/internal/pycore_pystate.h

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ struct _is {
2929

3030
int64_t id;
3131
int64_t id_refcount;
32+
int requires_idref;
3233
PyThread_type_lock id_mutex;
3334

3435
int finalizing;
@@ -89,66 +90,12 @@ PyAPI_FUNC(void) _PyInterpreterState_IDIncref(struct _is *);
8990
PyAPI_FUNC(void) _PyInterpreterState_IDDecref(struct _is *);
9091

9192

92-
/* cross-interpreter data */
93-
94-
struct _xid;
95-
96-
// _PyCrossInterpreterData is similar to Py_buffer as an effectively
97-
// opaque struct that holds data outside the object machinery. This
98-
// is necessary to pass safely between interpreters in the same process.
99-
typedef struct _xid {
100-
// data is the cross-interpreter-safe derivation of a Python object
101-
// (see _PyObject_GetCrossInterpreterData). It will be NULL if the
102-
// new_object func (below) encodes the data.
103-
void *data;
104-
// obj is the Python object from which the data was derived. This
105-
// is non-NULL only if the data remains bound to the object in some
106-
// way, such that the object must be "released" (via a decref) when
107-
// the data is released. In that case the code that sets the field,
108-
// likely a registered "crossinterpdatafunc", is responsible for
109-
// ensuring it owns the reference (i.e. incref).
110-
PyObject *obj;
111-
// interp is the ID of the owning interpreter of the original
112-
// object. It corresponds to the active interpreter when
113-
// _PyObject_GetCrossInterpreterData() was called. This should only
114-
// be set by the cross-interpreter machinery.
115-
//
116-
// We use the ID rather than the PyInterpreterState to avoid issues
117-
// with deleted interpreters.
118-
int64_t interp;
119-
// new_object is a function that returns a new object in the current
120-
// interpreter given the data. The resulting object (a new
121-
// reference) will be equivalent to the original object. This field
122-
// is required.
123-
PyObject *(*new_object)(struct _xid *);
124-
// free is called when the data is released. If it is NULL then
125-
// nothing will be done to free the data. For some types this is
126-
// okay (e.g. bytes) and for those types this field should be set
127-
// to NULL. However, for most the data was allocated just for
128-
// cross-interpreter use, so it must be freed when
129-
// _PyCrossInterpreterData_Release is called or the memory will
130-
// leak. In that case, at the very least this field should be set
131-
// to PyMem_RawFree (the default if not explicitly set to NULL).
132-
// The call will happen with the original interpreter activated.
133-
void (*free)(void *);
134-
} _PyCrossInterpreterData;
135-
136-
typedef int (*crossinterpdatafunc)(PyObject *, _PyCrossInterpreterData *);
137-
PyAPI_FUNC(int) _PyObject_CheckCrossInterpreterData(PyObject *);
138-
139-
PyAPI_FUNC(int) _PyObject_GetCrossInterpreterData(PyObject *, _PyCrossInterpreterData *);
140-
PyAPI_FUNC(PyObject *) _PyCrossInterpreterData_NewObject(_PyCrossInterpreterData *);
141-
PyAPI_FUNC(void) _PyCrossInterpreterData_Release(_PyCrossInterpreterData *);
142-
14393
/* cross-interpreter data registry */
14494

14595
/* For now we use a global registry of shareable classes. An
14696
alternative would be to add a tp_* slot for a class's
14797
crossinterpdatafunc. It would be simpler and more efficient. */
14898

149-
PyAPI_FUNC(int) _PyCrossInterpreterData_Register_Class(PyTypeObject *, crossinterpdatafunc);
150-
PyAPI_FUNC(crossinterpdatafunc) _PyCrossInterpreterData_Lookup(PyObject *);
151-
15299
struct _xidregitem;
153100

154101
struct _xidregitem {

Include/interpreteridobject.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef Py_INTERPRETERIDOBJECT_H
2+
#define Py_INTERPRETERIDOBJECT_H
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
#ifndef Py_LIMITED_API
9+
# define Py_CPYTHON_INTERPRETERIDOBJECT_H
10+
# include "cpython/interpreteridobject.h"
11+
# undef Py_CPYTHON_INTERPRETERIDOBJECT_H
12+
#endif
13+
14+
#ifdef __cplusplus
15+
}
16+
#endif
17+
#endif /* !Py_INTERPRETERIDOBJECT_H */

Makefile.pre.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ OBJECT_OBJS= \
391391
Objects/floatobject.o \
392392
Objects/frameobject.o \
393393
Objects/funcobject.o \
394+
Objects/interpreteridobject.o \
394395
Objects/iterobject.o \
395396
Objects/listobject.o \
396397
Objects/longobject.o \
@@ -977,6 +978,7 @@ PYTHON_HEADERS= \
977978
$(srcdir)/Include/funcobject.h \
978979
$(srcdir)/Include/genobject.h \
979980
$(srcdir)/Include/import.h \
981+
$(srcdir)/Include/interpreteridobject.h \
980982
$(srcdir)/Include/intrcheck.h \
981983
$(srcdir)/Include/iterobject.h \
982984
$(srcdir)/Include/listobject.h \
@@ -1039,6 +1041,7 @@ PYTHON_HEADERS= \
10391041
$(srcdir)/Include/cpython/abstract.h \
10401042
$(srcdir)/Include/cpython/coreconfig.h \
10411043
$(srcdir)/Include/cpython/dictobject.h \
1044+
$(srcdir)/Include/cpython/interpreteridobject.h \
10421045
$(srcdir)/Include/cpython/object.h \
10431046
$(srcdir)/Include/cpython/objimpl.h \
10441047
$(srcdir)/Include/cpython/pyerrors.h \

0 commit comments

Comments
 (0)