From 48949222c637da9fc72b0ed6526ae1b40bb55237 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Tue, 22 Nov 2022 15:14:49 -0800 Subject: [PATCH] Use `PyEval_InitThreads()` as intended (#4350) * Use `PyEval_InitThreads()` as intended (actually matters only for Python 3.6). * Add `if defined(WITH_THREAD)` condition. https://docs.python.org/3.6/c-api/init.html#c.PyEval_InitThreads > This function is not available when thread support is disabled at compile time. * Fix oversight pointed out by @EricCousineau-TRI: Remove condition that is always false. --- include/pybind11/detail/internals.h | 3 --- include/pybind11/embed.h | 3 +++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index 6fd61098c..ef1849fbe 100644 --- a/include/pybind11/detail/internals.h +++ b/include/pybind11/detail/internals.h @@ -468,9 +468,6 @@ PYBIND11_NOINLINE internals &get_internals() { internals_ptr = new internals(); #if defined(WITH_THREAD) -# if PY_VERSION_HEX < 0x03090000 - PyEval_InitThreads(); -# endif PyThreadState *tstate = PyThreadState_Get(); if (!PYBIND11_TLS_KEY_CREATE(internals_ptr->tstate)) { pybind11_fail("get_internals: could not successfully initialize the tstate TSS key!"); diff --git a/include/pybind11/embed.h b/include/pybind11/embed.h index 0ac609e0f..5b77594b3 100644 --- a/include/pybind11/embed.h +++ b/include/pybind11/embed.h @@ -118,6 +118,9 @@ inline void initialize_interpreter(bool init_signal_handlers = true, #if PY_VERSION_HEX < 0x030B0000 Py_InitializeEx(init_signal_handlers ? 1 : 0); +# if defined(WITH_THREAD) && PY_VERSION_HEX < 0x03070000 + PyEval_InitThreads(); +# endif // Before it was special-cased in python 3.8, passing an empty or null argv // caused a segfault, so we have to reimplement the special case ourselves.