Skip to content

Commit 2bc43cd

Browse files
nanjekyejoannahwillingc
authored andcommitted
bpo-37878: Remove PyThreadState_DeleteCurrent() function (GH-15315)
* Rename PyThreadState_DeleteCurrent() to _PyThreadState_DeleteCurrent() * Move it to the internal C API Co-Authored-By: Carol Willing <[email protected]>
1 parent 2c2b561 commit 2bc43cd

File tree

7 files changed

+18
-14
lines changed

7 files changed

+18
-14
lines changed

Doc/whatsnew/3.9.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ Removed
219219
standard :class:`bytes` objects are always used.
220220
(Contributed by Jon Janzen in :issue:`36409`.)
221221

222+
* ``PyThreadState_DeleteCurrent()`` has been removed. It was not documented.
223+
(Contributed by Joannah Nanjekye in :issue:`37878`.)
224+
222225

223226
Porting to Python 3.9
224227
=====================

Include/internal/pycore_pylifecycle.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ PyAPI_FUNC(void) _PyErr_Print(PyThreadState *tstate);
110110
PyAPI_FUNC(void) _PyErr_Display(PyObject *file, PyObject *exception,
111111
PyObject *value, PyObject *tb);
112112

113+
PyAPI_FUNC(void) _PyThreadState_DeleteCurrent(_PyRuntimeState *runtime);
114+
113115
#ifdef __cplusplus
114116
}
115117
#endif

Include/pystate.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ PyAPI_FUNC(PyObject*) PyState_FindModule(struct PyModuleDef*);
5050
PyAPI_FUNC(PyThreadState *) PyThreadState_New(PyInterpreterState *);
5151
PyAPI_FUNC(void) PyThreadState_Clear(PyThreadState *);
5252
PyAPI_FUNC(void) PyThreadState_Delete(PyThreadState *);
53-
PyAPI_FUNC(void) PyThreadState_DeleteCurrent(void);
5453

5554
/* Get the current thread state.
5655
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make :c:func:`PyThreadState_DeleteCurrent` Internal.

Modules/_threadmodule.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -993,18 +993,21 @@ struct bootstate {
993993
PyObject *args;
994994
PyObject *keyw;
995995
PyThreadState *tstate;
996+
_PyRuntimeState *runtime;
996997
};
997998

998999
static void
9991000
t_bootstrap(void *boot_raw)
10001001
{
10011002
struct bootstate *boot = (struct bootstate *) boot_raw;
10021003
PyThreadState *tstate;
1004+
_PyRuntimeState *runtime;
10031005
PyObject *res;
10041006

1007+
runtime = boot->runtime;
10051008
tstate = boot->tstate;
10061009
tstate->thread_id = PyThread_get_thread_ident();
1007-
_PyThreadState_Init(&_PyRuntime, tstate);
1010+
_PyThreadState_Init(runtime, tstate);
10081011
PyEval_AcquireThread(tstate);
10091012
tstate->interp->num_threads++;
10101013
res = PyObject_Call(boot->func, boot->args, boot->keyw);
@@ -1025,13 +1028,14 @@ t_bootstrap(void *boot_raw)
10251028
PyMem_DEL(boot_raw);
10261029
tstate->interp->num_threads--;
10271030
PyThreadState_Clear(tstate);
1028-
PyThreadState_DeleteCurrent();
1031+
_PyThreadState_DeleteCurrent(runtime);
10291032
PyThread_exit_thread();
10301033
}
10311034

10321035
static PyObject *
10331036
thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
10341037
{
1038+
_PyRuntimeState *runtime = &_PyRuntime;
10351039
PyObject *func, *args, *keyw = NULL;
10361040
struct bootstate *boot;
10371041
unsigned long ident;
@@ -1062,6 +1066,7 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
10621066
boot->args = args;
10631067
boot->keyw = keyw;
10641068
boot->tstate = _PyThreadState_Prealloc(boot->interp);
1069+
boot->runtime = runtime;
10651070
if (boot->tstate == NULL) {
10661071
PyMem_DEL(boot);
10671072
return PyErr_NoMemory();

Modules/_tracemalloc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static struct {
3434
#if defined(TRACE_RAW_MALLOC)
3535
/* This lock is needed because tracemalloc_free() is called without
3636
the GIL held from PyMem_RawFree(). It cannot acquire the lock because it
37-
would introduce a deadlock in PyThreadState_DeleteCurrent(). */
37+
would introduce a deadlock in _PyThreadState_DeleteCurrent(). */
3838
static PyThread_type_lock tables_lock;
3939
# define TABLES_LOCK() PyThread_acquire_lock(tables_lock, 1)
4040
# define TABLES_UNLOCK() PyThread_release_lock(tables_lock)
@@ -728,7 +728,7 @@ tracemalloc_free(void *ctx, void *ptr)
728728
return;
729729

730730
/* GIL cannot be locked in PyMem_RawFree() because it would introduce
731-
a deadlock in PyThreadState_DeleteCurrent(). */
731+
a deadlock in _PyThreadState_DeleteCurrent(). */
732732

733733
alloc->free(alloc->ctx, ptr);
734734

Python/pystate.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@ PyThreadState_Clear(PyThreadState *tstate)
809809
}
810810

811811

812-
/* Common code for PyThreadState_Delete() and PyThreadState_DeleteCurrent() */
812+
/* Common code for PyThreadState_Delete() and _PyThreadState_DeleteCurrent() */
813813
static void
814814
tstate_delete_common(_PyRuntimeState *runtime, PyThreadState *tstate)
815815
{
@@ -858,14 +858,14 @@ PyThreadState_Delete(PyThreadState *tstate)
858858
}
859859

860860

861-
static void
861+
void
862862
_PyThreadState_DeleteCurrent(_PyRuntimeState *runtime)
863863
{
864864
struct _gilstate_runtime_state *gilstate = &runtime->gilstate;
865865
PyThreadState *tstate = _PyRuntimeGILState_GetThreadState(gilstate);
866866
if (tstate == NULL)
867867
Py_FatalError(
868-
"PyThreadState_DeleteCurrent: no current tstate");
868+
"_PyThreadState_DeleteCurrent: no current tstate");
869869
tstate_delete_common(runtime, tstate);
870870
if (gilstate->autoInterpreterState &&
871871
PyThread_tss_get(&gilstate->autoTSSkey) == tstate)
@@ -876,12 +876,6 @@ _PyThreadState_DeleteCurrent(_PyRuntimeState *runtime)
876876
PyEval_ReleaseLock();
877877
}
878878

879-
void
880-
PyThreadState_DeleteCurrent()
881-
{
882-
_PyThreadState_DeleteCurrent(&_PyRuntime);
883-
}
884-
885879

886880
/*
887881
* Delete all thread states except the one passed as argument.

0 commit comments

Comments
 (0)