From 8838f38c023b58907138be65318ce4c5357c8404 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sun, 20 Nov 2022 19:41:06 -0800 Subject: [PATCH 1/3] Use `PyEval_InitThreads()` as intended (actually matters only for Python 3.6). --- include/pybind11/detail/internals.h | 3 --- include/pybind11/embed.h | 6 ++++++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index 6fd61098c4..ef1849fbea 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 0ac609e0f1..511d0c0dfc 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 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. @@ -168,6 +171,9 @@ inline void initialize_interpreter(bool init_signal_handlers = true, throw std::runtime_error(PyStatus_IsError(status) ? status.err_msg : "Failed to init CPython"); } +# if PY_VERSION_HEX < 0x03070000 + PyEval_InitThreads(); +# endif if (add_program_dir_to_path) { PyRun_SimpleString("import sys, os.path; " "sys.path.insert(0, " From a272c176d94a156e8483c8460382ffd35ada3a61 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Mon, 21 Nov 2022 02:22:46 -0800 Subject: [PATCH 2/3] 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. --- include/pybind11/embed.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/pybind11/embed.h b/include/pybind11/embed.h index 511d0c0dfc..460ee64e1d 100644 --- a/include/pybind11/embed.h +++ b/include/pybind11/embed.h @@ -118,7 +118,7 @@ inline void initialize_interpreter(bool init_signal_handlers = true, #if PY_VERSION_HEX < 0x030B0000 Py_InitializeEx(init_signal_handlers ? 1 : 0); -# if PY_VERSION_HEX < 0x03070000 +# if defined(WITH_THREAD) && PY_VERSION_HEX < 0x03070000 PyEval_InitThreads(); # endif @@ -171,7 +171,7 @@ inline void initialize_interpreter(bool init_signal_handlers = true, throw std::runtime_error(PyStatus_IsError(status) ? status.err_msg : "Failed to init CPython"); } -# if PY_VERSION_HEX < 0x03070000 +# if defined(WITH_THREAD) && PY_VERSION_HEX < 0x03070000 PyEval_InitThreads(); # endif if (add_program_dir_to_path) { From 96a6bfa64414f9d6eab334aa1681c919ede9ec20 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Tue, 22 Nov 2022 13:56:17 -0800 Subject: [PATCH 3/3] Fix oversight pointed out by @EricCousineau-TRI: Remove condition that is always false. --- include/pybind11/embed.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/include/pybind11/embed.h b/include/pybind11/embed.h index 460ee64e1d..5b77594b35 100644 --- a/include/pybind11/embed.h +++ b/include/pybind11/embed.h @@ -171,9 +171,6 @@ inline void initialize_interpreter(bool init_signal_handlers = true, throw std::runtime_error(PyStatus_IsError(status) ? status.err_msg : "Failed to init CPython"); } -# if defined(WITH_THREAD) && PY_VERSION_HEX < 0x03070000 - PyEval_InitThreads(); -# endif if (add_program_dir_to_path) { PyRun_SimpleString("import sys, os.path; " "sys.path.insert(0, "