Skip to content

Commit 3225b9f

Browse files
authored
bpo-39877: Remove useless PyEval_InitThreads() calls (GH-18883)
Py_Initialize() calls PyEval_InitThreads() since Python 3.7. It's no longer needed to call it explicitly.
1 parent eebaa9b commit 3225b9f

File tree

8 files changed

+7
-21
lines changed

8 files changed

+7
-21
lines changed

Include/ceval.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,6 @@ PyAPI_FUNC(PyObject *) PyEval_EvalFrameEx(struct _frame *f, int exc);
121121
WARNING: NEVER NEST CALLS TO Py_BEGIN_ALLOW_THREADS AND
122122
Py_END_ALLOW_THREADS!!!
123123
124-
The function PyEval_InitThreads() should be called only from
125-
init_thread() in "_threadmodule.c".
126-
127124
Note that not yet all candidates have been converted to use this
128125
mechanism!
129126
*/

Lib/test/test_embed.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,8 @@ def test_pre_initialization_sys_options(self):
267267

268268
def test_bpo20891(self):
269269
"""
270-
bpo-20891: Calling PyGILState_Ensure in a non-Python thread before
271-
calling PyEval_InitThreads() must not crash. PyGILState_Ensure() must
272-
call PyEval_InitThreads() for us in this case.
270+
bpo-20891: Calling PyGILState_Ensure in a non-Python thread must not
271+
crash.
273272
"""
274273
out, err = self.run_embedded_interpreter("test_bpo20891")
275274
self.assertEqual(out, '')

Modules/_ctypes/_ctypes.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5695,7 +5695,6 @@ PyInit__ctypes(void)
56955695
ob_type is the metatype (the 'type'), defaults to PyType_Type,
56965696
tp_base is the base type, defaults to 'object' aka PyBaseObject_Type.
56975697
*/
5698-
PyEval_InitThreads();
56995698
m = PyModule_Create(&_ctypesmodule);
57005699
if (!m)
57015700
return NULL;

Modules/_ctypes/callbacks.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,6 @@ static void LoadPython(void)
423423
{
424424
if (!Py_IsInitialized()) {
425425
Py_Initialize();
426-
PyEval_InitThreads();
427426
}
428427
}
429428

Modules/_testcapimodule.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2728,8 +2728,6 @@ test_thread_state(PyObject *self, PyObject *args)
27282728
return NULL;
27292729
}
27302730

2731-
/* Ensure Python is set up for threading */
2732-
PyEval_InitThreads();
27332731
thread_done = PyThread_allocate_lock();
27342732
if (thread_done == NULL)
27352733
return PyErr_NoMemory();
@@ -4175,8 +4173,6 @@ call_in_temporary_c_thread(PyObject *self, PyObject *callback)
41754173
test_c_thread_t test_c_thread;
41764174
long thread;
41774175

4178-
PyEval_InitThreads();
4179-
41804176
test_c_thread.start_event = PyThread_allocate_lock();
41814177
test_c_thread.exit_event = PyThread_allocate_lock();
41824178
test_c_thread.callback = NULL;

Modules/_threadmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
10741074
Py_INCREF(func);
10751075
Py_INCREF(args);
10761076
Py_XINCREF(keyw);
1077-
PyEval_InitThreads(); /* Start the interpreter's thread-awareness */
1077+
10781078
ident = PyThread_start_new_thread(t_bootstrap, (void*) boot);
10791079
if (ident == PYTHREAD_INVALID_THREAD_ID) {
10801080
PyErr_SetString(ThreadError, "can't start new thread");

Programs/_testembed.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ static int test_repeated_init_and_subinterpreters(void)
6262
_testembed_Py_Initialize();
6363
mainstate = PyThreadState_Get();
6464

65-
PyEval_InitThreads();
6665
PyEval_ReleaseThread(mainstate);
6766

6867
gilstate = PyGILState_Ensure();
@@ -252,9 +251,8 @@ static int test_bpo20891(void)
252251
/* the test doesn't support custom memory allocators */
253252
putenv("PYTHONMALLOC=");
254253

255-
/* bpo-20891: Calling PyGILState_Ensure in a non-Python thread before
256-
calling PyEval_InitThreads() must not crash. PyGILState_Ensure() must
257-
call PyEval_InitThreads() for us in this case. */
254+
/* bpo-20891: Calling PyGILState_Ensure in a non-Python thread must not
255+
crash. */
258256
PyThread_type_lock lock = PyThread_allocate_lock();
259257
if (!lock) {
260258
fprintf(stderr, "PyThread_allocate_lock failed!");

Python/ceval.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ PyEval_AcquireThread(PyThreadState *tstate)
345345
_PyRuntimeState *runtime = tstate->interp->runtime;
346346
struct _ceval_runtime_state *ceval = &runtime->ceval;
347347

348-
/* Check someone has called PyEval_InitThreads() to create the lock */
348+
/* Check that _PyEval_InitThreads() was called to create the lock */
349349
assert(gil_created(&ceval->gil));
350350

351351
take_gil(ceval, tstate);
@@ -541,9 +541,7 @@ Py_AddPendingCall(int (*func)(void *), void *arg)
541541
static int
542542
handle_signals(_PyRuntimeState *runtime)
543543
{
544-
/* Only handle signals on main thread. PyEval_InitThreads must
545-
* have been called already.
546-
*/
544+
/* Only handle signals on main thread */
547545
if (PyThread_get_thread_ident() != runtime->main_thread) {
548546
return 0;
549547
}

0 commit comments

Comments
 (0)