Skip to content

Commit e6373c0

Browse files
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.
1 parent 9ad199b commit e6373c0

11 files changed

+38
-36
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
1-
/* Interpreter ID Object */
2-
3-
#ifndef Py_INTERNAL_INTERPRETERIDOBJECT_H
4-
#define Py_INTERNAL_INTERPRETERIDOBJECT_H
5-
#ifdef __cplusplus
6-
extern "C" {
1+
#ifndef Py_CPYTHON_INTERPRETERIDOBJECT_H
2+
# error "this header file must not be included directly"
73
#endif
84

9-
#ifndef Py_BUILD_CORE
10-
# error "this header requires Py_BUILD_CORE define"
11-
#endif
5+
/* Interpreter ID Object */
126

137
PyAPI_DATA(PyTypeObject) _PyInterpreterID_Type;
148

159
PyAPI_FUNC(PyObject *) _PyInterpreterID_New(int64_t);
1610
PyAPI_FUNC(PyObject *) _PyInterpreterState_GetIDObject(PyInterpreterState *);
1711
PyAPI_FUNC(PyInterpreterState *) _PyInterpreterID_LookUp(PyObject *);
18-
19-
#ifdef __cplusplus
20-
}
21-
#endif
22-
#endif // !Py_INTERNAL_INTERPRETERIDOBJECT_H

Include/internal/pycore_pymem.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,4 @@ PyAPI_FUNC(int) _PyMem_SetupAllocators(PyMemAllocatorName allocator);
9494
#ifdef __cplusplus
9595
}
9696
#endif
97-
#endif // !Py_INTERNAL_PYMEM_H
97+
#endif /* !Py_INTERNAL_PYMEM_H */

Include/interpreteridobject.h

+17
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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1618,6 +1618,7 @@ PYTHON_HEADERS= \
16181618
$(srcdir)/Include/floatobject.h \
16191619
$(srcdir)/Include/frameobject.h \
16201620
$(srcdir)/Include/import.h \
1621+
$(srcdir)/Include/interpreteridobject.h \
16211622
$(srcdir)/Include/intrcheck.h \
16221623
$(srcdir)/Include/iterobject.h \
16231624
$(srcdir)/Include/listobject.h \
@@ -1688,6 +1689,7 @@ PYTHON_HEADERS= \
16881689
$(srcdir)/Include/cpython/genobject.h \
16891690
$(srcdir)/Include/cpython/import.h \
16901691
$(srcdir)/Include/cpython/initconfig.h \
1692+
$(srcdir)/Include/cpython/interpreteridobject.h \
16911693
$(srcdir)/Include/cpython/listobject.h \
16921694
$(srcdir)/Include/cpython/longintrepr.h \
16931695
$(srcdir)/Include/cpython/longobject.h \
@@ -1756,7 +1758,6 @@ PYTHON_HEADERS= \
17561758
$(srcdir)/Include/internal/pycore_import.h \
17571759
$(srcdir)/Include/internal/pycore_initconfig.h \
17581760
$(srcdir)/Include/internal/pycore_interp.h \
1759-
$(srcdir)/Include/internal/pycore_interpreteridobject.h \
17601761
$(srcdir)/Include/internal/pycore_intrinsics.h \
17611762
$(srcdir)/Include/internal/pycore_list.h \
17621763
$(srcdir)/Include/internal/pycore_long.h \

Modules/_xxinterpchannelsmodule.c

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11

22
/* interpreters module */
33
/* low-level access to interpreter primitives */
4-
#ifndef Py_BUILD_CORE_BUILTIN
5-
# define Py_BUILD_CORE_MODULE 1
6-
#endif
74

85
#include "Python.h"
9-
#include "pycore_pystate.h" // _PyThreadState_GET()
10-
#include "pycore_interpreteridobject.h"
6+
#include "interpreteridobject.h"
117

128

139
/*

Modules/_xxsubinterpretersmodule.c

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11

22
/* interpreters module */
33
/* low-level access to interpreter primitives */
4-
#ifndef Py_BUILD_CORE_BUILTIN
5-
# define Py_BUILD_CORE_MODULE 1
6-
#endif
74

85
#include "Python.h"
9-
// XXX This module should not rely on internal API.
10-
#include "pycore_frame.h"
11-
#include "pycore_pystate.h" // _PyThreadState_GET()
12-
#include "pycore_interpreteridobject.h"
6+
#include "interpreteridobject.h"
137

148

159
#define MODULE_NAME "_xxsubinterpreters"
@@ -376,7 +370,7 @@ _is_running(PyInterpreterState *interp)
376370
}
377371

378372
assert(!PyErr_Occurred());
379-
_PyInterpreterFrame *frame = tstate->cframe->current_frame;
373+
struct _PyInterpreterFrame *frame = tstate->cframe->current_frame;
380374
if (frame == NULL) {
381375
return 0;
382376
}
@@ -512,7 +506,7 @@ interp_create(PyObject *self, PyObject *args, PyObject *kwds)
512506
}
513507

