Skip to content

Commit 5592f2b

Browse files
authored
bpo-43268: Replace _PyThreadState_GET() with _PyInterpreterState_GET() (GH-24576)
Replace _PyThreadState_GET() with _PyInterpreterState_GET() in functions which only need the current interpreter, but don't need the current Python thread state. Replace also _PyThreadState_UncheckedGet() with _PyThreadState_GET() in faulthandler.c, since _PyThreadState_UncheckedGet() is just an alias to _PyThreadState_GET() in practice.
1 parent 2bb0bf4 commit 5592f2b

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

Include/internal/pycore_long.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,10 @@ extern "C" {
1414
// Don't call this function but _PyLong_GetZero() and _PyLong_GetOne()
1515
static inline PyObject* __PyLong_GetSmallInt_internal(int value)
1616
{
17-
PyThreadState *tstate = _PyThreadState_GET();
18-
#ifdef Py_DEBUG
19-
_Py_EnsureTstateNotNULL(tstate);
20-
#endif
17+
PyInterpreterState *interp = _PyInterpreterState_GET();
2118
assert(-_PY_NSMALLNEGINTS <= value && value < _PY_NSMALLPOSINTS);
2219
size_t index = _PY_NSMALLNEGINTS + value;
23-
PyObject *obj = (PyObject*)tstate->interp->small_ints[index];
20+
PyObject *obj = (PyObject*)interp->small_ints[index];
2421
// _PyLong_GetZero() and _PyLong_GetOne() must not be called
2522
// before _PyLong_Init() nor after _PyLong_Fini()
2623
assert(obj != NULL);

Include/internal/pycore_object.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE define"
99
#endif
1010

11-
#include "pycore_gc.h" // _PyObject_GC_IS_TRACKED()
12-
#include "pycore_interp.h" // PyInterpreterState.gc
13-
#include "pycore_pystate.h" // _PyThreadState_GET()
11+
#include "pycore_gc.h" // _PyObject_GC_IS_TRACKED()
12+
#include "pycore_interp.h" // PyInterpreterState.gc
13+
#include "pycore_pystate.h" // _PyInterpreterState_GET()
1414

1515
PyAPI_FUNC(int) _PyType_CheckConsistency(PyTypeObject *type);
1616
PyAPI_FUNC(int) _PyDict_CheckConsistency(PyObject *mp, int check_content);
@@ -85,8 +85,8 @@ static inline void _PyObject_GC_TRACK(
8585
"object is in generation which is garbage collected",
8686
filename, lineno, __func__);
8787

88-
PyThreadState *tstate = _PyThreadState_GET();
89-
PyGC_Head *generation0 = tstate->interp->gc.generation0;
88+
PyInterpreterState *interp = _PyInterpreterState_GET();
89+
PyGC_Head *generation0 = interp->gc.generation0;
9090
PyGC_Head *last = (PyGC_Head*)(generation0->_gc_prev);
9191
_PyGCHead_SET_NEXT(last, gc);
9292
_PyGCHead_SET_PREV(gc, last);

Modules/faulthandler.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "Python.h"
22
#include "pycore_initconfig.h" // _PyStatus_ERR
33
#include "pycore_pyerrors.h" // _Py_DumpExtensionModules
4+
#include "pycore_pystate.h" // _PyThreadState_GET()
45
#include "pycore_traceback.h" // _Py_DumpTracebackThreads
56
#include <signal.h>
67
#include <object.h>
@@ -208,7 +209,7 @@ faulthandler_get_fileno(PyObject **file_ptr)
208209
static PyThreadState*
209210
get_thread_state(void)
210211
{
211-
PyThreadState *tstate = _PyThreadState_UncheckedGet();
212+
PyThreadState *tstate = _PyThreadState_GET();
212213
if (tstate == NULL) {
213214
/* just in case but very unlikely... */
214215
PyErr_SetString(PyExc_RuntimeError,

0 commit comments

Comments
 (0)