Classes | Macros | Functions
embed.h File Reference
#include "pybind11.h"
#include "eval.h"
Include dependency graph for embed.h:
This graph shows which files directly or indirectly include this file:

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...
 
class  scoped_interpreter
 

Macros

#define PYBIND11_EMBEDDED_MODULE(name, variable)
 
#define PYBIND11_EMBEDDED_MODULE_IMPL(name)
 

Functions

void finalize_interpreter ()
 
void initialize_interpreter (bool init_signal_handlers=true)
 

Macro Definition Documentation

#define PYBIND11_EMBEDDED_MODULE (   name,
  variable 
)
Value:
static void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &); \
static PyObject PYBIND11_CONCAT(*pybind11_init_wrapper_, name)() { \
try { \
PYBIND11_CONCAT(pybind11_init_, name)(m); \
return m.ptr(); \
} catch (pybind11::error_already_set &e) { \
PyErr_SetString(PyExc_ImportError, e.what()); \
return nullptr; \
} catch (const std::exception &e) { \
PyErr_SetString(PyExc_ImportError, e.what()); \
return nullptr; \
} \
} \
pybind11::detail::embedded_module PYBIND11_CONCAT(pybind11_module_, name) \
PYBIND11_CONCAT(pybind11_init_impl_, name)); \
void PYBIND11_CONCAT(pybind11_init_, name)(pybind11::module &variable)
Matrix3f m
module_ module
Definition: pybind11.h:943
if n return
#define PYBIND11_EMBEDDED_MODULE_IMPL(name)
Definition: embed.h:26
Array< double, 1, 3 > e(1./3., 0.5, 2.)
#define PYBIND11_CONCAT(first, second)
Annotation for function names.
Definition: attr.h:36

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!"; }); }

Definition at line 48 of file embed.h.

#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 26 of file embed.h.

Function Documentation

void finalize_interpreter ( )
inline

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.

Definition at line 150 of file embed.h.

void initialize_interpreter ( bool  init_signal_handlers = true)
inline

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.

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.)

.. _Python documentation: https://docs.python.org/3/c-api/init.html#c.Py_InitializeEx

Definition at line 105 of file embed.h.



gtsam
Author(s):
autogenerated on Sat May 8 2021 02:51:29