gh-101524: Only Use Public C-API in the _xxsubinterpreters Module (gh-105258)

The _xxsubinterpreters module was meant to only use public API.  Some internal C-API usage snuck in over the last few years (e.g. gh-28969).  This fixes that.
This commit is contained in:
Eric Snow 2023-06-02 16:52:33 -06:00 committed by GitHub
parent 9ad199ba36
commit e6373c0d8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 39 additions and 37 deletions

View File

@ -1,22 +1,11 @@
#ifndef Py_CPYTHON_INTERPRETERIDOBJECT_H
# error "this header file must not be included directly"
#endif
/* Interpreter ID Object */ /* Interpreter ID Object */
#ifndef Py_INTERNAL_INTERPRETERIDOBJECT_H
#define Py_INTERNAL_INTERPRETERIDOBJECT_H
#ifdef __cplusplus
extern "C" {
#endif
#ifndef Py_BUILD_CORE
# error "this header requires Py_BUILD_CORE define"
#endif
PyAPI_DATA(PyTypeObject) _PyInterpreterID_Type; PyAPI_DATA(PyTypeObject) _PyInterpreterID_Type;
PyAPI_FUNC(PyObject *) _PyInterpreterID_New(int64_t); PyAPI_FUNC(PyObject *) _PyInterpreterID_New(int64_t);
PyAPI_FUNC(PyObject *) _PyInterpreterState_GetIDObject(PyInterpreterState *); PyAPI_FUNC(PyObject *) _PyInterpreterState_GetIDObject(PyInterpreterState *);
PyAPI_FUNC(PyInterpreterState *) _PyInterpreterID_LookUp(PyObject *); PyAPI_FUNC(PyInterpreterState *) _PyInterpreterID_LookUp(PyObject *);
#ifdef __cplusplus
}
#endif
#endif // !Py_INTERNAL_INTERPRETERIDOBJECT_H

View File

@ -94,4 +94,4 @@ PyAPI_FUNC(int) _PyMem_SetupAllocators(PyMemAllocatorName allocator);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
#endif // !Py_INTERNAL_PYMEM_H #endif /* !Py_INTERNAL_PYMEM_H */

View File

@ -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 */

View File

@ -1618,6 +1618,7 @@ PYTHON_HEADERS= \
$(srcdir)/Include/floatobject.h \ $(srcdir)/Include/floatobject.h \
$(srcdir)/Include/frameobject.h \ $(srcdir)/Include/frameobject.h \
$(srcdir)/Include/import.h \ $(srcdir)/Include/import.h \
$(srcdir)/Include/interpreteridobject.h \
$(srcdir)/Include/intrcheck.h \ $(srcdir)/Include/intrcheck.h \
$(srcdir)/Include/iterobject.h \ $(srcdir)/Include/iterobject.h \
$(srcdir)/Include/listobject.h \ $(srcdir)/Include/listobject.h \
@ -1688,6 +1689,7 @@ PYTHON_HEADERS= \
$(srcdir)/Include/cpython/genobject.h \ $(srcdir)/Include/cpython/genobject.h \
$(srcdir)/Include/cpython/import.h \ $(srcdir)/Include/cpython/import.h \
$(srcdir)/Include/cpython/initconfig.h \ $(srcdir)/Include/cpython/initconfig.h \
$(srcdir)/Include/cpython/interpreteridobject.h \
$(srcdir)/Include/cpython/listobject.h \ $(srcdir)/Include/cpython/listobject.h \
$(srcdir)/Include/cpython/longintrepr.h \ $(srcdir)/Include/cpython/longintrepr.h \
$(srcdir)/Include/cpython/longobject.h \ $(srcdir)/Include/cpython/longobject.h \
@ -1756,7 +1758,6 @@ PYTHON_HEADERS= \
$(srcdir)/Include/internal/pycore_import.h \ $(srcdir)/Include/internal/pycore_import.h \
$(srcdir)/Include/internal/pycore_initconfig.h \ $(srcdir)/Include/internal/pycore_initconfig.h \
$(srcdir)/Include/internal/pycore_interp.h \ $(srcdir)/Include/internal/pycore_interp.h \
$(srcdir)/Include/internal/pycore_interpreteridobject.h \
$(srcdir)/Include/internal/pycore_intrinsics.h \ $(srcdir)/Include/internal/pycore_intrinsics.h \
$(srcdir)/Include/internal/pycore_list.h \ $(srcdir)/Include/internal/pycore_list.h \
$(srcdir)/Include/internal/pycore_long.h \ $(srcdir)/Include/internal/pycore_long.h \

View File

