Skip to content

Commit cc89470

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 94ce6ec commit cc89470

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,
@@ -162,6 +135,38 @@ inline void initialize_interpreter_pre_pyconfig(bool init_signal_handlers,
162135
}
163136
#endif
164137

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

0 commit comments

Comments
 (0)