MCPcopy Create free account
hub / github.com/apple/coremltools / initialize_interpreter

Function initialize_interpreter

deps/pybind11/include/pybind11/embed.h:141–167  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

139
140#if PY_VERSION_HEX >= PYBIND11_PYCONFIG_SUPPORT_PY_VERSION_HEX
141inline 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
170/** \rst

Callers 2

scoped_interpreterMethod · 0.85

Calls 2

precheck_interpreterFunction · 0.85

Tested by

no test coverage detected