File indexing completed on 2025-02-16 05:12:15

0001 /*
0002     pybind11/embed.h: Support for embedding the interpreter
0003 
0004     Copyright (c) 2017 Wenzel Jakob <wenzel.jakob@epfl.ch>
0005 
0006     All rights reserved. Use of this source code is governed by a
0007     BSD-style license that can be found in the LICENSE file.
0008 */
0009 
0010 #pragma once
0011 
0012 #include "pybind11.h"
0013 #include "eval.h"
0014 
0015 #include <memory>
0016 #include <vector>
0017 
0018 #if defined(PYPY_VERSION)
0019 #  error Embedding the interpreter is not supported with PyPy
0020 #endif
0021 
0022 #if PY_MAJOR_VERSION >= 3
0023 #  define PYBIND11_EMBEDDED_MODULE_IMPL(name)            \
0024       extern "C" PyObject *pybind11_init_impl_##name();  \
0025       extern "C" PyObject *pybind11_init_impl_##name() { \
0026           return pybind11_init_wrapper_##name();         \
0027       }
0028 #else
0029 #  define PYBIND11_EMBEDDED_MODULE_IMPL(name)            \
0030       extern "C" void pybind11_init_impl_##name();       \
0031       extern "C" void pybind11_init_impl_##name() {      \
0032           pybind11_init_wrapper_##name();                \
0033       }
0034 #endif
0035 
0036 /** \rst
0037     Add a new module to the table of builtins for the interpreter. Must be
0038     defined in global scope. The first macro parameter is the name of the
0039     module (without quotes). The second parameter is the variable which will
0040     be used as the interface to add functions and classes to the module.
0041 
0042     .. code-block:: cpp
0043 
0044         PYBIND11_EMBEDDED_MODULE(example, m) {
0045             // ... initialize functions and classes here
0046             m.def("foo", []() {
0047                 return "Hello, World!";
0048             });
0049         }
0050  \endrst */
0051 #define PYBIND11_EMBEDDED_MODULE(name, variable)                                                  \
0052     static ::pybind11::module_::module_def PYBIND11_CONCAT(pybind11_module_def_, name);           \
0053     static void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &);                     \
0054     static PyObject PYBIND11_CONCAT(*pybind11_init_wrapper_, name)() {                            \
0055         auto m = ::pybind11::module_::create_extension_module(                                    \
0056             PYBIND11_TOSTRING(name), nullptr, &PYBIND11_CONCAT(pybind11_module_def_, name));      \
0057         try {                                                                                     \
0058             PYBIND11_CONCAT(pybind11_init_, name)(m);                                             \
0059             return m.ptr();                                                                       \
0060         }                                                                                         \
0061         PYBIND11_CATCH_INIT_EXCEPTIONS                                                            \
0062     }                                                                                             \
0063     PYBIND11_EMBEDDED_MODULE_IMPL(name)                                                           \
0064     ::pybind11::detail::embedded_module PYBIND11_CONCAT(pybind11_module_, name)(                  \
0065         PYBIND11_TOSTRING(name), PYBIND11_CONCAT(pybind11_init_impl_, name));                     \
0066     void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_                                \
0067                                                & variable) // NOLINT(bugprone-macro-parentheses)
0068 
0069 PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
0070 PYBIND11_NAMESPACE_BEGIN(detail)
0071 
0072 /// Python 2.7/3.x compatible version of `PyImport_AppendInittab` and error checks.
0073 struct embedded_module {
0074 #if PY_MAJOR_VERSION >= 3
0075     using init_t = PyObject *(*)();
0076 #else
0077     using init_t = void (*)();
0078 #endif
0079     embedded_module(const char *name, init_t init) {
0080         if (Py_IsInitialized() != 0)
0081             pybind11_fail("Can't add new modules after the interpreter has been initialized");
0082 
0083         auto result = PyImport_AppendInittab(name, init);
0084         if (result == -1)
0085             pybind11_fail("Insufficient memory to add a new module");
0086     }
0087 };
0088 
0089 struct wide_char_arg_deleter {
0090     void operator()(wchar_t *ptr) const {
0091 #if PY_VERSION_HEX >= 0x030500f0
0092         // API docs: https://docs.python.org/3/c-api/sys.html#c.Py_DecodeLocale
0093         PyMem_RawFree(ptr);
0094 #else
0095         delete[] ptr;
0096 #endif
0097     }
0098 };
0099 
0100 inline wchar_t *widen_chars(const char *safe_arg) {
0101 #if PY_VERSION_HEX >= 0x030500f0
0102     wchar_t *widened_arg = Py_DecodeLocale(safe_arg, nullptr);
0103 #else
0104     wchar_t *widened_arg = nullptr;
0105 
0106 // warning C4996: 'mbstowcs': This function or variable may be unsafe.
0107 #if defined(_MSC_VER)
0108 #pragma warning(push)
0109 #pragma warning(disable:4996)
0110 #endif
0111 
0112 #    if defined(HAVE_BROKEN_MBSTOWCS) && HAVE_BROKEN_MBSTOWCS
0113     size_t count = std::strlen(safe_arg);
0114 #    else
0115     size_t count = std::mbstowcs(nullptr, safe_arg, 0);
0116 #    endif
0117     if (count != static_cast<size_t>(-1)) {
0118         widened_arg = new wchar_t[count + 1];
0119         std::mbstowcs(widened_arg, safe_arg, count + 1);
0120     }
0121 
0122 #if defined(_MSC_VER)
0123 #pragma warning(pop)
0124 #endif
0125 
0126 #endif
0127     return widened_arg;
0128 }
0129 
0130 /// Python 2.x/3.x-compatible version of `PySys_SetArgv`
0131 inline void set_interpreter_argv(int argc, const char *const *argv, bool add_program_dir_to_path) {
0132     // Before it was special-cased in python 3.8, passing an empty or null argv
0133     // caused a segfault, so we have to reimplement the special case ourselves.
0134     bool special_case = (argv == nullptr || argc <= 0);
0135 
0136     const char *const empty_argv[]{"\0"};
0137     const char *const *safe_argv = special_case ? empty_argv : argv;
0138     if (special_case)
0139         argc = 1;
0140 
0141     auto argv_size = static_cast<size_t>(argc);
0142 #if PY_MAJOR_VERSION >= 3
0143     // SetArgv* on python 3 takes wchar_t, so we have to convert.
0144     std::unique_ptr<wchar_t *[]> widened_argv(new wchar_t *[argv_size]);
0145     std::vector<std::unique_ptr<wchar_t[], wide_char_arg_deleter>> widened_argv_entries;
0146     widened_argv_entries.reserve(argv_size);
0147     for (size_t ii = 0; ii < argv_size; ++ii) {
0148         widened_argv_entries.emplace_back(widen_chars(safe_argv[ii]));
0149         if (!widened_argv_entries.back()) {
0150             // A null here indicates a character-encoding failure or the python
0151             // interpreter out of memory. Give up.
0152             return;
0153         }
0154         widened_argv[ii] = widened_argv_entries.back().get();
0155     }
0156 
0157     auto pysys_argv = widened_argv.get();
0158 #else
0159     // python 2.x
0160     std::vector<std::string> strings{safe_argv, safe_argv + argv_size};
0161     std::vector<char *> char_strings{argv_size};
0162     for (std::size_t i = 0; i < argv_size; ++i)
0163         char_strings[i] = &strings[i][0];
0164     char **pysys_argv = char_strings.data();
0165 #endif
0166 
0167     PySys_SetArgvEx(argc, pysys_argv, static_cast<int>(add_program_dir_to_path));
0168 }
0169 
0170 PYBIND11_NAMESPACE_END(detail)
0171 
0172 /** \rst
0173     Initialize the Python interpreter. No other pybind11 or CPython API functions can be
0174     called before this is done; with the exception of `PYBIND11_EMBEDDED_MODULE`. The
0175     optional `init_signal_handlers` parameter can be used to skip the registration of
0176     signal handlers (see the `Python documentation`_ for details). Calling this function
0177     again after the interpreter has already been initialized is a fatal error.
0178 
0179     If initializing the Python interpreter fails, then the program is terminated.  (This
0180     is controlled by the CPython runtime and is an exception to pybind11's normal behavior
0181     of throwing exceptions on errors.)
0182 
0183     The remaining optional parameters, `argc`, `argv`, and `add_program_dir_to_path` are
0184     used to populate ``sys.argv`` and ``sys.path``.
0185     See the |PySys_SetArgvEx documentation|_ for details.
0186 
0187     .. _Python documentation: https://docs.python.org/3/c-api/init.html#c.Py_InitializeEx
0188     .. |PySys_SetArgvEx documentation| replace:: ``PySys_SetArgvEx`` documentation
0189     .. _PySys_SetArgvEx documentation: https://docs.python.org/3/c-api/init.html#c.PySys_SetArgvEx
0190  \endrst */
0191 inline void initialize_interpreter(bool init_signal_handlers = true,
0192                                    int argc = 0,
0193                                    const char *const *argv = nullptr,
0194                                    bool add_program_dir_to_path = true) {
0195     if (Py_IsInitialized() != 0)
0196         pybind11_fail("The interpreter is already running");
0197 
0198     Py_InitializeEx(init_signal_handlers ? 1 : 0);
0199 
0200     detail::set_interpreter_argv(argc, argv, add_program_dir_to_path);
0201 }
0202 
0203 /** \rst
0204     Shut down the Python interpreter. No pybind11 or CPython API functions can be called
0205     after this. In addition, pybind11 objects must not outlive the interpreter:
0206 
0207     .. code-block:: cpp
0208 
0209         { // BAD
0210             py::initialize_interpreter();
0211             auto hello = py::str("Hello, World!");
0212             py::finalize_interpreter();
0213         } // <-- BOOM, hello's destructor is called after interpreter shutdown
0214 
0215         { // GOOD
0216             py::initialize_interpreter();
0217             { // scoped
0218                 auto hello = py::str("Hello, World!");
0219             } // <-- OK, hello is cleaned up properly
0220             py::finalize_interpreter();
0221         }
0222 
0223         { // BETTER
0224             py::scoped_interpreter guard{};
0225             auto hello = py::str("Hello, World!");
0226         }
0227 
0228     .. warning::
0229 
0230         The interpreter can be restarted by calling `initialize_interpreter` again.
0231         Modules created using pybind11 can be safely re-initialized. However, Python
0232         itself cannot completely unload binary extension modules and there are several
0233         caveats with regard to interpreter restarting. All the details can be found
0234         in the CPython documentation. In short, not all interpreter memory may be
0235         freed, either due to reference cycles or user-created global data.
0236 
0237  \endrst */
0238 inline void finalize_interpreter() {
0239     handle builtins(PyEval_GetBuiltins());
0240     const char *id = PYBIND11_INTERNALS_ID;
0241 
0242     // Get the internals pointer (without creating it if it doesn't exist).  It's possible for the
0243     // internals to be created during Py_Finalize() (e.g. if a py::capsule calls `get_internals()`
0244     // during destruction), so we get the pointer-pointer here and check it after Py_Finalize().
0245     detail::internals **internals_ptr_ptr = detail::get_internals_pp();
0246     // It could also be stashed in builtins, so look there too:
0247     if (builtins.contains(id) && isinstance<capsule>(builtins[id]))
0248         internals_ptr_ptr = capsule(builtins[id]);
0249 
0250     Py_Finalize();
0251 
0252     if (internals_ptr_ptr) {
0253         delete *internals_ptr_ptr;
0254         *internals_ptr_ptr = nullptr;
0255     }
0256 }
0257 
0258 /** \rst
0259     Scope guard version of `initialize_interpreter` and `finalize_interpreter`.
0260     This a move-only guard and only a single instance can exist.
0261 
0262     See `initialize_interpreter` for a discussion of its constructor arguments.
0263 
0264     .. code-block:: cpp
0265 
0266         #include <pybind11/embed.h>
0267 
0268         int main() {
0269             py::scoped_interpreter guard{};
0270             py::print(Hello, World!);
0271         } // <-- interpreter shutdown
0272  \endrst */
0273 class scoped_interpreter {
0274 public:
0275     explicit scoped_interpreter(bool init_signal_handlers = true,
0276                                 int argc = 0,
0277                                 const char *const *argv = nullptr,
0278                                 bool add_program_dir_to_path = true) {
0279         initialize_interpreter(init_signal_handlers, argc, argv, add_program_dir_to_path);
0280     }
0281 
0282     scoped_interpreter(const scoped_interpreter &) = delete;
0283     scoped_interpreter(scoped_interpreter &&other) noexcept { other.is_valid = false; }
0284     scoped_interpreter &operator=(const scoped_interpreter &) = delete;
0285     scoped_interpreter &operator=(scoped_interpreter &&) = delete;
0286 
0287     ~scoped_interpreter() {
0288         if (is_valid)
0289             finalize_interpreter();
0290     }
0291 
0292 private:
0293     bool is_valid = true;
0294 };
0295 
0296 PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)