@ -1,13 +1,9 @@
/* interpreters module */ /* interpreters module */
/* low-level access to interpreter primitives */ /* low-level access to interpreter primitives */
#ifndef Py_BUILD_CORE_BUILTIN
# define Py_BUILD_CORE_MODULE 1
#endif
#include "Python.h" #include "Python.h"
#include "pycore_pystate.h" // _PyThreadState_GET() #include "interpreteridobject.h"
#include "pycore_interpreteridobject.h"
/* /*

View File

@ -1,15 +1,9 @@
/* interpreters module */ /* interpreters module */
/* low-level access to interpreter primitives */ /* low-level access to interpreter primitives */
#ifndef Py_BUILD_CORE_BUILTIN
# define Py_BUILD_CORE_MODULE 1
#endif
#include "Python.h" #include "Python.h"
// XXX This module should not rely on internal API. #include "interpreteridobject.h"
#include "pycore_frame.h"
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_interpreteridobject.h"
#define MODULE_NAME "_xxsubinterpreters" #define MODULE_NAME "_xxsubinterpreters"
@ -376,7 +370,7 @@ _is_running(PyInterpreterState *interp)
} }
assert(!PyErr_Occurred()); assert(!PyErr_Occurred());
_PyInterpreterFrame *frame = tstate->cframe->current_frame; struct _PyInterpreterFrame *frame = tstate->cframe->current_frame;
if (frame == NULL) { if (frame == NULL) {
return 0; return 0;
} }
@ -512,7 +506,7 @@ interp_create(PyObject *self, PyObject *args, PyObject *kwds)
} }
// Create and initialize the new interpreter. // Create and initialize the new interpreter.
PyThreadState *save_tstate = _PyThreadState_GET(); PyThreadState *save_tstate = PyThreadState_Get();
assert(save_tstate != NULL); assert(save_tstate != NULL);
const PyInterpreterConfig config = isolated const PyInterpreterConfig config = isolated
? (PyInterpreterConfig)_PyInterpreterConfig_INIT ? (PyInterpreterConfig)_PyInterpreterConfig_INIT

View File

