Skip to content

Commit 8fb02b6

Browse files
authored
bpo-39947: Add PyThreadState_GetInterpreter() (GH-18981)
Add PyThreadState_GetInterpreter(tstate): get the interpreter of a Python thread state.
1 parent be79373 commit 8fb02b6

File tree

6 files changed

+37
-9
lines changed

6 files changed

+37
-9
lines changed

Doc/c-api/init.rst

+14-5
Original file line numberDiff line numberDiff line change
@@ -1064,12 +1064,21 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
10641064
:c:func:`PyThreadState_Clear`.
10651065
10661066
1067-
.. c:function:: void PyThreadState_DeleteCurrent()
1067+
.. c:function:: void PyThreadState_DeleteCurrent(void)
10681068
1069-
Destroy the current thread state and release the global interpreter lock.
1070-
Like :c:func:`PyThreadState_Delete`, the global interpreter lock need not
1071-
be held. The thread state must have been reset with a previous call
1072-
to :c:func:`PyThreadState_Clear`.
1069+
Destroy the current thread state and release the global interpreter lock.
1070+
Like :c:func:`PyThreadState_Delete`, the global interpreter lock need not
1071+
be held. The thread state must have been reset with a previous call
1072+
to :c:func:`PyThreadState_Clear`.
1073+
1074+
1075+
.. c:function:: PyInterpreterState* PyThreadState_GetInterpreter(PyThreadState *tstate)
1076+
1077+
Get the interpreter of the Python thread state *tstate*.
1078+
1079+
*tstate* must not be ``NULL``.
1080+
1081+
.. versionadded:: 3.9
10731082
10741083
10751084
.. c:function:: PyInterpreterState* PyInterpreterState_Get(void)

Doc/whatsnew/3.9.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,8 @@ Optimizations
406406
Build and C API Changes
407407
=======================
408408

409-
* New :c:func:`PyInterpreterState_Get` function.
409+
* New :c:func:`PyThreadState_GetInterpreter` and
410+
:c:func:`PyInterpreterState_Get` functions to get the interpreter.
410411

411412
* Add ``--with-platlibdir`` option to the ``configure`` script: name of the
412413
platform-specific library directory, stored in the new :attr:`sys.platlibdir`

Include/pystate.h

+5
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ PyAPI_FUNC(PyThreadState *) PyThreadState_Swap(PyThreadState *);
8787
PyAPI_FUNC(PyObject *) PyThreadState_GetDict(void);
8888
PyAPI_FUNC(int) PyThreadState_SetAsyncExc(unsigned long, PyObject *);
8989

90+
#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
91+
/* New in 3.9 */
92+
PyAPI_FUNC(PyInterpreterState *) PyThreadState_GetInterpreter(PyThreadState *tstate);
93+
#endif
94+
9095
typedef
9196
enum {PyGILState_LOCKED, PyGILState_UNLOCKED}
9297
PyGILState_STATE;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add :c:func:`PyThreadState_GetInterpreter`: get the interpreter of a Python
2+
thread state.

Modules/faulthandler.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ faulthandler_py_enable(PyObject *self, PyObject *args, PyObject *kwargs)
540540
Py_XSETREF(fatal_error.file, file);
541541
fatal_error.fd = fd;
542542
fatal_error.all_threads = all_threads;
543-
fatal_error.interp = tstate->interp;
543+
fatal_error.interp = PyThreadState_GetInterpreter(tstate);
544544

545545
if (faulthandler_enable() < 0) {
546546
return NULL;
@@ -756,7 +756,7 @@ faulthandler_dump_traceback_later(PyObject *self,
756756
/* the downcast is safe: we check that 0 < timeout_us < PY_TIMEOUT_MAX */
757757
thread.timeout_us = (PY_TIMEOUT_T)timeout_us;
758758
thread.repeat = repeat;
759-
thread.interp = tstate->interp;
759+
thread.interp = PyThreadState_GetInterpreter(tstate);
760760
thread.exit = exit;
761761
thread.header = header;
762762
thread.header_len = header_len;
@@ -939,7 +939,7 @@ faulthandler_register_py(PyObject *self,
939939
user->fd = fd;
940940
user->all_threads = all_threads;
941941
user->chain = chain;
942-
user->interp = tstate->interp;
942+
user->interp = PyThreadState_GetInterpreter(tstate);
943943
user->enabled = 1;
944944

945945
Py_RETURN_NONE;

Python/pystate.c

+11
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,17 @@ PyThreadState_GetDict(void)
998998
}
999999

10001000

1001+
PyInterpreterState *
1002+
PyThreadState_GetInterpreter(PyThreadState *tstate)
1003+
{
1004+
assert(tstate != NULL);
1005+
if (tstate == NULL) {
1006+
return NULL;
1007+
}
1008+
return tstate->interp;
1009+
}
1010+
1011+
10011012
/* Asynchronously raise an exception in a thread.
10021013
Requested by Just van Rossum and Alex Martelli.
10031014
To prevent naive misuse, you must write your own extension

0 commit comments

Comments
 (0)