Skip to content

gh-101524: Only Use Public C-API in the _xxsubinterpreters Module #107359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Include/cpython/interpreteridobject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef Py_CPYTHON_INTERPRETERIDOBJECT_H
# error "this header file must not be included directly"
#endif

/* Interpreter ID Object */

PyAPI_DATA(PyTypeObject) PyInterpreterID_Type;

PyAPI_FUNC(PyObject *) PyInterpreterID_New(int64_t);
PyAPI_FUNC(PyObject *) PyInterpreterState_GetIDObject(PyInterpreterState *);
PyAPI_FUNC(PyInterpreterState *) PyInterpreterID_LookUp(PyObject *);
4 changes: 4 additions & 0 deletions Include/cpython/pylifecycle.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ typedef struct {
PyAPI_FUNC(PyStatus) Py_NewInterpreterFromConfig(
PyThreadState **tstate_p,
const PyInterpreterConfig *config);

typedef void (*atexit_datacallbackfunc)(void *);
PyAPI_FUNC(int) PyUnstable_AtExit(
PyInterpreterState *, atexit_datacallbackfunc, void *);
1 change: 1 addition & 0 deletions Include/cpython/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
PyAPI_FUNC(int) _PyInterpreterState_RequiresIDRef(PyInterpreterState *);
PyAPI_FUNC(void) _PyInterpreterState_RequireIDRef(PyInterpreterState *, int);

PyAPI_FUNC(PyObject *) PyUnstable_InterpreterState_GetMainModule(PyInterpreterState *);

/* State unique per thread */

Expand Down
3 changes: 0 additions & 3 deletions Include/internal/pycore_interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,6 @@ extern int _PyInterpreterState_IDInitref(PyInterpreterState *);
extern int _PyInterpreterState_IDIncref(PyInterpreterState *);
extern void _PyInterpreterState_IDDecref(PyInterpreterState *);

// Export for '_xxsubinterpreters' shared extension
PyAPI_FUNC(PyObject*) _PyInterpreterState_GetMainModule(PyInterpreterState *);

extern const PyConfig* _PyInterpreterState_GetConfig(PyInterpreterState *interp);

/* Get a copy of the current interpreter configuration.
Expand Down
28 changes: 0 additions & 28 deletions Include/internal/pycore_interp_id.h

This file was deleted.

17 changes: 17 additions & 0 deletions Include/interpreteridobject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef Py_INTERPRETERIDOBJECT_H
#define Py_INTERPRETERIDOBJECT_H

#ifdef __cplusplus
extern "C" {
#endif

#ifndef Py_LIMITED_API
# define Py_CPYTHON_INTERPRETERIDOBJECT_H
# include "cpython/interpreteridobject.h"
# undef Py_CPYTHON_INTERPRETERIDOBJECT_H
#endif

#ifdef __cplusplus
}
#endif
#endif /* !Py_INTERPRETERIDOBJECT_H */
3 changes: 2 additions & 1 deletion Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -1631,6 +1631,7 @@ PYTHON_HEADERS= \
$(srcdir)/Include/floatobject.h \
$(srcdir)/Include/frameobject.h \
$(srcdir)/Include/import.h \
$(srcdir)/Include/interpreteridobject.h \
$(srcdir)/Include/intrcheck.h \
$(srcdir)/Include/iterobject.h \
$(srcdir)/Include/listobject.h \
Expand Down Expand Up @@ -1700,6 +1701,7 @@ PYTHON_HEADERS= \
$(srcdir)/Include/cpython/genobject.h \
$(srcdir)/Include/cpython/import.h \
$(srcdir)/Include/cpython/initconfig.h \
$(srcdir)/Include/cpython/interpreteridobject.h \
$(srcdir)/Include/cpython/listobject.h \
$(srcdir)/Include/cpython/longintrepr.h \
$(srcdir)/Include/cpython/longobject.h \
Expand Down Expand Up @@ -1771,7 +1773,6 @@ PYTHON_HEADERS= \
$(srcdir)/Include/internal/pycore_import.h \
$(srcdir)/Include/internal/pycore_initconfig.h \
$(srcdir)/Include/internal/pycore_interp.h \
$(srcdir)/Include/internal/pycore_interp_id.h \
$(srcdir)/Include/internal/pycore_intrinsics.h \
$(srcdir)/Include/internal/pycore_list.h \
$(srcdir)/Include/internal/pycore_long.h \
Expand Down
12 changes: 6 additions & 6 deletions Modules/_testinternalcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,27 @@
#include "pycore_atomic_funcs.h" // _Py_atomic_int_get()
#include "pycore_bitutils.h" // _Py_bswap32()
#include "pycore_bytesobject.h" // _PyBytes_Find()
#include "pycore_ceval.h" // _PyEval_AddPendingCall
#include "pycore_compile.h" // _PyCompile_CodeGen, _PyCompile_OptimizeCfg, _PyCompile_Assemble, _PyCompile_CleanDoc
#include "pycore_ceval.h" // _PyEval_AddPendingCall
#include "pycore_fileutils.h" // _Py_normpath
#include "pycore_frame.h" // _PyInterpreterFrame
#include "pycore_gc.h" // PyGC_Head
#include "pycore_hashtable.h" // _Py_hashtable_new()
#include "pycore_initconfig.h" // _Py_GetConfigsAsDict()
#include "pycore_interp.h" // _PyInterpreterState_GetConfigCopy()
#include "pycore_interp_id.h" // _PyInterpreterID_LookUp()
#include "pycore_object.h" // _PyObject_IsFreed()
#include "pycore_object.h" // _PyObject_IsFreed()
#include "pycore_pathconfig.h" // _PyPathConfig_ClearGlobal()
#include "pycore_pyerrors.h" // _Py_UTF8_Edit_Cost()
#include "pycore_pystate.h" // _PyThreadState_GET()

#include "frameobject.h"
#include "interpreteridobject.h" // PyInterpreterID_LookUp()
#include "osdefs.h" // MAXPATHLEN

#include "clinic/_testinternalcapi.c.h"

#ifdef MS_WINDOWS
# include <winsock2.h> // struct timeval
# include <winsock2.h> // struct timeval
#endif


Expand Down Expand Up @@ -1083,7 +1083,7 @@ pending_identify(PyObject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "O:pending_identify", &interpid)) {
return NULL;
}
PyInterpreterState *interp = _PyInterpreterID_LookUp(interpid);
PyInterpreterState *interp = PyInterpreterID_LookUp(interpid);
if (interp == NULL) {
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_ValueError, "interpreter not found");
Expand Down Expand Up @@ -1433,7 +1433,7 @@ test_atexit(PyObject *self, PyObject *Py_UNUSED(args))
PyThreadState *tstate = Py_NewInterpreter();

