embed.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. pybind11/embed.h: Support for embedding the interpreter
  3. Copyright (c) 2017 Wenzel Jakob <wenzel.jakob@epfl.ch>
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. */
  7. #pragma once
  8. #include "pybind11.h"
  9. #include "eval.h"
  10. #if defined(PYPY_VERSION)
  11. # error Embedding the interpreter is not supported with PyPy
  12. #endif
  13. #if PY_MAJOR_VERSION >= 3
  14. # define PYBIND11_EMBEDDED_MODULE_IMPL(name) \
  15. extern "C" PyObject *pybind11_init_impl_##name(); \
  16. extern "C" PyObject *pybind11_init_impl_##name() { \
  17. return pybind11_init_wrapper_##name(); \
  18. }
  19. #else
  20. # define PYBIND11_EMBEDDED_MODULE_IMPL(name) \
  21. extern "C" void pybind11_init_impl_##name(); \
  22. extern "C" void pybind11_init_impl_##name() { \
  23. pybind11_init_wrapper_##name(); \
  24. }
  25. #endif
  26. /** \rst
  27. Add a new module to the table of builtins for the interpreter. Must be
  28. defined in global scope. The first macro parameter is the name of the
  29. module (without quotes). The second parameter is the variable which will
  30. be used as the interface to add functions and classes to the module.
  31. .. code-block:: cpp
  32. PYBIND11_EMBEDDED_MODULE(example, m) {
  33. // ... initialize functions and classes here
  34. m.def("foo", []() {
  35. return "Hello, World!";
  36. });
  37. }
  38. \endrst */
  39. #define PYBIND11_EMBEDDED_MODULE(name, variable) \
  40. static ::pybind11::module_::module_def \
  41. PYBIND11_CONCAT(pybind11_module_def_, name); \
  42. static void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &); \
  43. static PyObject PYBIND11_CONCAT(*pybind11_init_wrapper_, name)() { \
  44. auto m = ::pybind11::module_::create_extension_module( \
  45. PYBIND11_TOSTRING(name), nullptr, \
  46. &PYBIND11_CONCAT(pybind11_module_def_, name)); \
  47. try { \
  48. PYBIND11_CONCAT(pybind11_init_, name)(m); \
  49. return m.ptr(); \
  50. } PYBIND11_CATCH_INIT_EXCEPTIONS \
  51. } \
  52. PYBIND11_EMBEDDED_MODULE_IMPL(name) \
  53. ::pybind11::detail::embedded_module PYBIND11_CONCAT(pybind11_module_, name) \
  54. (PYBIND11_TOSTRING(name), \
  55. PYBIND11_CONCAT(pybind11_init_impl_, name)); \
  56. void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &variable)
  57. PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
  58. PYBIND11_NAMESPACE_BEGIN(detail)
  59. /// Python 2.7/3.x compatible version of `PyImport_AppendInittab` and error checks.
  60. struct embedded_module {
  61. #if PY_MAJOR_VERSION >= 3
  62. using init_t = PyObject *(*)();
  63. #else
  64. using init_t = void (*)();
  65. #endif
  66. embedded_module(const char *name, init_t init) {
  67. if (Py_IsInitialized())
  68. pybind11_fail("Can't add new modules after the interpreter has been initialized");
  69. auto result = PyImport_AppendInittab(name, init);
  70. if (result == -1)
  71. pybind11_fail("Insufficient memory to add a new module");
  72. }
  73. };
  74. PYBIND11_NAMESPACE_END(detail)
  75. /** \rst
  76. Initialize the Python interpreter. No other pybind11 or CPython API functions can be
  77. called before this is done; with the exception of `PYBIND11_EMBEDDED_MODULE`. The
  78. optional parameter can be used to skip the registration of signal handlers (see the
  79. `Python documentation`_ for details). Calling this function again after the interpreter
  80. has already been initialized is a fatal error.
  81. If initializing the Python interpreter fails, then the program is terminated. (This
  82. is controlled by the CPython runtime and is an exception to pybind11's normal behavior
  83. of throwing exceptions on errors.)
  84. .. _Python documentation: https://docs.python.org/3/c-api/init.html#c.Py_InitializeEx
  85. \endrst */
  86. inline void initialize_interpreter(bool init_signal_handlers = true) {
  87. if (Py_IsInitialized())
  88. pybind11_fail("The interpreter is already running");
  89. Py_InitializeEx(init_signal_handlers ? 1 : 0);
  90. // Make .py files in the working directory available by default
  91. module_::import("sys").attr("path").cast<list>().append(".");
  92. }
  93. /** \rst
  94. Shut down the Python interpreter. No pybind11 or CPython API functions can be called
  95. after this. In addition, pybind11 objects must not outlive the interpreter:
  96. .. code-block:: cpp
  97. { // BAD
  98. py::initialize_interpreter();
  99. auto hello = py::str("Hello, World!");
  100. py::finalize_interpreter();
  101. } // <-- BOOM, hello's destructor is called after interpreter shutdown
  102. { // GOOD
  103. py::initialize_interpreter();
  104. { // scoped
  105. auto hello = py::str("Hello, World!");
  106. } // <-- OK, hello is cleaned up properly
  107. py::finalize_interpreter();
  108. }
  109. { // BETTER
  110. py::scoped_interpreter guard{};
  111. auto hello = py::str("Hello, World!");
  112. }
  113. .. warning::
  114. The interpreter can be restarted by calling `initialize_interpreter` again.
  115. Modules created using pybind11 can be safely re-initialized. However, Python
  116. itself cannot completely unload binary extension modules and there are several
  117. caveats with regard to interpreter restarting. All the details can be found
  118. in the CPython documentation. In short, not all interpreter memory may be
  119. freed, either due to reference cycles or user-created global data.
  120. \endrst */
  121. inline void finalize_interpreter() {
  122. handle builtins(PyEval_GetBuiltins());
  123. const char *id = PYBIND11_INTERNALS_ID;
  124. // Get the internals pointer (without creating it if it doesn't exist). It's possible for the
  125. // internals to be created during Py_Finalize() (e.g. if a py::capsule calls `get_internals()`
  126. // during destruction), so we get the pointer-pointer here and check it after Py_Finalize().
  127. detail::internals **internals_ptr_ptr = detail::get_internals_pp();
  128. // It could also be stashed in builtins, so look there too:
  129. if (builtins.contains(id) && isinstance<capsule>(builtins[id]))
  130. internals_ptr_ptr = capsule(builtins[id]);
  131. Py_Finalize();
  132. if (internals_ptr_ptr) {
  133. delete *internals_ptr_ptr;
  134. *internals_ptr_ptr = nullptr;
  135. }
  136. }
  137. /** \rst
  138. Scope guard version of `initialize_interpreter` and `finalize_interpreter`.
  139. This a move-only guard and only a single instance can exist.
  140. .. code-block:: cpp
  141. #include <pybind11/embed.h>
  142. int main() {
  143. py::scoped_interpreter guard{};
  144. py::print(Hello, World!);
  145. } // <-- interpreter shutdown
  146. \endrst */
  147. class scoped_interpreter {
  148. public:
  149. scoped_interpreter(bool init_signal_handlers = true) {
  150. initialize_interpreter(init_signal_handlers);
  151. }
  152. scoped_interpreter(const scoped_interpreter &) = delete;
  153. scoped_interpreter(scoped_interpreter &&other) noexcept { other.is_valid = false; }
  154. scoped_interpreter &operator=(const scoped_interpreter &) = delete;
  155. scoped_interpreter &operator=(scoped_interpreter &&) = delete;
  156. ~scoped_interpreter() {
  157. if (is_valid)
  158. finalize_interpreter();
  159. }
  160. private:
  161. bool is_valid = true;
  162. };
  163. PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)