18#if defined(PYPY_VERSION)
19# error Embedding the interpreter is not supported with PyPy
22#define PYBIND11_EMBEDDED_MODULE_IMPL(name) \
23 extern "C" PyObject *pybind11_init_impl_##name(); \
24 extern "C" PyObject *pybind11_init_impl_##name() { return pybind11_init_wrapper_##name(); }
41#define PYBIND11_EMBEDDED_MODULE(name, variable) \
42 static ::pybind11::module_::module_def PYBIND11_CONCAT(pybind11_module_def_, name); \
43 static void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &); \
44 static PyObject PYBIND11_CONCAT(*pybind11_init_wrapper_, name)() { \
45 auto m = ::pybind11::module_::create_extension_module( \
46 PYBIND11_TOSTRING(name), nullptr, &PYBIND11_CONCAT(pybind11_module_def_, name)); \
48 PYBIND11_CONCAT(pybind11_init_, name)(m); \
51 PYBIND11_CATCH_INIT_EXCEPTIONS \
53 PYBIND11_EMBEDDED_MODULE_IMPL(name) \
54 ::pybind11::detail::embedded_module PYBIND11_CONCAT(pybind11_module_, name)( \
55 PYBIND11_TOSTRING(name), PYBIND11_CONCAT(pybind11_init_impl_, name)); \
56 void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ \
66 if (Py_IsInitialized() != 0) {
67 pybind11_fail(
"Can't add new modules after the interpreter has been initialized");
70 auto result = PyImport_AppendInittab(
name,
init);
85 wchar_t *widened_arg = Py_DecodeLocale(safe_arg,
nullptr);
90 if (Py_IsInitialized() != 0) {
95#if !defined(PYBIND11_PYCONFIG_SUPPORT_PY_VERSION_HEX)
96# define PYBIND11_PYCONFIG_SUPPORT_PY_VERSION_HEX (0x03080000)
99#if PY_VERSION_HEX < PYBIND11_PYCONFIG_SUPPORT_PY_VERSION_HEX
100inline void initialize_interpreter_pre_pyconfig(
bool init_signal_handlers,
102 const char *
const *argv,
103 bool add_program_dir_to_path) {
104 detail::precheck_interpreter();
105 Py_InitializeEx(init_signal_handlers ? 1 : 0);
106# if defined(WITH_THREAD) && PY_VERSION_HEX < 0x03070000
107 PyEval_InitThreads();
112 bool special_case = (argv ==
nullptr || argc <= 0);
114 const char *
const empty_argv[]{
"\0"};
115 const char *
const *safe_argv = special_case ? empty_argv : argv;
120 auto argv_size =
static_cast<size_t>(argc);
122 std::unique_ptr<wchar_t *[]> widened_argv(
new wchar_t *[argv_size]);
123 std::vector<std::unique_ptr<wchar_t[], detail::wide_char_arg_deleter>> widened_argv_entries;
124 widened_argv_entries.reserve(argv_size);
125 for (
size_t ii = 0; ii < argv_size; ++ii) {
126 widened_argv_entries.emplace_back(detail::widen_chars(safe_argv[ii]));
127 if (!widened_argv_entries.back()) {
132 widened_argv[ii] = widened_argv_entries.back().get();
135 auto *pysys_argv = widened_argv.get();
137 PySys_SetArgvEx(argc, pysys_argv,
static_cast<int>(add_program_dir_to_path));
143#if PY_VERSION_HEX >= PYBIND11_PYCONFIG_SUPPORT_PY_VERSION_HEX
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) {
153 PyConfig_Clear(config);
154 throw std::runtime_error(PyStatus_IsError(status) != 0 ? status.err_msg
155 :
"Failed to prepare CPython");
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");
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 '')");
169 PyConfig_Clear(config);
194 const char *
const *argv =
nullptr,
195 bool add_program_dir_to_path =
true) {
196#if PY_VERSION_HEX < PYBIND11_PYCONFIG_SUPPORT_PY_VERSION_HEX
197 detail::initialize_interpreter_pre_pyconfig(
198 init_signal_handlers, argc, argv, add_program_dir_to_path);
201 PyConfig_InitIsolatedConfig(&config);
203 config.use_environment = 1;
204 config.install_signal_handlers = init_signal_handlers ? 1 : 0;
245 handle builtins(PyEval_GetBuiltins());
251 detail::internals **internals_ptr_ptr = detail::get_internals_pp();
253 if (builtins.contains(
id) && isinstance<capsule>(builtins[
id])) {
254 internals_ptr_ptr =
capsule(builtins[
id]);
258 detail::get_local_internals().registered_types_cpp.clear();
259 detail::get_local_internals().registered_exception_translators.clear();
263 if (internals_ptr_ptr) {
264 delete *internals_ptr_ptr;
265 *internals_ptr_ptr =
nullptr;
288 const char *
const *argv =
nullptr,
289 bool add_program_dir_to_path =
true) {
293#if PY_VERSION_HEX >= PYBIND11_PYCONFIG_SUPPORT_PY_VERSION_HEX
296 const char *
const *argv =
nullptr,
297 bool add_program_dir_to_path =
true) {
\rst Holds a reference to a Python object (no reference counting)
\rst Scope guard version of initialize_interpreter and finalize_interpreter.
scoped_interpreter & operator=(const scoped_interpreter &)=delete
scoped_interpreter(const scoped_interpreter &)=delete
scoped_interpreter(PyConfig *config, int argc=0, const char *const *argv=nullptr, bool add_program_dir_to_path=true)
scoped_interpreter & operator=(scoped_interpreter &&)=delete
scoped_interpreter(bool init_signal_handlers=true, int argc=0, const char *const *argv=nullptr, bool add_program_dir_to_path=true)
scoped_interpreter(scoped_interpreter &&other) noexcept
PYBIND11_NOINLINE void pybind11_fail(const char *reason)
Thrown when pybind11::cast or.
#define PYBIND11_NAMESPACE_END(name)
#define PYBIND11_NAMESPACE_BEGIN(name)
#define PYBIND11_INTERNALS_ID
wchar_t * widen_chars(const char *safe_arg)
void finalize_interpreter()
\rst Shut down the Python interpreter.
void initialize_interpreter(bool init_signal_handlers=true, int argc=0, const char *const *argv=nullptr, bool add_program_dir_to_path=true)
\rst Initialize the Python interpreter.
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
void finalize_interpreter()
\rst Shut down the Python interpreter.
void initialize_interpreter(PyConfig *config, int argc=0, const char *const *argv=nullptr, bool add_program_dir_to_path=true)
void precheck_interpreter()
Python 2.7/3.x compatible version of PyImport_AppendInittab and error checks.
embedded_module(const char *name, init_t init)
Annotation for function names.
void operator()(wchar_t *ptr) const