514508
// Create and initialize the new interpreter.
515-
PyThreadState *save_tstate = _PyThreadState_GET();
509+
PyThreadState *save_tstate = PyThreadState_Get();
516510
assert(save_tstate != NULL);
517511
const PyInterpreterConfig config = isolated
518512
? (PyInterpreterConfig)_PyInterpreterConfig_INIT

Objects/interpreteridobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "Python.h"
44
#include "pycore_abstract.h" // _PyIndex_Check()
55
#include "pycore_interp.h" // _PyInterpreterState_LookUpID()
6-
#include "pycore_interpreteridobject.h"
6+
#include "interpreteridobject.h"
77

88

99
typedef struct interpid {

Objects/object.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "pycore_typevarobject.h" // _PyTypeAlias_Type, _Py_initialize_generic
1818
#include "pycore_typeobject.h" // _PyBufferWrapper_Type
1919
#include "pycore_unionobject.h" // _PyUnion_Type
20-
#include "pycore_interpreteridobject.h" // _PyInterpreterID_Type
20+
#include "interpreteridobject.h" // _PyInterpreterID_Type
2121

2222
#ifdef Py_LIMITED_API
2323
// Prevent recursive call _Py_IncRef() <=> Py_INCREF()

PCbuild/pythoncore.vcxproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@
152152
<ClInclude Include="..\Include\cpython\genobject.h" />
153153
<ClInclude Include="..\Include\cpython\import.h" />
154154
<ClInclude Include="..\Include\cpython\initconfig.h" />
155+
<ClInclude Include="..\Include\cpython\interpreteridobject.h" />
155156
<ClInclude Include="..\Include\cpython\listobject.h" />
156157
<ClInclude Include="..\Include\cpython\longintrepr.h" />
157158
<ClInclude Include="..\Include\cpython\longobject.h" />
@@ -234,7 +235,6 @@
234235
<ClInclude Include="..\Include\internal\pycore_import.h" />
235236
<ClInclude Include="..\Include\internal\pycore_initconfig.h" />
236237
<ClInclude Include="..\Include\internal\pycore_interp.h" />
237-
<ClInclude Include="..\Include\internal\pycore_interpreteridobject.h" />
238238
<ClInclude Include="..\Include\internal\pycore_intrinsics.h" />
239239
<ClInclude Include="..\Include\internal\pycore_list.h" />
240240
<ClInclude Include="..\Include\internal\pycore_long.h" />
@@ -275,6 +275,7 @@
275275
<ClInclude Include="..\Include\internal\pycore_unicodeobject.h" />
276276
<ClInclude Include="..\Include\internal\pycore_unicodeobject_generated.h" />
277277
<ClInclude Include="..\Include\internal\pycore_warnings.h" />
278+
<ClInclude Include="..\Include\interpreteridobject.h" />
278279
<ClInclude Include="..\Include\intrcheck.h" />
279280
<ClInclude Include="..\Include\iterobject.h" />
280281
<ClInclude Include="..\Include\listobject.h" />

PCbuild/pythoncore.vcxproj.filters

+6-3
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,9 @@
315315
<ClInclude Include="..\Include\pyhash.h">
316316
<Filter>Include</Filter>
317317
</ClInclude>
318+
<ClInclude Include="..\Include\interpreteridobject.h">
319+
<Filter>Include</Filter>
320+
</ClInclude>
318321
<ClInclude Include="..\Modules\hashtable.h">
319322
<Filter>Modules</Filter>
320323
</ClInclude>
@@ -459,6 +462,9 @@
459462
<ClInclude Include="..\Include\cpython\genobject.h">
460463
<Filter>Include</Filter>
461464
</ClInclude>
465+
<ClInclude Include="..\Include\cpython\interpreteridobject.h">
466+
<Filter>Include\cpython</Filter>
467+
</ClInclude>
462468
<ClInclude Include="..\Include\cpython\pythonrun.h">
463469
<Filter>Include\cpython</Filter>
464470
</ClInclude>
@@ -603,9 +609,6 @@
603609
<ClInclude Include="..\Include\internal\pycore_interp.h">
604610
<Filter>Include\internal</Filter>
605611
</ClInclude>
606-
<ClInclude Include="..\Include\internal\pycore_interpreteridobject.h">
607-
<Filter>Include\cpython</Filter>
608-
</ClInclude>
609612
<ClInclude Include="..\Include\internal\pycore_intrinsics.h">
610613
<Filter>Include\cpython</Filter>
611614
</ClInclude>

Tools/c-analyzer/cpython/_parser.py

+1
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ def clean_lines(text):
226226
Include/cpython/fileutils.h Py_CPYTHON_FILEUTILS_H 1
227227
Include/cpython/frameobject.h Py_CPYTHON_FRAMEOBJECT_H 1
228228
Include/cpython/import.h Py_CPYTHON_IMPORT_H 1
229+
Include/cpython/interpreteridobject.h Py_CPYTHON_INTERPRETERIDOBJECT_H 1
229230
Include/cpython/listobject.h Py_CPYTHON_LISTOBJECT_H 1
230231
Include/cpython/methodobject.h Py_CPYTHON_METHODOBJECT_H 1
231232
Include/cpython/object.h Py_CPYTHON_OBJECT_H 1

0 commit comments

Comments
 (0)