Skip to content

Commit b94d6ba

Browse files
committed
Move initialize_interpreter_pre_pyconfig() into the detail namespace.
Move the `PYBIND11_PYCONFIG_SUPPORT_PY_VERSION_HEX` define down to where it is used for the first time, and check if it is defined already, so that it is possible to customize from the compilation command line, just in case there is some unforeseen issue for Python 3.8, 3.9, 3.10.
1 parent 29e4951 commit b94d6ba

File tree

1 file changed

+38
-32
lines changed

1 file changed

+38
-32
lines changed

include/pybind11/embed.h

+38-32
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
PYBIND11_TOSTRING(name), PYBIND11_CONCAT(pybind11_init_impl_, name)); \
5656
void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ \
5757
& variable) // NOLINT(bugprone-macro-parentheses)
58-
#define PYBIND11_PYCONFIG_SUPPORT_PY_VERSION_HEX (0x03080000)
5958

6059
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
6160
PYBIND11_NAMESPACE_BEGIN(detail)
@@ -93,37 +92,11 @@ inline void precheck_interpreter() {
9392
}
9493
}
9594

96-
PYBIND11_NAMESPACE_END(detail)
95+
#if !defined(PYBIND11_PYCONFIG_SUPPORT_PY_VERSION_HEX)
96+
# define PYBIND11_PYCONFIG_SUPPORT_PY_VERSION_HEX (0x03080000)
97+
#endif
9798

98-
#if PY_VERSION_HEX >= PYBIND11_PYCONFIG_SUPPORT_PY_VERSION_HEX
99-
inline void initialize_interpreter(PyConfig *config,
100-
int argc = 0,
101-
const char *const *argv = nullptr,
102-
bool add_program_dir_to_path = true) {
103-
detail::precheck_interpreter();
104-
PyStatus status = PyConfig_SetBytesArgv(config, argc, const_cast<char *const *>(argv));
105-
if (PyStatus_Exception(status) != 0) {
106-
// A failure here indicates a character-encoding failure or the python
107-
// interpreter out of memory. Give up.
108-
PyConfig_Clear(config);
109-
throw std::runtime_error(PyStatus_IsError(status) != 0 ? status.err_msg
110-
: "Failed to prepare CPython");
111-
}
112-
status = Py_InitializeFromConfig(config);
113-
if (PyStatus_Exception(status) != 0) {
114-
PyConfig_Clear(config);
115-
throw std::runtime_error(PyStatus_IsError(status) != 0 ? status.err_msg
116-
: "Failed to init CPython");
117-
}
118-
if (add_program_dir_to_path) {
119-
PyRun_SimpleString("import sys, os.path; "
120-
"sys.path.insert(0, "
121-
"os.path.abspath(os.path.dirname(sys.argv[0])) "
122-
"if sys.argv and os.path.exists(sys.argv[0]) else '')");
123-
}
124-
PyConfig_Clear(config);
125-
}
126-
#else
99+
#if PY_VERSION_HEX < PYBIND11_PYCONFIG_SUPPORT_PY_VERSION_HEX
127100
inline void initialize_interpreter_pre_pyconfig(bool init_signal_handlers,
128101
int argc,
129102
const char *const *argv,
@@ -165,6 +138,38 @@ inline void initialize_interpreter_pre_pyconfig(bool init_signal_handlers,
165138
}
166139
#endif
167140

141+
PYBIND11_NAMESPACE_END(detail)
142+
143+
#if PY_VERSION_HEX >= PYBIND11_PYCONFIG_SUPPORT_PY_VERSION_HEX
144+
inline void initialize_interpreter(PyConfig *config,
145+
int argc = 0,
146+
const char *const *argv = nullptr,
147+
bool add_program_dir_to_path = true) {
148+
detail::precheck_interpreter();
149+
PyStatus status = PyConfig_SetBytesArgv(config, argc, const_cast<char *const *>(argv));
150+
if (PyStatus_Exception(status) != 0) {
151+
// A failure here indicates a character-encoding failure or the python
152+
// interpreter out of memory. Give up.
153+
PyConfig_Clear(config);
154+
throw std::runtime_error(PyStatus_IsError(status) != 0 ? status.err_msg
155+
: "Failed to prepare CPython");
156+
}
157+
status = Py_InitializeFromConfig(config);
158+
if (PyStatus_Exception(status) != 0) {
159+
PyConfig_Clear(config);
160+
throw std::runtime_error(PyStatus_IsError(status) != 0 ? status.err_msg
161+
: "Failed to init CPython");
162+
}
163+
if (add_program_dir_to_path) {
164+
PyRun_SimpleString("import sys, os.path; "
165+
"sys.path.insert(0, "
166+
"os.path.abspath(os.path.dirname(sys.argv[0])) "
167+
"if sys.argv and os.path.exists(sys.argv[0]) else '')");
168+
}
169+
PyConfig_Clear(config);
170+
}
171+
#endif
172+
168173
/** \rst
169174
Initialize the Python interpreter. No other pybind11 or CPython API functions can be
170175
called before this is done; with the exception of `PYBIND11_EMBEDDED_MODULE`. The
@@ -189,7 +194,8 @@ inline void initialize_interpreter(bool init_signal_handlers = true,
189194
const char *const *argv = nullptr,
190195
bool add_program_dir_to_path = true) {
191196
#if PY_VERSION_HEX < PYBIND11_PYCONFIG_SUPPORT_PY_VERSION_HEX
192-
initialize_interpreter_pre_pyconfig(init_signal_handlers, argc, argv, add_program_dir_to_path);
197+
detail::initialize_interpreter_pre_pyconfig(
198+
init_signal_handlers, argc, argv, add_program_dir_to_path);
193199
#else
194200
PyConfig config;
195201
PyConfig_InitIsolatedConfig(&config);

0 commit comments

Comments
 (0)