bpo-39947: Add PyThreadState_GetInterpreter() (GH-18981) · python/cpython@8fb02b6 · GitHub
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

6 files changed

Lines changed: 37 additions & 9 deletions

File tree

Doc/c-api/init.rst

Lines changed: 14 additions & 5 deletions

Doc/whatsnew/3.9.rst

Lines changed: 2 additions & 1 deletion
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

Lines changed: 5 additions & 0 deletions
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;
Lines changed: 2 additions & 0 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 11 additions & 0 deletions

0 commit comments

Comments
 (0)