55
55
PYBIND11_TOSTRING (name), PYBIND11_CONCAT(pybind11_init_impl_, name)); \
56
56
void PYBIND11_CONCAT (pybind11_init_, name)(::pybind11::module_ \
57
57
& variable) // NOLINT(bugprone-macro-parentheses)
58
- #define PYBIND11_PYCONFIG_SUPPORT_PY_VERSION_HEX (0x03080000 )
59
58
60
59
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
61
60
PYBIND11_NAMESPACE_BEGIN(detail)
@@ -93,37 +92,11 @@ inline void precheck_interpreter() {
93
92
}
94
93
}
95
94
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
97
98
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
127
100
inline void initialize_interpreter_pre_pyconfig (bool init_signal_handlers,
128
101
int argc,
129
102
const char *const *argv,
@@ -165,6 +138,38 @@ inline void initialize_interpreter_pre_pyconfig(bool init_signal_handlers,
165
138
}
166
139
#endif
167
140
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
+
168
173
/* * \rst
169
174
Initialize the Python interpreter. No other pybind11 or CPython API functions can be
170
175
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,
189
194
const char *const *argv = nullptr ,
190
195
bool add_program_dir_to_path = true ) {
191
196
#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);
193
199
#else
194
200
PyConfig config;
195
201
PyConfig_InitIsolatedConfig (&config);
0 commit comments