struct atexit_data data = {0};
int res = _Py_AtExit(tstate->interp, callback, (void *)&data);
int res = PyUnstable_AtExit(tstate->interp, callback, (void *)&data);
Py_EndInterpreter(tstate);
PyThreadState_Swap(oldts);
if (res < 0) {
Expand Down
11 changes: 3 additions & 8 deletions Modules/_xxinterpchannelsmodule.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
/* interpreters module */
/* low-level access to interpreter primitives */

#ifndef Py_BUILD_CORE_BUILTIN
# define Py_BUILD_CORE_MODULE 1
#endif

#include "Python.h"
#include "pycore_atexit.h" // _Py_AtExit()
#include "pycore_interp_id.h" // _PyInterpreterState_GetIDObject()
#include "interpreteridobject.h"


/*
Expand Down Expand Up @@ -2140,7 +2135,7 @@ channel_list_interpreters(PyObject *self, PyObject *args, PyObject *kwds)
goto except;
}
if (res) {
id_obj = _PyInterpreterState_GetIDObject(interp);
id_obj = PyInterpreterState_GetIDObject(interp);
if (id_obj == NULL) {
goto except;
}
Expand Down Expand Up @@ -2407,7 +2402,7 @@ module_exec(PyObject *mod)

// Make sure chnnels drop objects owned by this interpreter
PyInterpreterState *interp = _get_current_interp();
_Py_AtExit(interp, clear_interpreter, (void *)interp);
PyUnstable_AtExit(interp, clear_interpreter, (void *)interp);

return 0;

Expand Down
25 changes: 10 additions & 15 deletions Modules/_xxsubinterpretersmodule.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
/* interpreters module */
/* low-level access to interpreter primitives */

#ifndef Py_BUILD_CORE_BUILTIN
# define Py_BUILD_CORE_MODULE 1
#endif

#include "Python.h"
#include "pycore_interp.h" // _PyInterpreterState_GetMainModule()
#include "pycore_interp_id.h" // _PyInterpreterState_GetIDObject()
#include "interpreteridobject.h"


#define MODULE_NAME "_xxsubinterpreters"
Expand Down Expand Up @@ -400,7 +395,7 @@ _run_script(PyInterpreterState *interp, const char *codestr,
_sharedns *shared, _sharedexception *sharedexc)
{
PyObject *excval = NULL;
PyObject *main_mod = _PyInterpreterState_GetMainModule(interp);
PyObject *main_mod = PyUnstable_InterpreterState_GetMainModule(interp);
if (main_mod == NULL) {
goto error;
}
Expand Down Expand Up @@ -531,7 +526,7 @@ interp_create(PyObject *self, PyObject *args, PyObject *kwds)
}
assert(tstate != NULL);
PyInterpreterState *interp = PyThreadState_GetInterpreter(tstate);
PyObject *idobj = _PyInterpreterState_GetIDObject(interp);
PyObject *idobj = PyInterpreterState_GetIDObject(interp);
if (idobj == NULL) {
// XXX Possible GILState issues?
save_tstate = PyThreadState_Swap(tstate);
Expand Down Expand Up @@ -561,7 +556,7 @@ interp_destroy(PyObject *self, PyObject *args, PyObject *kwds)
}

// Look up the interpreter.
PyInterpreterState *interp = _PyInterpreterID_LookUp(id);
PyInterpreterState *interp = PyInterpreterID_LookUp(id);
if (interp == NULL) {
return NULL;
}
Expand Down Expand Up @@ -616,7 +611,7 @@ interp_list_all(PyObject *self, PyObject *Py_UNUSED(ignored))

interp = PyInterpreterState_Head();
while (interp != NULL) {
id = _PyInterpreterState_GetIDObject(interp);
id = PyInterpreterState_GetIDObject(interp);
if (id == NULL) {
Py_DECREF(ids);
return NULL;
Expand Down Expand Up @@ -648,7 +643,7 @@ interp_get_current(PyObject *self, PyObject *Py_UNUSED(ignored))
if (interp == NULL) {
return NULL;
}
return _PyInterpreterState_GetIDObject(interp);
return PyInterpreterState_GetIDObject(interp);
}

PyDoc_STRVAR(get_current_doc,
Expand All @@ -662,7 +657,7 @@ interp_get_main(PyObject *self, PyObject *Py_UNUSED(ignored))
{
// Currently, 0 is always the main interpreter.
int64_t id = 0;
return _PyInterpreterID_New(id);
return PyInterpreterID_New(id);
}

PyDoc_STRVAR(get_main_doc,
Expand All @@ -684,7 +679,7 @@ interp_run_string(PyObject *self, PyObject *args, PyObject *kwds)
}

// Look up the interpreter.
PyInterpreterState *interp = _PyInterpreterID_LookUp(id);
PyInterpreterState *interp = PyInterpreterID_LookUp(id);
if (interp == NULL) {
return NULL;
}
Expand Down Expand Up @@ -750,7 +745,7 @@ interp_is_running(PyObject *self, PyObject *args, PyObject *kwds)
return NULL;
}

PyInterpreterState *interp = _PyInterpreterID_LookUp(id);
PyInterpreterState *interp = PyInterpreterID_LookUp(id);
if (interp == NULL) {
return NULL;
}
Expand Down Expand Up @@ -808,7 +803,7 @@ module_exec(PyObject *mod)
}

// PyInterpreterID
if (PyModule_AddType(mod, &_PyInterpreterID_Type) < 0) {
if (PyModule_AddType(mod, &PyInterpreterID_Type) < 0) {
goto error;
}

Expand Down
4 changes: 2 additions & 2 deletions Modules/atexitmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ get_atexit_state(void)


int
_Py_AtExit(PyInterpreterState *interp,
atexit_datacallbackfunc func, void *data)
PyUnstable_AtExit(PyInterpreterState *interp,
atexit_datacallbackfunc func, void *data)
{
assert(interp == _PyInterpreterState_GET());
atexit_callback *callback = PyMem_Malloc(sizeof(atexit_callback));
Expand Down
20 changes: 10 additions & 10 deletions Objects/interpreteridobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "Python.h"
#include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_interp.h" // _PyInterpreterState_LookUpID()
#include "pycore_interp_id.h" // _PyInterpreterID_Type
#include "interpreteridobject.h"


typedef struct interpid {
Expand Down Expand Up @@ -46,7 +46,7 @@ static int
interp_id_converter(PyObject *arg, void *ptr)
{
int64_t id;
if (PyObject_TypeCheck(arg, &_PyInterpreterID_Type)) {
if (PyObject_TypeCheck(arg, &PyInterpreterID_Type)) {
id = ((interpid *)arg)->id;
}
else if (_PyIndex_Check(arg)) {
Expand Down Expand Up @@ -183,13 +183,13 @@ interpid_richcompare(PyObject *self, PyObject *other, int op)
Py_RETURN_NOTIMPLEMENTED;
}

if (!PyObject_TypeCheck(self, &_PyInterpreterID_Type)) {
if (!PyObject_TypeCheck(self, &PyInterpreterID_Type)) {
Py_RETURN_NOTIMPLEMENTED;
}

interpid *id = (interpid *)self;
int equal;
if (PyObject_TypeCheck(other, &_PyInterpreterID_Type)) {
if (PyObject_TypeCheck(other, &PyInterpreterID_Type)) {
interpid *otherid = (interpid *)other;
equal = (id->id == otherid->id);
}
Expand Down Expand Up @@ -224,7 +224,7 @@ interpid_richcompare(PyObject *self, PyObject *other, int op)
PyDoc_STRVAR(interpid_doc,
"A interpreter ID identifies a interpreter and may be used as an int.");

PyTypeObject _PyInterpreterID_Type = {
PyTypeObject PyInterpreterID_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"InterpreterID", /* tp_name */
sizeof(interpid), /* tp_basicsize */
Expand Down Expand Up @@ -265,13 +265,13 @@ PyTypeObject _PyInterpreterID_Type = {
interpid_new, /* tp_new */
};

PyObject *_PyInterpreterID_New(int64_t id)
PyObject *PyInterpreterID_New(int64_t id)
{
return (PyObject *)newinterpid(&_PyInterpreterID_Type, id, 0);
return (PyObject *)newinterpid(&PyInterpreterID_Type, id, 0);
}

PyObject *
_PyInterpreterState_GetIDObject(PyInterpreterState *interp)
PyInterpreterState_GetIDObject(PyInterpreterState *interp)
{
if (_PyInterpreterState_IDInitref(interp) != 0) {
return NULL;
Expand All @@ -280,11 +280,11 @@ _PyInterpreterState_GetIDObject(PyInterpreterState *interp)
if (id < 0) {
return NULL;
}
return (PyObject *)newinterpid(&_PyInterpreterID_Type, id, 0);
return (PyObject *)newinterpid(&PyInterpreterID_Type, id, 0);
}

PyInterpreterState *
_PyInterpreterID_LookUp(PyObject *requested_id)
PyInterpreterID_LookUp(PyObject *requested_id)
{
int64_t id;
if (!interp_id_converter(requested_id, &id)) {
Expand Down
Loading