Skip to content

Commit c052b19

Browse files
[3.13] gh-120838: Add _PyThreadState_WHENCE_FINI (gh-121013)
We also add _PyThreadState_NewBound() and drop _PyThreadState_SetWhence(). This change only affects internal API. (cherry picked from commit a905721, AKA gh-121010) Co-authored-by: Eric Snow <[email protected]>
1 parent bc515b3 commit c052b19

File tree

8 files changed

+27
-27
lines changed

8 files changed

+27
-27
lines changed

Include/cpython/pystate.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,11 @@ struct _ts {
9797
#ifdef Py_BUILD_CORE
9898
# define _PyThreadState_WHENCE_NOTSET -1
9999
# define _PyThreadState_WHENCE_UNKNOWN 0
100-
# define _PyThreadState_WHENCE_INTERP 1
101-
# define _PyThreadState_WHENCE_THREADING 2
102-
# define _PyThreadState_WHENCE_GILSTATE 3
103-
# define _PyThreadState_WHENCE_EXEC 4
100+
# define _PyThreadState_WHENCE_INIT 1
101+
# define _PyThreadState_WHENCE_FINI 2
102+
# define _PyThreadState_WHENCE_THREADING 3
103+
# define _PyThreadState_WHENCE_GILSTATE 4
104+
# define _PyThreadState_WHENCE_EXEC 5
104105
#endif
105106
int _whence;
106107

Include/internal/pycore_pystate.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,14 @@ static inline PyInterpreterState* _PyInterpreterState_GET(void) {
217217

218218
// PyThreadState functions
219219

220-
extern PyThreadState * _PyThreadState_New(
220+
// Export for _testinternalcapi
221+
PyAPI_FUNC(PyThreadState *) _PyThreadState_New(
221222
PyInterpreterState *interp,
222223
int whence);
223224
extern void _PyThreadState_Bind(PyThreadState *tstate);
225+
PyAPI_FUNC(PyThreadState *) _PyThreadState_NewBound(
226+
PyInterpreterState *interp,
227+
int whence);
224228
extern PyThreadState * _PyThreadState_RemoveExcept(PyThreadState *tstate);
225229
extern void _PyThreadState_DeleteList(PyThreadState *list);
226230
extern void _PyThreadState_ClearMimallocHeaps(PyThreadState *tstate);

Include/internal/pycore_tstate.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@ extern "C" {
1414
#include "pycore_qsbr.h" // struct qsbr
1515

1616

17-
static inline void
18-
_PyThreadState_SetWhence(PyThreadState *tstate, int whence)
19-
{
20-
tstate->_whence = whence;
21-
}
22-
23-
2417
// Every PyThreadState is actually allocated as a _PyThreadStateImpl. The
2518
// PyThreadState fields are exposed as part of the C API, although most fields
2619
// are intended to be private. The _PyThreadStateImpl fields not exposed.

Modules/_testinternalcapi.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,8 +1585,8 @@ exec_interpreter(PyObject *self, PyObject *args, PyObject *kwargs)
15851585
}
15861586

15871587
PyObject *res = NULL;
1588-
PyThreadState *tstate = PyThreadState_New(interp);
1589-
_PyThreadState_SetWhence(tstate, _PyThreadState_WHENCE_EXEC);
1588+
PyThreadState *tstate =
1589+
_PyThreadState_NewBound(interp, _PyThreadState_WHENCE_EXEC);
15901590

15911591
PyThreadState *save_tstate = PyThreadState_Swap(tstate);
15921592

Python/crossinterp.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,8 +1544,7 @@ _enter_session(_PyXI_session *session, PyInterpreterState *interp)
15441544
PyThreadState *tstate = PyThreadState_Get();
15451545
PyThreadState *prev = tstate;
15461546
if (interp != tstate->interp) {
1547-
tstate = PyThreadState_New(interp);
1548-
_PyThreadState_SetWhence(tstate, _PyThreadState_WHENCE_EXEC);
1547+
tstate = _PyThreadState_NewBound(interp, _PyThreadState_WHENCE_EXEC);
15491548
// XXX Possible GILState issues?
15501549
session->prev_tstate = PyThreadState_Swap(tstate);
15511550
assert(session->prev_tstate == prev);
@@ -1895,8 +1894,7 @@ _PyXI_EndInterpreter(PyInterpreterState *interp,
18951894
tstate = cur_tstate;
18961895
}
18971896
else {
1898-
tstate = PyThreadState_New(interp);
1899-
_PyThreadState_SetWhence(tstate, _PyThreadState_WHENCE_INTERP);
1897+
tstate = _PyThreadState_NewBound(interp, _PyThreadState_WHENCE_FINI);
19001898
assert(tstate != NULL);
19011899
save_tstate = PyThreadState_Swap(tstate);
19021900
}

Python/import.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,11 +1518,11 @@ switch_to_main_interpreter(PyThreadState *tstate)
15181518
if (_Py_IsMainInterpreter(tstate->interp)) {
15191519
return tstate;
15201520
}
1521-
PyThreadState *main_tstate = PyThreadState_New(_PyInterpreterState_Main());
1521+
PyThreadState *main_tstate = _PyThreadState_NewBound(
1522+
_PyInterpreterState_Main(), _PyThreadState_WHENCE_EXEC);
15221523
if (main_tstate == NULL) {
15231524
return NULL;
15241525
}
1525-
main_tstate->_whence = _PyThreadState_WHENCE_EXEC;
15261526
#ifndef NDEBUG
15271527
PyThreadState *old_tstate = PyThreadState_Swap(main_tstate);
15281528
assert(old_tstate == tstate);

Python/pylifecycle.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
677677
}
678678

679679
PyThreadState *tstate = _PyThreadState_New(interp,
680-
_PyThreadState_WHENCE_INTERP);
680+
_PyThreadState_WHENCE_INIT);
681681
if (tstate == NULL) {
682682
return _PyStatus_ERR("can't make first thread");
683683
}
@@ -2233,7 +2233,7 @@ new_interpreter(PyThreadState **tstate_p,
22332233
goto error;
22342234
}
22352235

2236-
tstate = _PyThreadState_New(interp, _PyThreadState_WHENCE_INTERP);
2236+
tstate = _PyThreadState_New(interp, _PyThreadState_WHENCE_INIT);
22372237
if (tstate == NULL) {
22382238
status = _PyStatus_NO_MEMORY();
22392239
goto error;

Python/pystate.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,9 +1293,8 @@ _PyInterpreterState_IDDecref(PyInterpreterState *interp)
12931293
PyThread_release_lock(interp->id_mutex);
12941294

12951295
if (refcount == 0 && interp->requires_idref) {
1296-
PyThreadState *tstate = _PyThreadState_New(interp,
1297-
_PyThreadState_WHENCE_INTERP);
1298-
_PyThreadState_Bind(tstate);
1296+
PyThreadState *tstate =
1297+
_PyThreadState_NewBound(interp, _PyThreadState_WHENCE_FINI);
12991298

13001299
// XXX Possible GILState issues?
13011300
PyThreadState *save_tstate = _PyThreadState_Swap(runtime, tstate);
@@ -1603,8 +1602,13 @@ new_threadstate(PyInterpreterState *interp, int whence)
16031602
PyThreadState *
16041603
PyThreadState_New(PyInterpreterState *interp)
16051604
{
1606-
PyThreadState *tstate = new_threadstate(interp,
1607-
_PyThreadState_WHENCE_UNKNOWN);
1605+
return _PyThreadState_NewBound(interp, _PyThreadState_WHENCE_UNKNOWN);
1606+
}
1607+
1608+
PyThreadState *
1609+
_PyThreadState_NewBound(PyInterpreterState *interp, int whence)
1610+
{
1611+
PyThreadState *tstate = new_threadstate(interp, whence);
16081612
if (tstate) {
16091613
bind_tstate(tstate);
16101614
// This makes sure there's a gilstate tstate bound

0 commit comments

Comments
 (0)