bpo-39947: Add PyThreadState_GetInterpreter() by vstinner · Pull Request #18981 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions Doc/c-api/init.rst
3 changes: 2 additions & 1 deletion Doc/whatsnew/3.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,8 @@ Optimizations
Build and C API Changes
=======================

* New :c:func:`PyInterpreterState_Get` function.
* New :c:func:`PyThreadState_GetInterpreter` and
:c:func:`PyInterpreterState_Get` functions to get the interpreter.

* Add ``--with-platlibdir`` option to the ``configure`` script: name of the
platform-specific library directory, stored in the new :attr:`sys.platlibdir`
Expand Down
5 changes: 5 additions & 0 deletions Include/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ PyAPI_FUNC(PyThreadState *) PyThreadState_Swap(PyThreadState *);
PyAPI_FUNC(PyObject *) PyThreadState_GetDict(void);
PyAPI_FUNC(int) PyThreadState_SetAsyncExc(unsigned long, PyObject *);

#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
/* New in 3.9 */
PyAPI_FUNC(PyInterpreterState *) PyThreadState_GetInterpreter(PyThreadState *tstate);
#endif

typedef
enum {PyGILState_LOCKED, PyGILState_UNLOCKED}
PyGILState_STATE;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add :c:func:`PyThreadState_GetInterpreter`: get the interpreter of a Python
thread state.
6 changes: 3 additions & 3 deletions Modules/faulthandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ faulthandler_py_enable(PyObject *self, PyObject *args, PyObject *kwargs)
Py_XSETREF(fatal_error.file, file);
fatal_error.fd = fd;
fatal_error.all_threads = all_threads;
fatal_error.interp = tstate->interp;
fatal_error.interp = PyThreadState_GetInterpreter(tstate);

if (faulthandler_enable() < 0) {
return NULL;
Expand Down Expand Up @@ -756,7 +756,7 @@ faulthandler_dump_traceback_later(PyObject *self,
/* the downcast is safe: we check that 0 < timeout_us < PY_TIMEOUT_MAX */
thread.timeout_us = (PY_TIMEOUT_T)timeout_us;
thread.repeat = repeat;
thread.interp = tstate->interp;
thread.interp = PyThreadState_GetInterpreter(tstate);
thread.exit = exit;
thread.header = header;
thread.header_len = header_len;
Expand Down Expand Up @@ -939,7 +939,7 @@ faulthandler_register_py(PyObject *self,
user->fd = fd;
user->all_threads = all_threads;
user->chain = chain;
user->interp = tstate->interp;
user->interp = PyThreadState_GetInterpreter(tstate);
user->enabled = 1;

Py_RETURN_NONE;
Expand Down
11 changes: 11 additions & 0 deletions Python/pystate.c