Warning
Please be advised that the reference documentation discussing pybind11 internals is currently incomplete. Please refer to the previous sections and the pybind11 header files for the nitty gritty details.
Reference¶
Macros¶
-
PYBIND11_MODULE
(name, variable)¶ This macro creates the entry point that will be invoked when the Python interpreter imports an extension module. The module name is given as the fist argument and it should not be in quotes. The second macro argument defines a variable of type
py::module
which can be used to initialize the module.PYBIND11_MODULE(example, m) { m.doc() = "pybind11 example module"; // Add bindings here m.def("foo", []() { return "Hello, World!"; }); }
Convenience classes for arbitrary Python types¶
Common member functions¶
- template <typename Derived>
-
class
object_api
¶ A mixin class which adds common functions to
handle
,object
and various accessors. The only requirement forDerived
is to implementPyObject *Derived::ptr() const
.Inherits from pyobject_tag
Public Functions
-
iterator
begin
() const¶ Return an iterator equivalent to calling
iter()
in Python. The object must be a collection which supports the iteration protocol.
-
item_accessor
operator[]
(handle key) const¶ Return an internal functor to invoke the object’s sequence protocol. Casting the returned
detail::item_accessor
instance to ahandle
orobject
subclass causes a corresponding call to__getitem__
. Assigning ahandle
orobject
subclass causes a call to__setitem__
.
-
item_accessor
operator[]
(const char *key) const¶ See above (the only difference is that they key is provided as a string literal)
-
obj_attr_accessor
attr
(handle key) const¶ Return an internal functor to access the object’s attributes. Casting the returned
detail::obj_attr_accessor
instance to ahandle
orobject
subclass causes a corresponding call togetattr
. Assigning ahandle
orobject
subclass causes a call tosetattr
.
-
str_attr_accessor
attr
(const char *key) const¶ See above (the only difference is that they key is provided as a string literal)
-
args_proxy
operator*
() const¶ Matches * unpacking in Python, e.g. to unpack arguments out of a
tuple
orlist
for a function call. Applying another * to the result yields ** unpacking, e.g. to unpack a dict as function keyword arguments. See Calling Python functions.
- template <typename T>
-
bool
contains
(T &&item) const¶ Check if the given item is contained within this object, i.e.
item in obj
.
- template <return_value_policy policy = return_value_policy::automatic_reference, typename… Args>
-
object
operator()
(Args&&... args) const¶ Assuming the Python object is a function or implements the
__call__
protocol,operator()
invokes the underlying function, passing an arbitrary set of parameters. The result is returned as aobject
and may need to be converted back into a Python object usinghandle::cast()
.When some of the arguments cannot be converted to Python objects, the function will throw a
cast_error
exception. When the Python function call fails, aerror_already_set
exception is thrown.
-
bool
is
(object_api const &other) const¶ Equivalent to
obj is other
in Python.
-
bool
is_none
() const¶ Equivalent to
obj is None
in Python.
-
bool
equal
(object_api const &other) const¶ Equivalent to obj == other in Python.
-
str_attr_accessor
doc
() const¶ Get or set the object’s docstring, i.e.
obj.__doc__
.
-
int
ref_count
() const¶ Return the object’s current reference count.
-
iterator
Without reference counting¶
-
class
handle
¶ Holds a reference to a Python object (no reference counting)
The
handle
class is a thin wrapper around an arbitrary Python object (i.e. aPyObject *
in Python’s C API). It does not perform any automatic reference counting and merely provides a basic C++ interface to various Python API functions.Inherits from detail::object_api< handle >
Subclassed by args_proxy, kwargs_proxy, object
Public Functions
-
handle
()¶ The default constructor creates a handle with a
nullptr
-valued pointer.
-
handle
(PyObject *ptr)¶ Creates a
handle
from the given raw Python object pointer.
-
PyObject *
ptr
() const¶ Return the underlying
PyObject *
pointer.
-
const handle &
inc_ref
() const¶ Manually increase the reference count of the Python object. Usually, it is preferable to use the
object
class which derives fromhandle
and calls this function automatically. Returns a reference to itself.
-
const handle &
dec_ref
() const¶ Manually decrease the reference count of the Python object. Usually, it is preferable to use the
object
class which derives fromhandle
and calls this function automatically. Returns a reference to itself.
- template <typename T>
-
T
cast
() const¶ Attempt to cast the Python object into the given C++ type. A
cast_error
will be throw upon failure.
-
operator bool
() const¶ Return
true
when thehandle
wraps a valid Python object.
-
With reference counting¶
-
class
object
¶ Holds a reference to a Python object (with reference counting)
Like
handle
, theobject
class is a thin wrapper around an arbitrary Python object (i.e. aPyObject *
in Python’s C API). In contrast tohandle
, it optionally increases the object’s reference count upon construction, and it always decreases the reference count when theobject
instance goes out of scope and is destructed. When usingobject
instances consistently, it is much easier to get reference counting right at the first attempt.Inherits from handle
Subclassed by bool_, buffer, bytes, capsule, dict, dtype, exception< type >, float_, function, generic_type, int_, iterable, iterator, list, memoryview, module, none, sequence, set, slice, str, tuple, weakref
Public Functions
-
object
(object &&other)¶ Move constructor; steals the object from
other
and preserves its reference count.
-
~object
()¶ Destructor; automatically calls
handle::dec_ref()
-
- template <typename T>
-
T
reinterpret_borrow
(handle h)¶ Declare that a
handle
orPyObject *
is a certain type and borrow the reference. The target typeT
must beobject
or one of its derived classes. The function doesn’t do any conversions or checks. It’s up to the user to make sure that the target type is correct.PyObject *p = PyList_GetItem(obj, index); py::object o = reinterpret_borrow<py::object>(p); // or py::tuple t = reinterpret_borrow<py::tuple>(p); // <-- `p` must be already be a `tuple`
- template <typename T>
-
T
reinterpret_steal
(handle h)¶ Like
reinterpret_borrow()
, but steals the reference.PyObject *p = PyObject_Str(obj); py::str s = reinterpret_steal<py::str>(p); // <-- `p` must be already be a `str`
Convenience classes for specific Python types¶
-
class
module
¶ Wrapper for Python extension modules.
Inherits from object
Public Functions
-
module
(const char *name, const char *doc = nullptr)¶ Create a new top-level Python module with the given name and docstring.
- template <typename Func, typename… Extra>
-
module &
def
(const char *name_, Func &&f, const Extra&... extra)¶ Create Python binding for a new function within the module scope.
Func
can be a plain C++ function, a function pointer, or a lambda function. For details on theExtra&& ... extra
argument, see section Passing extra arguments to def or class_.
-
module
def_submodule
(const char *name, const char *doc = nullptr)¶ Create and return a new Python submodule with the given name and docstring. This also works recursively, i.e.
py::module m("example", "pybind11 example plugin"); py::module m2 = m.def_submodule("sub", "A submodule of 'example'"); py::module m3 = m2.def_submodule("subsub", "A submodule of 'example.sub'");
-
void
reload
()¶ Reload the module or throws
error_already_set
.
Public Static Functions
-
static module
import
(const char *name)¶ Import and return a module or throws
error_already_set
.
-
-
group
pytypes
Functions
- template <typename Unsigned>
-
Unsigned
as_unsigned
(PyObject *o)¶
-
bytes
(const pybind11::str &s)¶
-
class
iterator
¶ - #include <pytypes.h>
Wraps a Python iterator so that it can also be used as a C++ input iterator
Caveat: copying an iterator does not (and cannot) clone the internal state of the Python iterable. This also applies to the post-increment operator. This iterator should only be used to retrieve the current value using
operator*()
.Inherits from object
Passing extra arguments to def
or class_
¶
-
group
annotations
-
struct
is_method
¶ - #include <attr.h>
Annotation for methods.
-
struct
is_operator
¶ - #include <attr.h>
Annotation for operators.
-
struct
scope
¶ - #include <attr.h>
Annotation for parent scope.
-
struct
doc
¶ - #include <attr.h>
Annotation for documentation.
-
struct
name
¶ - #include <attr.h>
Annotation for function names.
-
struct
sibling
¶ - #include <attr.h>
Annotation indicating that a function is an overload associated with a given “sibling”.
- template <typename T>
-
struct
base
¶ - #include <attr.h>
Annotation indicating that a class derives from another given type.
- template <size_t Nurse, size_t Patient>
-
struct
keep_alive
¶ - #include <attr.h>
Keep patient alive while nurse lives.
-
struct
multiple_inheritance
¶ - #include <attr.h>
Annotation indicating that a class is involved in a multiple inheritance relationship.
-
struct
dynamic_attr
¶ - #include <attr.h>
Annotation which enables dynamic attributes, i.e. adds
__dict__
to a class.
-
struct
buffer_protocol
¶ - #include <attr.h>
Annotation which enables the buffer protocol for a type.
-
struct
metaclass
¶ - #include <attr.h>
Annotation which requests that a special metaclass is created for a type.
-
struct
module_local
¶ - #include <attr.h>
Annotation that marks a class as local to the module:
-
struct
arithmetic
¶ - #include <attr.h>
Annotation to mark enums as an arithmetic type.
- template <typename… Ts>
-
struct
call_guard
¶ - #include <attr.h>
A call policy which places one or more guard variables (
Ts...
) around the function call.For example, this definition:
m.def("foo", foo, py::call_guard<T>());
is equivalent to the following pseudocode:
m.def("foo", [](args...) { T scope_guard; return foo(args...); // forwarded arguments });
-
struct
arg_v
¶ - #include <cast.h>
Annotation for arguments with values
Inherits from arg
Public Functions
- template <typename T>
-
arg_v
(const char *name, T &&x, const char *descr = nullptr)¶ Direct construction with name, default, and description.
- template <typename T>
-
arg_v
(const arg &base, T &&x, const char *descr = nullptr)¶ Called internally when invoking
py::arg("a") = value
-
arg_v &
noconvert
(bool flag = true)¶ Same as
arg::noconvert()
, but returns *this as arg_v&, not arg&.
-
struct
Embedding the interpreter¶
-
PYBIND11_EMBEDDED_MODULE
(name, variable)¶ 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.
PYBIND11_EMBEDDED_MODULE(example, m) { // ... initialize functions and classes here m.def("foo", []() { return "Hello, World!"; }); }
-
void
initialize_interpreter
(bool init_signal_handlers = true)¶ 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.)
-
void
finalize_interpreter
()¶ 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:
{ // 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.
-
class
scoped_interpreter
¶ Scope guard version of
initialize_interpreter()
andfinalize_interpreter()
. This a move-only guard and only a single instance can exist.#include <pybind11/embed.h> int main() { py::scoped_interpreter guard{}; py::print(Hello, World!); } // <-- interpreter shutdown
Redirecting C++ streams¶
-
class
scoped_ostream_redirect
¶ This a move-only guard that redirects output.
#include <pybind11/iostream.h> ... { py::scoped_ostream_redirect output; std::cout << "Hello, World!"; // Python stdout } // <-- return std::cout to normal
You can explicitly pass the c++ stream and the python object, for example to guard stderr instead.
{ py::scoped_ostream_redirect output{std::cerr, py::module::import("sys").attr("stderr")}; std::cerr << "Hello, World!"; }
Subclassed by scoped_estream_redirect
-
class
scoped_estream_redirect
¶ Like
scoped_ostream_redirect
, but redirects cerr by default. This class is provided primary to makepy::call_guard
easier to make.m.def("noisy_func", &noisy_func, py::call_guard<scoped_ostream_redirect, scoped_estream_redirect>());
Inherits from scoped_ostream_redirect
-
class_<detail::OstreamRedirect>
add_ostream_redirect
(module m, std::string name = "ostream_redirect")¶ This is a helper function to add a C++ redirect context manager to Python instead of using a C++ guard. To use it, add the following to your binding code:
#include <pybind11/iostream.h> ... py::add_ostream_redirect(m, "ostream_redirect");
You now have a Python context manager that redirects your output:
with m.ostream_redirect(): m.print_to_cout_function()
This manager can optionally be told which streams to operate on:
with m.ostream_redirect(stdout=true, stderr=true): m.noisy_function_with_error_printing()
Python built-in functions¶
-
group
python_builtins
Unless stated otherwise, the following C++ functions behave the same as their Python counterparts.
Functions
-
dict
globals
()¶ Return a dictionary representing the global variables in the current execution frame, or
__main__.__dict__
if there is no frame (usually when the interpreter is embedded).
-
dict
Exceptions¶
-
class
error_already_set
¶ Fetch and hold an error which was already set in Python. An instance of this is typically thrown to propagate python-side errors back through C++ which can either be caught manually or else falls back to the function dispatcher (which then raises the captured error back to python).
Inherits from runtime_error
Public Functions
-
error_already_set
()¶ Constructs a new exception from the current Python error indicator, if any. The current Python error indicator will be cleared.
-
void
restore
()¶ Give the currently-held error back to Python, if any. If there is currently a Python error already set it is cleared first. After this call, the current object no longer stores the error variables (but the
.what()
string is still available).
-