Skip to content

bpo-39947: Add PyInterpreterState_Get() function #18979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 13, 2020
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
12 changes: 12 additions & 0 deletions Doc/c-api/init.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,18 @@ All of the following functions must be called after :c:func:`Py_Initialize`.
to :c:func:`PyThreadState_Clear`.


.. c:function:: PyInterpreterState* PyInterpreterState_Get(void)

Get the current interpreter.

Issue a fatal error if there no current Python thread state or no current
interpreter. It cannot return NULL.

The caller must hold the GIL.

.. versionadded:: 3.9


.. c:function:: PY_INT64_T PyInterpreterState_GetID(PyInterpreterState *interp)

Return the interpreter's unique ID. If there was any error in doing
Expand Down
2 changes: 2 additions & 0 deletions Doc/whatsnew/3.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,8 @@ Optimizations
Build and C API Changes
=======================

* New :c:func:`PyInterpreterState_Get` function.

* Add ``--with-platlibdir`` option to the ``configure`` script: name of the
platform-specific library directory, stored in the new :attr:`sys.platlibdir`
attribute. See :attr:`sys.platlibdir` attribute for more information.
Expand Down
9 changes: 2 additions & 7 deletions Include/cpython/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,8 @@ struct _ts {

};

/* Get the current interpreter state.

Issue a fatal error if there no current Python thread state or no current
interpreter. It cannot return NULL.

The caller must hold the GIL.*/
PyAPI_FUNC(PyInterpreterState *) _PyInterpreterState_Get(void);
// Alias for backward compatibility with Python 3.8
#define _PyInterpreterState_Get PyInterpreterState_Get

PyAPI_FUNC(PyThreadState *) _PyThreadState_Prealloc(PyInterpreterState *);

Expand Down
11 changes: 11 additions & 0 deletions Include/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_New(void);
PyAPI_FUNC(void) PyInterpreterState_Clear(PyInterpreterState *);
PyAPI_FUNC(void) PyInterpreterState_Delete(PyInterpreterState *);

#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
/* New in 3.9 */
/* Get the current interpreter state.

Issue a fatal error if there no current Python thread state or no current
interpreter. It cannot return NULL.

The caller must hold the GIL. */
PyAPI_FUNC(PyInterpreterState *) PyInterpreterState_Get(void);
#endif

#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03080000
/* New in 3.8 */
PyAPI_FUNC(PyObject *) PyInterpreterState_GetDict(PyInterpreterState *);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add :c:func:`PyInterpreterState_Get` function to the limited C API.
2 changes: 1 addition & 1 deletion Modules/_posixsubprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
return NULL;

if ((preexec_fn != Py_None) &&
(_PyInterpreterState_Get() != PyInterpreterState_Main())) {
(PyInterpreterState_Get() != PyInterpreterState_Main())) {
PyErr_SetString(PyExc_RuntimeError,
"preexec_fn not supported within subinterpreters");
return NULL;
Expand Down
6 changes: 3 additions & 3 deletions Modules/_xxsubinterpretersmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ _copy_raw_string(PyObject *strobj)
static PyInterpreterState *
_get_current(void)
{
// _PyInterpreterState_Get() aborts if lookup fails, so don't need
// PyInterpreterState_Get() aborts if lookup fails, so don't need
// to check the result for NULL.
return _PyInterpreterState_Get();
return PyInterpreterState_Get();
}


Expand Down Expand Up @@ -1928,7 +1928,7 @@ _run_script_in_interpreter(PyInterpreterState *interp, const char *codestr,

// Switch to interpreter.
PyThreadState *save_tstate = NULL;
if (interp != _PyInterpreterState_Get()) {
if (interp != PyInterpreterState_Get()) {
// XXX Using the "head" thread isn't strictly correct.
PyThreadState *tstate = PyInterpreterState_ThreadHead(interp);
// XXX Possible GILState issues?
Expand Down
6 changes: 3 additions & 3 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ _PyInterpreterState_DeleteExceptMain(_PyRuntimeState *runtime)


PyInterpreterState *
_PyInterpreterState_Get(void)
PyInterpreterState_Get(void)
{
PyThreadState *tstate = _PyThreadState_GET();
if (tstate == NULL) {
Expand Down Expand Up @@ -1423,9 +1423,9 @@ _check_xidata(_PyCrossInterpreterData *data)
int
_PyObject_GetCrossInterpreterData(PyObject *obj, _PyCrossInterpreterData *data)
{
// _PyInterpreterState_Get() aborts if lookup fails, so we don't need
// PyInterpreterState_Get() aborts if lookup fails, so we don't need
// to check the result for NULL.
PyInterpreterState *interp = _PyInterpreterState_Get();
PyInterpreterState *interp = PyInterpreterState_Get();

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