Skip to content

Commit be79373

Browse files
authored
bpo-39947: Add PyInterpreterState_Get() function (GH-18979)
* Rename _PyInterpreterState_Get() to PyInterpreterState_Get() and move it the limited C API. * Add _PyInterpreterState_Get() alias to PyInterpreterState_Get() for backward compatibility with Python 3.8.
1 parent ff4584c commit be79373

File tree

8 files changed

+35
-14
lines changed

8 files changed

+35
-14
lines changed

Doc/c-api/init.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,18 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
10721072
to :c:func:`PyThreadState_Clear`.
10731073
10741074
1075+
.. c:function:: PyInterpreterState* PyInterpreterState_Get(void)
1076+
1077+
Get the current interpreter.
1078+
1079+
Issue a fatal error if there no current Python thread state or no current
1080+
interpreter. It cannot return NULL.
1081+
1082+
The caller must hold the GIL.
1083+
1084+
.. versionadded:: 3.9
1085+
1086+
10751087
.. c:function:: PY_INT64_T PyInterpreterState_GetID(PyInterpreterState *interp)
10761088
10771089
Return the interpreter's unique ID. If there was any error in doing

Doc/whatsnew/3.9.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,8 @@ Optimizations
406406
Build and C API Changes
407407
=======================
408408

409+
* New :c:func:`PyInterpreterState_Get` function.
410+
409411
* Add ``--with-platlibdir`` option to the ``configure`` script: name of the
410412
platform-specific library directory, stored in the new :attr:`sys.platlibdir`
411413
attribute. See :attr:`sys.platlibdir` attribute for more information.

Include/cpython/pystate.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,8 @@ struct _ts {
139139

140140
};
141141

142-
/* Get the current interpreter state.
143-
144-
Issue a fatal error if there no current Python thread state or no current
145-
interpreter. It cannot return NULL.
146-
147-
The caller must hold the GIL.*/
148-
PyAPI_FUNC(PyInterpreterState *) _PyInterpreterState_Get(void);
142+
// Alias for backward compatibility with Python 3.8
143+
#define _PyInterpreterState_Get PyInterpreterState_Get
149144

150145
PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *);
151146

Include/pystate.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_New(void);
2828
PyAPI_FUNC(void) PyInterpreterState_Clear(PyInterpreterState *);
2929
PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *);
3030

31+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
32+
/* New in 3.9 */
33+
/* Get the current interpreter state.
34+
35+
Issue a fatal error if there no current Python thread state or no current
36+
interpreter. It cannot return NULL.
37+
38+
The caller must hold the GIL. */
39+
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Get(void);
40+
#endif
41+
3142
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03080000
3243
/* New in 3.8 */
3344
PyAPI_FUNC(PyObject *) PyInterpreterState_GetDict(PyInterpreterState *);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add :c:func:`PyInterpreterState_Get` function to the limited C API.

Modules/_posixsubprocess.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
635635
return NULL;
636636

637637
if ((preexec_fn != Py_None) &&
638-
(_PyInterpreterState_Get() != PyInterpreterState_Main())) {
638+
(PyInterpreterState_Get() != PyInterpreterState_Main())) {
639639
PyErr_SetString(PyExc_RuntimeError,
640640
"preexec_fn not supported within subinterpreters");
641641
return NULL;

Modules/_xxsubinterpretersmodule.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ _copy_raw_string(PyObject *strobj)
2626
static PyInterpreterState *
2727
_get_current(void)
2828
{
29-
// _PyInterpreterState_Get() aborts if lookup fails, so don't need
29+
// PyInterpreterState_Get() aborts if lookup fails, so don't need
3030
// to check the result for NULL.
31-
return _PyInterpreterState_Get();
31+
return PyInterpreterState_Get();
3232
}
3333

3434

@@ -1928,7 +1928,7 @@ _run_script_in_interpreter(PyInterpreterState *interp, const char *codestr,
19281928

19291929
// Switch to interpreter.
19301930
PyThreadState *save_tstate = NULL;
1931-
if (interp != _PyInterpreterState_Get()) {
1931+
if (interp != PyInterpreterState_Get()) {
19321932
// XXX Using the "head" thread isn't strictly correct.
19331933
PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
19341934
// XXX Possible GILState issues?

Python/pystate.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime)
396396

397397

398398
PyInterpreterState *
399-
_PyInterpreterState_Get(void)
399+
PyInterpreterState_Get(void)
400400
{
401401
PyThreadState *tstate = _PyThreadState_GET();
402402
if (tstate == NULL) {
@@ -1423,9 +1423,9 @@ _check_xidata(_PyCrossInterpreterData *data)
14231423
int
14241424
_PyObject_GetCrossInterpreterData(PyObject *obj, _PyCrossInterpreterData *data)
14251425
{
1426-
// _PyInterpreterState_Get() aborts if lookup fails, so we don't need
1426+
// PyInterpreterState_Get() aborts if lookup fails, so we don't need
14271427
// to check the result for NULL.
1428-
PyInterpreterState *interp = _PyInterpreterState_Get();
1428+
PyInterpreterState *interp = PyInterpreterState_Get();
14291429

14301430
// Reset data before re-populating.
14311431
*data = (_PyCrossInterpreterData){0};

0 commit comments

Comments
 (0)