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.
This commit is contained in:
Victor Stinner 2020-03-09 20:56:57 +01:00 committed by GitHub
parent eebaa9bfc5
commit 3225b9f973
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 7 additions and 21 deletions

View File

@ -121,9 +121,6 @@ PyAPI_FUNC(PyObject *) PyEval_EvalFrameEx(struct _frame *f, int exc);
WARNING: NEVER NEST CALLS TO Py_BEGIN_ALLOW_THREADS AND WARNING: NEVER NEST CALLS TO Py_BEGIN_ALLOW_THREADS AND
Py_END_ALLOW_THREADS!!! Py_END_ALLOW_THREADS!!!
The function PyEval_InitThreads() should be called only from
init_thread() in "_threadmodule.c".
Note that not yet all candidates have been converted to use this Note that not yet all candidates have been converted to use this
mechanism! mechanism!
*/ */

View File

@ -267,9 +267,8 @@ class EmbeddingTests(EmbeddingTestsMixin, unittest.TestCase):
def test_bpo20891(self): def test_bpo20891(self):
""" """
bpo-20891: Calling PyGILState_Ensure in a non-Python thread before bpo-20891: Calling PyGILState_Ensure in a non-Python thread must not
calling PyEval_InitThreads() must not crash. PyGILState_Ensure() must crash.
call PyEval_InitThreads() for us in this case.
""" """
out, err = self.run_embedded_interpreter("test_bpo20891") out, err = self.run_embedded_interpreter("test_bpo20891")
self.assertEqual(out, '') self.assertEqual(out, '')

View File

@ -5695,7 +5695,6 @@ PyInit__ctypes(void)
ob_type is the metatype (the 'type'), defaults to PyType_Type, ob_type is the metatype (the 'type'), defaults to PyType_Type,
tp_base is the base type, defaults to 'object' aka PyBaseObject_Type. tp_base is the base type, defaults to 'object' aka PyBaseObject_Type.
*/ */
PyEval_InitThreads();
m = PyModule_Create(&_ctypesmodule); m = PyModule_Create(&_ctypesmodule);
if (!m) if (!m)
return NULL; return NULL;

View File

@ -423,7 +423,6 @@ static void LoadPython(void)
{ {
if (!Py_IsInitialized()) { if (!Py_IsInitialized()) {
Py_Initialize(); Py_Initialize();
PyEval_InitThreads();
} }
} }

View File

@ -2728,8 +2728,6 @@ test_thread_state(PyObject *self, PyObject *args)
return NULL; return NULL;
} }
/* Ensure Python is set up for threading */
PyEval_InitThreads();
thread_done = PyThread_allocate_lock(); thread_done = PyThread_allocate_lock();
if (thread_done == NULL) if (thread_done == NULL)
return PyErr_NoMemory(); return PyErr_NoMemory();
@ -4175,8 +4173,6 @@ call_in_temporary_c_thread(PyObject *self, PyObject *callback)
test_c_thread_t test_c_thread; test_c_thread_t test_c_thread;
long thread; long thread;
PyEval_InitThreads();
test_c_thread.start_event = PyThread_allocate_lock(); test_c_thread.start_event = PyThread_allocate_lock();
test_c_thread.exit_event = PyThread_allocate_lock(); test_c_thread.exit_event = PyThread_allocate_lock();
test_c_thread.callback = NULL; test_c_thread.callback = NULL;

View File

@ -1074,7 +1074,7 @@ thread_PyThread_start_new_thread(PyObject *self, PyObject *fargs)
Py_INCREF(func); Py_INCREF(func);
Py_INCREF(args); Py_INCREF(args);
Py_XINCREF(keyw); Py_XINCREF(keyw);
PyEval_InitThreads(); /* Start the interpreter's thread-awareness */
ident = PyThread_start_new_thread(t_bootstrap, (void*) boot); ident = PyThread_start_new_thread(t_bootstrap, (void*) boot);
if (ident == PYTHREAD_INVALID_THREAD_ID) { if (ident == PYTHREAD_INVALID_THREAD_ID) {
PyErr_SetString(ThreadError, "can't start new thread"); PyErr_SetString(ThreadError, "can't start new thread");

View File

@ -62,7 +62,6 @@ static int test_repeated_init_and_subinterpreters(void)
_testembed_Py_Initialize(); _testembed_Py_Initialize();
mainstate = PyThreadState_Get(); mainstate = PyThreadState_Get();
PyEval_InitThreads();
PyEval_ReleaseThread(mainstate); PyEval_ReleaseThread(mainstate);
gilstate = PyGILState_Ensure(); gilstate = PyGILState_Ensure();
@ -252,9 +251,8 @@ static int test_bpo20891(void)
/* the test doesn't support custom memory allocators */ /* the test doesn't support custom memory allocators */
putenv("PYTHONMALLOC="); putenv("PYTHONMALLOC=");
/* bpo-20891: Calling PyGILState_Ensure in a non-Python thread before /* bpo-20891: Calling PyGILState_Ensure in a non-Python thread must not
calling PyEval_InitThreads() must not crash. PyGILState_Ensure() must crash. */
call PyEval_InitThreads() for us in this case. */
PyThread_type_lock lock = PyThread_allocate_lock(); PyThread_type_lock lock = PyThread_allocate_lock();
if (!lock) { if (!lock) {
fprintf(stderr, "PyThread_allocate_lock failed!"); fprintf(stderr, "PyThread_allocate_lock failed!");

View File

@ -345,7 +345,7 @@ PyEval_AcquireThread(PyThreadState *tstate)
_PyRuntimeState *runtime = tstate->interp->runtime; _PyRuntimeState *runtime = tstate->interp->runtime;
struct _ceval_runtime_state *ceval = &runtime->ceval; struct _ceval_runtime_state *ceval = &runtime->ceval;
/* Check someone has called PyEval_InitThreads() to create the lock */ /* Check that _PyEval_InitThreads() was called to create the lock */
assert(gil_created(&ceval->gil)); assert(gil_created(&ceval->gil));
take_gil(ceval, tstate); take_gil(ceval, tstate);
@ -541,9 +541,7 @@ Py_AddPendingCall(int (*func)(void *), void *arg)
static int static int
handle_signals(_PyRuntimeState *runtime) handle_signals(_PyRuntimeState *runtime)
{ {
/* Only handle signals on main thread. PyEval_InitThreads must /* Only handle signals on main thread */
* have been called already.
*/
if (PyThread_get_thread_ident() != runtime->main_thread) { if (PyThread_get_thread_ident() != runtime->main_thread) {
return 0; return 0;
} }