MCPcopy Create free account
hub / github.com/pybind/pybind11 / initialize_interpreter

Function initialize_interpreter

include/pybind11/embed.h:139–165  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

137
138#if PY_VERSION_HEX >= PYBIND11_PYCONFIG_SUPPORT_PY_VERSION_HEX
139inline void initialize_interpreter(PyConfig *config,
140 int argc = 0,
141 const char *const *argv = nullptr,
142 bool add_program_dir_to_path = true) {
143 detail::precheck_interpreter();
144 PyStatus status = PyConfig_SetBytesArgv(config, argc, const_cast<char *const *>(argv));
145 if (PyStatus_Exception(status) != 0) {
146 // A failure here indicates a character-encoding failure or the python
147 // interpreter out of memory. Give up.
148 PyConfig_Clear(config);
149 throw std::runtime_error(PyStatus_IsError(status) != 0 ? status.err_msg
150 : "Failed to prepare CPython");
151 }
152 status = Py_InitializeFromConfig(config);
153 if (PyStatus_Exception(status) != 0) {
154 PyConfig_Clear(config);
155 throw std::runtime_error(PyStatus_IsError(status) != 0 ? status.err_msg
156 : "Failed to init CPython");
157 }
158 if (add_program_dir_to_path) {
159 PyRun_SimpleString("import sys, os.path; "
160 "sys.path.insert(0, "
161 "os.path.abspath(os.path.dirname(sys.argv[0])) "
162 "if sys.argv and os.path.exists(sys.argv[0]) else '')");
163 }
164 PyConfig_Clear(config);
165}
166#endif
167
168/** \rst

Callers 2

scoped_interpreterMethod · 0.85

Calls 2

precheck_interpreterFunction · 0.85

Tested by

no test coverage detected