aboutsummaryrefslogtreecommitdiffstats
path: root/3rdparty/pybind11/include/pybind11/embed.h
diff options
context:
space:
mode:
Diffstat (limited to '3rdparty/pybind11/include/pybind11/embed.h')
-rw-r--r--3rdparty/pybind11/include/pybind11/embed.h180
1 files changed, 129 insertions, 51 deletions
diff --git a/3rdparty/pybind11/include/pybind11/embed.h b/3rdparty/pybind11/include/pybind11/embed.h
index 204aaf98..0ac609e0 100644
--- a/3rdparty/pybind11/include/pybind11/embed.h
+++ b/3rdparty/pybind11/include/pybind11/embed.h
@@ -12,23 +12,16 @@
#include "pybind11.h"
#include "eval.h"
+#include <memory>
+#include <vector>
+
#if defined(PYPY_VERSION)
-# error Embedding the interpreter is not supported with PyPy
+# error Embedding the interpreter is not supported with PyPy
#endif
-#if PY_MAJOR_VERSION >= 3
-# define PYBIND11_EMBEDDED_MODULE_IMPL(name) \
- extern "C" PyObject *pybind11_init_impl_##name(); \
- extern "C" PyObject *pybind11_init_impl_##name() { \
- return pybind11_init_wrapper_##name(); \
- }
-#else
-# define PYBIND11_EMBEDDED_MODULE_IMPL(name) \
- extern "C" void pybind11_init_impl_##name(); \
- extern "C" void pybind11_init_impl_##name() { \
- pybind11_init_wrapper_##name(); \
- }
-#endif
+#define PYBIND11_EMBEDDED_MODULE_IMPL(name) \
+ extern "C" PyObject *pybind11_init_impl_##name(); \
+ extern "C" PyObject *pybind11_init_impl_##name() { return pybind11_init_wrapper_##name(); }
/** \rst
Add a new module to the table of builtins for the interpreter. Must be
@@ -45,69 +38,143 @@
});
}
\endrst */
-#define PYBIND11_EMBEDDED_MODULE(name, variable) \
- static ::pybind11::module_::module_def \
- PYBIND11_CONCAT(pybind11_module_def_, name); \
- static void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &); \
- static PyObject PYBIND11_CONCAT(*pybind11_init_wrapper_, name)() { \
- auto m = ::pybind11::module_::create_extension_module( \
- PYBIND11_TOSTRING(name), nullptr, \
- &PYBIND11_CONCAT(pybind11_module_def_, name)); \
- try { \
- PYBIND11_CONCAT(pybind11_init_, name)(m); \
- return m.ptr(); \
- } PYBIND11_CATCH_INIT_EXCEPTIONS \
- } \
- PYBIND11_EMBEDDED_MODULE_IMPL(name) \
- ::pybind11::detail::embedded_module PYBIND11_CONCAT(pybind11_module_, name) \
- (PYBIND11_TOSTRING(name), \
- PYBIND11_CONCAT(pybind11_init_impl_, name)); \
- void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &variable)
-
+#define PYBIND11_EMBEDDED_MODULE(name, variable) \
+ static ::pybind11::module_::module_def PYBIND11_CONCAT(pybind11_module_def_, name); \
+ static void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ &); \
+ static PyObject PYBIND11_CONCAT(*pybind11_init_wrapper_, name)() { \
+ auto m = ::pybind11::module_::create_extension_module( \
+ PYBIND11_TOSTRING(name), nullptr, &PYBIND11_CONCAT(pybind11_module_def_, name)); \
+ try { \
+ PYBIND11_CONCAT(pybind11_init_, name)(m); \
+ return m.ptr(); \
+ } \
+ PYBIND11_CATCH_INIT_EXCEPTIONS \
+ } \
+ PYBIND11_EMBEDDED_MODULE_IMPL(name) \
+ ::pybind11::detail::embedded_module PYBIND11_CONCAT(pybind11_module_, name)( \
+ PYBIND11_TOSTRING(name), PYBIND11_CONCAT(pybind11_init_impl_, name)); \
+ void PYBIND11_CONCAT(pybind11_init_, name)(::pybind11::module_ \
+ & variable) // NOLINT(bugprone-macro-parentheses)
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
PYBIND11_NAMESPACE_BEGIN(detail)
/// Python 2.7/3.x compatible version of `PyImport_AppendInittab` and error checks.
struct embedded_module {
-#if PY_MAJOR_VERSION >= 3
- using init_t = PyObject *(*)();
-#else
- using init_t = void (*)();
-#endif
+ using init_t = PyObject *(*) ();
embedded_module(const char *name, init_t init) {
- if (Py_IsInitialized())
+ if (Py_IsInitialized() != 0) {
pybind11_fail("Can't add new modules after the interpreter has been initialized");
+ }
auto result = PyImport_AppendInittab(name, init);
- if (result == -1)
+ if (result == -1) {
pybind11_fail("Insufficient memory to add a new module");
+ }
}
};
+struct wide_char_arg_deleter {
+ void operator()(wchar_t *ptr) const {
+ // API docs: https://docs.python.org/3/c-api/sys.html#c.Py_DecodeLocale
+ PyMem_RawFree(ptr);
+ }
+};
+
+inline wchar_t *widen_chars(const char *safe_arg) {
+ wchar_t *widened_arg = Py_DecodeLocale(safe_arg, nullptr);
+ return widened_arg;
+}
+
PYBIND11_NAMESPACE_END(detail)
/** \rst
Initialize the Python interpreter. No other pybind11 or CPython API functions can be
called before this is done; with the exception of `PYBIND11_EMBEDDED_MODULE`. The
- optional parameter can be used to skip the registration of signal handlers (see the
- `Python documentation`_ for details). Calling this function again after the interpreter
- has already been initialized is a fatal error.
+ optional `init_signal_handlers` parameter can be used to skip the registration of
+ signal handlers (see the `Python documentation`_ for details). Calling this function
+ again after the interpreter has already been initialized is a fatal error.
If initializing the Python interpreter fails, then the program is terminated. (This
is controlled by the CPython runtime and is an exception to pybind11's normal behavior
of throwing exceptions on errors.)
+ The remaining optional parameters, `argc`, `argv`, and `add_program_dir_to_path` are
+ used to populate ``sys.argv`` and ``sys.path``.
+ See the |PySys_SetArgvEx documentation|_ for details.
+
.. _Python documentation: https://docs.python.org/3/c-api/init.html#c.Py_InitializeEx
+ .. |PySys_SetArgvEx documentation| replace:: ``PySys_SetArgvEx`` documentation
+ .. _PySys_SetArgvEx documentation: https://docs.python.org/3/c-api/init.html#c.PySys_SetArgvEx
\endrst */
-inline void initialize_interpreter(bool init_signal_handlers = true) {
- if (Py_IsInitialized())
+inline void initialize_interpreter(bool init_signal_handlers = true,
+ int argc = 0,
+ const char *const *argv = nullptr,
+ bool add_program_dir_to_path = true) {
+ if (Py_IsInitialized() != 0) {
pybind11_fail("The interpreter is already running");
+ }
+
+#if PY_VERSION_HEX < 0x030B0000
Py_InitializeEx(init_signal_handlers ? 1 : 0);
- // Make .py files in the working directory available by default
- module_::import("sys").attr("path").cast<list>().append(".");
+ // Before it was special-cased in python 3.8, passing an empty or null argv
+ // caused a segfault, so we have to reimplement the special case ourselves.
+ bool special_case = (argv == nullptr || argc <= 0);
+
+ const char *const empty_argv[]{"\0"};
+ const char *const *safe_argv = special_case ? empty_argv : argv;
+ if (special_case) {
+ argc = 1;
+ }
+
+ auto argv_size = static_cast<size_t>(argc);
+ // SetArgv* on python 3 takes wchar_t, so we have to convert.
+ std::unique_ptr<wchar_t *[]> widened_argv(new wchar_t *[argv_size]);
+ std::vector<std::unique_ptr<wchar_t[], detail::wide_char_arg_deleter>> widened_argv_entries;
+ widened_argv_entries.reserve(argv_size);
+ for (size_t ii = 0; ii < argv_size; ++ii) {
+ widened_argv_entries.emplace_back(detail::widen_chars(safe_argv[ii]));
+ if (!widened_argv_entries.back()) {
+ // A null here indicates a character-encoding failure or the python
+ // interpreter out of memory. Give up.
+ return;
+ }
+ widened_argv[ii] = widened_argv_entries.back().get();
+ }
+
+ auto *pysys_argv = widened_argv.get();
+
+ PySys_SetArgvEx(argc, pysys_argv, static_cast<int>(add_program_dir_to_path));
+#else
+ PyConfig config;
+ PyConfig_InitIsolatedConfig(&config);
+ config.isolated = 0;
+ config.use_environment = 1;
+ config.install_signal_handlers = init_signal_handlers ? 1 : 0;
+
+ PyStatus status = PyConfig_SetBytesArgv(&config, argc, const_cast<char *const *>(argv));
+ if (PyStatus_Exception(status)) {
+ // A failure here indicates a character-encoding failure or the python
+ // interpreter out of memory. Give up.
+ PyConfig_Clear(&config);
+ throw std::runtime_error(PyStatus_IsError(status) ? status.err_msg
+ : "Failed to prepare CPython");
+ }
+ status = Py_InitializeFromConfig(&config);
+ PyConfig_Clear(&config);
+ if (PyStatus_Exception(status)) {
+ throw std::runtime_error(PyStatus_IsError(status) ? status.err_msg
+ : "Failed to init CPython");
+ }
+ if (add_program_dir_to_path) {
+ PyRun_SimpleString("import sys, os.path; "
+ "sys.path.insert(0, "
+ "os.path.abspath(os.path.dirname(sys.argv[0])) "
+ "if sys.argv and os.path.exists(sys.argv[0]) else '')");
+ }
+#endif
}
/** \rst
@@ -154,8 +221,13 @@ inline void finalize_interpreter() {
// during destruction), so we get the pointer-pointer here and check it after Py_Finalize().
detail::internals **internals_ptr_ptr = detail::get_internals_pp();
// It could also be stashed in builtins, so look there too:
- if (builtins.contains(id) && isinstance<capsule>(builtins[id]))
+ if (builtins.contains(id) && isinstance<capsule>(builtins[id])) {
internals_ptr_ptr = capsule(builtins[id]);
+ }
+ // Local internals contains data managed by the current interpreter, so we must clear them to
+ // avoid undefined behaviors when initializing another interpreter
+ detail::get_local_internals().registered_types_cpp.clear();
+ detail::get_local_internals().registered_exception_translators.clear();
Py_Finalize();
@@ -169,6 +241,8 @@ inline void finalize_interpreter() {
Scope guard version of `initialize_interpreter` and `finalize_interpreter`.
This a move-only guard and only a single instance can exist.
+ See `initialize_interpreter` for a discussion of its constructor arguments.
+
.. code-block:: cpp
#include <pybind11/embed.h>
@@ -180,8 +254,11 @@ inline void finalize_interpreter() {
\endrst */
class scoped_interpreter {
public:
- scoped_interpreter(bool init_signal_handlers = true) {
- initialize_interpreter(init_signal_handlers);
+ explicit scoped_interpreter(bool init_signal_handlers = true,
+ int argc = 0,
+ const char *const *argv = nullptr,
+ bool add_program_dir_to_path = true) {
+ initialize_interpreter(init_signal_handlers, argc, argv, add_program_dir_to_path);
}
scoped_interpreter(const scoped_interpreter &) = delete;
@@ -190,8 +267,9 @@ public:
scoped_interpreter &operator=(scoped_interpreter &&) = delete;
~scoped_interpreter() {
- if (is_valid)
+ if (is_valid) {
finalize_interpreter();
+ }
}
private: