μHAL (v2.8.17)
Part of the IPbus software repository
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Classes | Macros | Functions
embed.h File Reference
#include "pybind11.h"
#include "eval.h"
#include <memory>
#include <vector>
Include dependency graph for embed.h:

Go to the source code of this file.

Classes

struct  embedded_module
 Python 2.7/3.x compatible version of PyImport_AppendInittab and error checks. More...
 
struct  wide_char_arg_deleter
 
class  scoped_interpreter
 \rst Scope guard version of initialize_interpreter and finalize_interpreter. More...
 

Macros

#define PYBIND11_EMBEDDED_MODULE_IMPL(name)
 
#define PYBIND11_EMBEDDED_MODULE(name, variable)
 \rst Add a new module to the table of builtins for the interpreter. More...
 

Functions

wchar_t * widen_chars (const char *safe_arg)
 
void set_interpreter_argv (int argc, const char *const *argv, bool add_program_dir_to_path)
 Python 2.x/3.x-compatible version of PySys_SetArgv More...
 
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. More...
 
void finalize_interpreter ()
 \rst Shut down the Python interpreter. More...
 

Macro Definition Documentation

◆ PYBIND11_EMBEDDED_MODULE

#define PYBIND11_EMBEDDED_MODULE (   name,
  variable 
)
Value:
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_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
Variable width.
#define PYBIND11_TOSTRING(x)
Definition: common.h:344
#define PYBIND11_CATCH_INIT_EXCEPTIONS
Definition: common.h:379
#define PYBIND11_CONCAT(first, second)
Definition: common.h:345
Annotation for function names.
Definition: attr.h:47

\rst Add a new module to the table of builtins for the interpreter.

Must be defined in global scope. The first macro parameter is the name of the module (without quotes). The second parameter is the variable which will be used as the interface to add functions and classes to the module.

.. code-block:: cpp

PYBIND11_EMBEDDED_MODULE(example, m) {

... initialize functions and classes here m.def("foo", []() { return "Hello, World!"; }); } \endrst

Definition at line 47 of file embed.h.

◆ PYBIND11_EMBEDDED_MODULE_IMPL

#define PYBIND11_EMBEDDED_MODULE_IMPL (   name)
Value:
extern "C" void pybind11_init_impl_##name(); \
extern "C" void pybind11_init_impl_##name() { pybind11_init_wrapper_##name(); }

Definition at line 27 of file embed.h.

Function Documentation

◆ finalize_interpreter()

void finalize_interpreter ( )
inline

\rst Shut down the Python interpreter.

No pybind11 or CPython API functions can be called after this. In addition, pybind11 objects must not outlive the interpreter:

.. code-block:: cpp

{ // BAD
    py::initialize_interpreter();
    auto hello = py::str("Hello, World!");
    py::finalize_interpreter();
} // <-- BOOM, hello's destructor is called after interpreter shutdown

{ // GOOD
    py::initialize_interpreter();
    { // scoped
        auto hello = py::str("Hello, World!");
    } // <-- OK, hello is cleaned up properly
    py::finalize_interpreter();
}

{ // BETTER
    py::scoped_interpreter guard{};
    auto hello = py::str("Hello, World!");
}

.. warning::

The interpreter can be restarted by calling `initialize_interpreter` again.
Modules created using pybind11 can be safely re-initialized. However, Python
itself cannot completely unload binary extension modules and there are several
caveats with regard to interpreter restarting. All the details can be found
in the CPython documentation. In short, not all interpreter memory may be
freed, either due to reference cycles or user-created global data.

\endrst

Definition at line 238 of file embed.h.

References PYBIND11_INTERNALS_ID.

◆ initialize_interpreter()

void initialize_interpreter ( bool  init_signal_handlers = true,
int  argc = 0,
const char *const *  argv = nullptr,
bool  add_program_dir_to_path = true 
)
inline

\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 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

Definition at line 190 of file embed.h.

References pybind11_fail().

Referenced by initialize_interpreter().

◆ set_interpreter_argv()

void set_interpreter_argv ( int  argc,
const char *const *  argv,
bool  add_program_dir_to_path 
)
inline

Python 2.x/3.x-compatible version of PySys_SetArgv

Definition at line 129 of file embed.h.

References widen_chars().

◆ widen_chars()

wchar_t * widen_chars ( const char *  safe_arg)
inline

Definition at line 98 of file embed.h.

Referenced by set_interpreter_argv().