]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-120838: Add _PyThreadState_WHENCE_FINI (gh-121013)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 26 Jun 2024 21:32:00 +0000 (23:32 +0200)
committerGitHub <noreply@github.com>
Wed, 26 Jun 2024 21:32:00 +0000 (15:32 -0600)
We also add _PyThreadState_NewBound() and drop _PyThreadState_SetWhence().

This change only affects internal API.

(cherry picked from commit a905721b9c5c15279e67c2f7785034b7356b2d46, AKA gh-121010)

Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
Include/cpython/pystate.h
Include/internal/pycore_pystate.h
Include/internal/pycore_tstate.h
Modules/_testinternalcapi.c
Python/crossinterp.c
Python/import.c
Python/pylifecycle.c
Python/pystate.c

index ed3ee090ae53db82f599e8760fe7fa4a408f4877..bb2af78a376d7515bd3f76ad7bd19447eac25fdd 100644 (file)
@@ -97,10 +97,11 @@ struct _ts {
 #ifdef Py_BUILD_CORE
 #  define _PyThreadState_WHENCE_NOTSET -1
 #  define _PyThreadState_WHENCE_UNKNOWN 0
-#  define _PyThreadState_WHENCE_INTERP 1
-#  define _PyThreadState_WHENCE_THREADING 2
-#  define _PyThreadState_WHENCE_GILSTATE 3
-#  define _PyThreadState_WHENCE_EXEC 4
+#  define _PyThreadState_WHENCE_INIT 1
+#  define _PyThreadState_WHENCE_FINI 2
+#  define _PyThreadState_WHENCE_THREADING 3
+#  define _PyThreadState_WHENCE_GILSTATE 4
+#  define _PyThreadState_WHENCE_EXEC 5
 #endif
     int _whence;
 
index a668d78b969bd9365d4d311c0b9be9187bf2c677..b0e72523f58ed8ec5aadc6c7844b652aa30d727c 100644 (file)
@@ -217,10 +217,14 @@ static inline PyInterpreterState* _PyInterpreterState_GET(void) {
 
 // PyThreadState functions
 
-extern PyThreadState * _PyThreadState_New(
+// Export for _testinternalcapi
+PyAPI_FUNC(PyThreadState *) _PyThreadState_New(
     PyInterpreterState *interp,
     int whence);
 extern void _PyThreadState_Bind(PyThreadState *tstate);
+PyAPI_FUNC(PyThreadState *) _PyThreadState_NewBound(
+    PyInterpreterState *interp,
+    int whence);
 extern PyThreadState * _PyThreadState_RemoveExcept(PyThreadState *tstate);
 extern void _PyThreadState_DeleteList(PyThreadState *list);
 extern void _PyThreadState_ClearMimallocHeaps(PyThreadState *tstate);
index 733e3172a1c0ff642b86eaa38f2c6a927b0d616f..befca950920bacddf7d6524e206eccb691d8f4d9 100644 (file)
@@ -14,13 +14,6 @@ extern "C" {
 #include "pycore_qsbr.h"          // struct qsbr
 
 
-static inline void
-_PyThreadState_SetWhence(PyThreadState *tstate, int whence)
-{
-    tstate->_whence = whence;
-}
-
-
 // Every PyThreadState is actually allocated as a _PyThreadStateImpl. The
 // PyThreadState fields are exposed as part of the C API, although most fields
 // are intended to be private. The _PyThreadStateImpl fields not exposed.
index 5055bca1d26214b41343360cb02b8cfb61d15e01..6185fa313daa096da0b518987a7672e8323bf83a 100644 (file)
@@ -1585,8 +1585,8 @@ exec_interpreter(PyObject *self, PyObject *args, PyObject *kwargs)
     }
 
     PyObject *res = NULL;
-    PyThreadState *tstate = PyThreadState_New(interp);
-    _PyThreadState_SetWhence(tstate, _PyThreadState_WHENCE_EXEC);
+    PyThreadState *tstate =
+        _PyThreadState_NewBound(interp, _PyThreadState_WHENCE_EXEC);
 
     PyThreadState *save_tstate = PyThreadState_Swap(tstate);
 
index 367e29d40d895ad5024c4a3877da2d3de1f62f83..a03456a8bbfd6f6f16fefa56bae3d36ce0020317 100644 (file)
@@ -1544,8 +1544,7 @@ _enter_session(_PyXI_session *session, PyInterpreterState *interp)
     PyThreadState *tstate = PyThreadState_Get();
     PyThreadState *prev = tstate;
     if (interp != tstate->interp) {
-        tstate = PyThreadState_New(interp);
-        _PyThreadState_SetWhence(tstate, _PyThreadState_WHENCE_EXEC);
+        tstate = _PyThreadState_NewBound(interp, _PyThreadState_WHENCE_EXEC);
         // XXX Possible GILState issues?
         session->prev_tstate = PyThreadState_Swap(tstate);
         assert(session->prev_tstate == prev);
@@ -1895,8 +1894,7 @@ _PyXI_EndInterpreter(PyInterpreterState *interp,
             tstate = cur_tstate;
         }
         else {
-            tstate = PyThreadState_New(interp);
-            _PyThreadState_SetWhence(tstate, _PyThreadState_WHENCE_INTERP);
+            tstate = _PyThreadState_NewBound(interp, _PyThreadState_WHENCE_FINI);
             assert(tstate != NULL);
             save_tstate = PyThreadState_Swap(tstate);
         }
index dc7ff903aab04ba2ff8572cdf58b680c7ffb7246..98ecaed36f0cdc031bd8e5548df9f319038e3791 100644 (file)
@@ -1518,11 +1518,11 @@ switch_to_main_interpreter(PyThreadState *tstate)
     if (_Py_IsMainInterpreter(tstate->interp)) {
         return tstate;
     }
-    PyThreadState *main_tstate = PyThreadState_New(_PyInterpreterState_Main());
+    PyThreadState *main_tstate = _PyThreadState_NewBound(
+            _PyInterpreterState_Main(), _PyThreadState_WHENCE_EXEC);
     if (main_tstate == NULL) {
         return NULL;
     }
-    main_tstate->_whence = _PyThreadState_WHENCE_EXEC;
 #ifndef NDEBUG
     PyThreadState *old_tstate = PyThreadState_Swap(main_tstate);
     assert(old_tstate == tstate);
index e5f749bf273c5e13a903b48e039d12e1730c59f0..3580ca411e1293c18c6b10d21fa64be4de148b49 100644 (file)
@@ -677,7 +677,7 @@ pycore_create_interpreter(_PyRuntimeState *runtime,
     }
 
     PyThreadState *tstate = _PyThreadState_New(interp,
-                                               _PyThreadState_WHENCE_INTERP);
+                                               _PyThreadState_WHENCE_INIT);
     if (tstate == NULL) {
         return _PyStatus_ERR("can't make first thread");
     }
@@ -2233,7 +2233,7 @@ new_interpreter(PyThreadState **tstate_p,
         goto error;
     }
 
-    tstate = _PyThreadState_New(interp, _PyThreadState_WHENCE_INTERP);
+    tstate = _PyThreadState_New(interp, _PyThreadState_WHENCE_INIT);
     if (tstate == NULL) {
         status = _PyStatus_NO_MEMORY();
         goto error;
index 8d31a4db200d74e010a82c27b300afb37e40ef04..602b13e18c71ae58e8a5e8479309c1a0ea5e6b7b 100644 (file)
@@ -1293,9 +1293,8 @@ _PyInterpreterState_IDDecref(PyInterpreterState *interp)
     PyThread_release_lock(interp->id_mutex);
 
     if (refcount == 0 && interp->requires_idref) {
-        PyThreadState *tstate = _PyThreadState_New(interp,
-                                                   _PyThreadState_WHENCE_INTERP);
-        _PyThreadState_Bind(tstate);
+        PyThreadState *tstate =
+            _PyThreadState_NewBound(interp, _PyThreadState_WHENCE_FINI);
 
         // XXX Possible GILState issues?
         PyThreadState *save_tstate = _PyThreadState_Swap(runtime, tstate);
@@ -1603,8 +1602,13 @@ new_threadstate(PyInterpreterState *interp, int whence)
 PyThreadState *
 PyThreadState_New(PyInterpreterState *interp)
 {
-    PyThreadState *tstate = new_threadstate(interp,
-                                            _PyThreadState_WHENCE_UNKNOWN);
+    return _PyThreadState_NewBound(interp, _PyThreadState_WHENCE_UNKNOWN);
+}
+
+PyThreadState *
+_PyThreadState_NewBound(PyInterpreterState *interp, int whence)
+{
+    PyThreadState *tstate = new_threadstate(interp, whence);
     if (tstate) {
         bind_tstate(tstate);
         // This makes sure there's a gilstate tstate bound