@ -3,7 +3,7 @@
#include "Python.h" #include "Python.h"
#include "pycore_abstract.h" // _PyIndex_Check() #include "pycore_abstract.h" // _PyIndex_Check()
#include "pycore_interp.h" // _PyInterpreterState_LookUpID() #include "pycore_interp.h" // _PyInterpreterState_LookUpID()
#include "pycore_interpreteridobject.h" #include "interpreteridobject.h"
typedef struct interpid { typedef struct interpid {

View File

@ -17,7 +17,7 @@
#include "pycore_typevarobject.h" // _PyTypeAlias_Type, _Py_initialize_generic #include "pycore_typevarobject.h" // _PyTypeAlias_Type, _Py_initialize_generic
#include "pycore_typeobject.h" // _PyBufferWrapper_Type #include "pycore_typeobject.h" // _PyBufferWrapper_Type
#include "pycore_unionobject.h" // _PyUnion_Type #include "pycore_unionobject.h" // _PyUnion_Type
#include "pycore_interpreteridobject.h" // _PyInterpreterID_Type #include "interpreteridobject.h" // _PyInterpreterID_Type
#ifdef Py_LIMITED_API #ifdef Py_LIMITED_API
// Prevent recursive call _Py_IncRef() <=> Py_INCREF() // Prevent recursive call _Py_IncRef() <=> Py_INCREF()

View File

@ -152,6 +152,7 @@
<ClInclude Include="..\Include\cpython\genobject.h" /> <ClInclude Include="..\Include\cpython\genobject.h" />
<ClInclude Include="..\Include\cpython\import.h" /> <ClInclude Include="..\Include\cpython\import.h" />
<ClInclude Include="..\Include\cpython\initconfig.h" /> <ClInclude Include="..\Include\cpython\initconfig.h" />
<ClInclude Include="..\Include\cpython\interpreteridobject.h" />
<ClInclude Include="..\Include\cpython\listobject.h" /> <ClInclude Include="..\Include\cpython\listobject.h" />
<ClInclude Include="..\Include\cpython\longintrepr.h" /> <ClInclude Include="..\Include\cpython\longintrepr.h" />
<ClInclude Include="..\Include\cpython\longobject.h" /> <ClInclude Include="..\Include\cpython\longobject.h" />
@ -234,7 +235,6 @@
<ClInclude Include="..\Include\internal\pycore_import.h" /> <ClInclude Include="..\Include\internal\pycore_import.h" />
<ClInclude Include="..\Include\internal\pycore_initconfig.h" /> <ClInclude Include="..\Include\internal\pycore_initconfig.h" />
<ClInclude Include="..\Include\internal\pycore_interp.h" /> <ClInclude Include="..\Include\internal\pycore_interp.h" />
<ClInclude Include="..\Include\internal\pycore_interpreteridobject.h" />
<ClInclude Include="..\Include\internal\pycore_intrinsics.h" /> <ClInclude Include="..\Include\internal\pycore_intrinsics.h" />
<ClInclude Include="..\Include\internal\pycore_list.h" /> <ClInclude Include="..\Include\internal\pycore_list.h" />
<ClInclude Include="..\Include\internal\pycore_long.h" /> <ClInclude Include="..\Include\internal\pycore_long.h" />
@ -275,6 +275,7 @@
<ClInclude Include="..\Include\internal\pycore_unicodeobject.h" /> <ClInclude Include="..\Include\internal\pycore_unicodeobject.h" />
<ClInclude Include="..\Include\internal\pycore_unicodeobject_generated.h" /> <ClInclude Include="..\Include\internal\pycore_unicodeobject_generated.h" />
<ClInclude Include="..\Include\internal\pycore_warnings.h" /> <ClInclude Include="..\Include\internal\pycore_warnings.h" />
<ClInclude Include="..\Include\interpreteridobject.h" />
<ClInclude Include="..\Include\intrcheck.h" /> <ClInclude Include="..\Include\intrcheck.h" />
<ClInclude Include="..\Include\iterobject.h" /> <ClInclude Include="..\Include\iterobject.h" />
<ClInclude Include="..\Include\listobject.h" /> <ClInclude Include="..\Include\listobject.h" />

View File

@ -315,6 +315,9 @@
<ClInclude Include="..\Include\pyhash.h"> <ClInclude Include="..\Include\pyhash.h">
<Filter>Include</Filter> <Filter>Include</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\Include\interpreteridobject.h">
<Filter>Include</Filter>
</ClInclude>
<ClInclude Include="..\Modules\hashtable.h"> <ClInclude Include="..\Modules\hashtable.h">
<Filter>Modules</Filter> <Filter>Modules</Filter>
</ClInclude> </ClInclude>
@ -459,6 +462,9 @@
<ClInclude Include="..\Include\cpython\genobject.h"> <ClInclude Include="..\Include\cpython\genobject.h">
<Filter>Include</Filter> <Filter>Include</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\Include\cpython\interpreteridobject.h">
<Filter>Include\cpython</Filter>
</ClInclude>
<ClInclude Include="..\Include\cpython\pythonrun.h"> <ClInclude Include="..\Include\cpython\pythonrun.h">
<Filter>Include\cpython</Filter> <Filter>Include\cpython</Filter>
</ClInclude> </ClInclude>
@ -603,9 +609,6 @@
<ClInclude Include="..\Include\internal\pycore_interp.h"> <ClInclude Include="..\Include\internal\pycore_interp.h">
<Filter>Include\internal</Filter> <Filter>Include\internal</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\Include\internal\pycore_interpreteridobject.h">
<Filter>Include\cpython</Filter>
</ClInclude>
<ClInclude Include="..\Include\internal\pycore_intrinsics.h"> <ClInclude Include="..\Include\internal\pycore_intrinsics.h">
<Filter>Include\cpython</Filter> <Filter>Include\cpython</Filter>
</ClInclude> </ClInclude>

View File

@ -226,6 +226,7 @@ Include/cpython/fileobject.h Py_CPYTHON_FILEOBJECT_H 1
Include/cpython/fileutils.h Py_CPYTHON_FILEUTILS_H 1 Include/cpython/fileutils.h Py_CPYTHON_FILEUTILS_H 1
Include/cpython/frameobject.h Py_CPYTHON_FRAMEOBJECT_H 1 Include/cpython/frameobject.h Py_CPYTHON_FRAMEOBJECT_H 1
Include/cpython/import.h Py_CPYTHON_IMPORT_H 1 Include/cpython/import.h Py_CPYTHON_IMPORT_H 1
Include/cpython/interpreteridobject.h Py_CPYTHON_INTERPRETERIDOBJECT_H 1
Include/cpython/listobject.h Py_CPYTHON_LISTOBJECT_H 1 Include/cpython/listobject.h Py_CPYTHON_LISTOBJECT_H 1
Include/cpython/methodobject.h Py_CPYTHON_METHODOBJECT_H 1 Include/cpython/methodobject.h Py_CPYTHON_METHODOBJECT_H 1
Include/cpython/object.h Py_CPYTHON_OBJECT_H 1 Include/cpython/object.h Py_CPYTHON_OBJECT_H 1