15 #if defined(PYPY_VERSION) 16 # error Embedding the interpreter is not supported with PyPy 19 #if PY_MAJOR_VERSION >= 3 20 # define PYBIND11_EMBEDDED_MODULE_IMPL(name) \ 21 extern "C" PyObject *pybind11_init_impl_##name() { \ 22 return pybind11_init_wrapper_##name(); \ 25 # define PYBIND11_EMBEDDED_MODULE_IMPL(name) \ 26 extern "C" void pybind11_init_impl_##name() { \ 27 pybind11_init_wrapper_##name(); \ 46 #define PYBIND11_EMBEDDED_MODULE(name, variable) \ 47 static void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &); \ 48 static PyObject PYBIND11_CONCAT(*pybind11_init_wrapper_, name)() { \ 49 auto m = pybind11::module(PYBIND11_TOSTRING(name)); \ 51 PYBIND11_CONCAT(pybind11_init_, name)(m); \ 53 } catch (pybind11::error_already_set &e) { \ 54 PyErr_SetString(PyExc_ImportError, e.what()); \ 56 } catch (const std::exception &e) { \ 57 PyErr_SetString(PyExc_ImportError, e.what()); \ 61 PYBIND11_EMBEDDED_MODULE_IMPL(name) \ 62 pybind11::detail::embedded_module name(PYBIND11_TOSTRING(name), \ 63 PYBIND11_CONCAT(pybind11_init_impl_, name)); \ 64 void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &variable) 70 struct embedded_module {
72 #if PY_MAJOR_VERSION >= 3 73 using init_t = PyObject *(*)();
78 if (Py_IsInitialized())
79 pybind11_fail(
"Can't add new modules after the interpreter has been initialized");
81 auto result = PyImport_AppendInittab(name, init);
83 pybind11_fail(
"Insufficient memory to add a new module");
103 if (Py_IsInitialized())
104 pybind11_fail(
"The interpreter is already running");
106 Py_InitializeEx(init_signal_handlers ? 1 : 0);
109 module::import(
"sys").attr(
"path").cast<
list>().append(
".");
148 handle builtins(PyEval_GetBuiltins());
156 if (builtins.
contains(
id) && isinstance<capsule>(builtins[id]))
157 internals_ptr_ptr =
capsule(builtins[
id]);
161 if (internals_ptr_ptr) {
162 delete *internals_ptr_ptr;
163 *internals_ptr_ptr =
nullptr;
197 bool is_valid =
true;
scoped_interpreter(bool init_signal_handlers=true)
#define PYBIND11_NAMESPACE
bool contains(T &&item) const
Check if the given item is contained within this object, i.e. item in obj.
internals **& get_internals_pp()
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
#define NAMESPACE_END(name)
embedded_module(const char *name, init_t init)
void finalize_interpreter()
scoped_interpreter(scoped_interpreter &&other) noexcept
#define PYBIND11_INTERNALS_ID
void initialize_interpreter(bool init_signal_handlers=true)
Annotation for function names.
#define NAMESPACE_BEGIN(name)