reference.rst revision 11986
111986Sandreas.sandberg@arm.com.. _reference:
211986Sandreas.sandberg@arm.com
311986Sandreas.sandberg@arm.com.. warning::
411986Sandreas.sandberg@arm.com
511986Sandreas.sandberg@arm.com    Please be advised that the reference documentation discussing pybind11
611986Sandreas.sandberg@arm.com    internals is currently incomplete. Please refer to the previous sections
711986Sandreas.sandberg@arm.com    and the pybind11 header files for the nitty gritty details.
811986Sandreas.sandberg@arm.com
911986Sandreas.sandberg@arm.comReference
1011986Sandreas.sandberg@arm.com#########
1111986Sandreas.sandberg@arm.com
1211986Sandreas.sandberg@arm.comMacros
1311986Sandreas.sandberg@arm.com======
1411986Sandreas.sandberg@arm.com
1511986Sandreas.sandberg@arm.com.. function:: PYBIND11_PLUGIN(const char *name)
1611986Sandreas.sandberg@arm.com
1711986Sandreas.sandberg@arm.com    This macro creates the entry point that will be invoked when the Python
1811986Sandreas.sandberg@arm.com    interpreter imports a plugin library. Please create a
1911986Sandreas.sandberg@arm.com    :class:`module` in the function body and return the pointer to its
2011986Sandreas.sandberg@arm.com    underlying Python object at the end.
2111986Sandreas.sandberg@arm.com
2211986Sandreas.sandberg@arm.com    .. code-block:: cpp
2311986Sandreas.sandberg@arm.com
2411986Sandreas.sandberg@arm.com        PYBIND11_PLUGIN(example) {
2511986Sandreas.sandberg@arm.com            pybind11::module m("example", "pybind11 example plugin");
2611986Sandreas.sandberg@arm.com            /// Set up bindings here
2711986Sandreas.sandberg@arm.com            return m.ptr();
2811986Sandreas.sandberg@arm.com        }
2911986Sandreas.sandberg@arm.com
3011986Sandreas.sandberg@arm.com.. _core_types:
3111986Sandreas.sandberg@arm.com
3211986Sandreas.sandberg@arm.comConvenience classes for arbitrary Python types
3311986Sandreas.sandberg@arm.com==============================================
3411986Sandreas.sandberg@arm.com
3511986Sandreas.sandberg@arm.comWithout reference counting
3611986Sandreas.sandberg@arm.com--------------------------
3711986Sandreas.sandberg@arm.com
3811986Sandreas.sandberg@arm.com.. class:: handle
3911986Sandreas.sandberg@arm.com
4011986Sandreas.sandberg@arm.com    The :class:`handle` class is a thin wrapper around an arbitrary Python
4111986Sandreas.sandberg@arm.com    object (i.e. a ``PyObject *`` in Python's C API). It does not perform any
4211986Sandreas.sandberg@arm.com    automatic reference counting and merely provides a basic C++ interface to
4311986Sandreas.sandberg@arm.com    various Python API functions.
4411986Sandreas.sandberg@arm.com
4511986Sandreas.sandberg@arm.com.. seealso::
4611986Sandreas.sandberg@arm.com
4711986Sandreas.sandberg@arm.com    The :class:`object` class inherits from :class:`handle` and adds automatic
4811986Sandreas.sandberg@arm.com    reference counting features.
4911986Sandreas.sandberg@arm.com
5011986Sandreas.sandberg@arm.com.. function:: handle::handle()
5111986Sandreas.sandberg@arm.com
5211986Sandreas.sandberg@arm.com    The default constructor creates a handle with a ``nullptr``-valued pointer.
5311986Sandreas.sandberg@arm.com
5411986Sandreas.sandberg@arm.com.. function:: handle::handle(const handle&)
5511986Sandreas.sandberg@arm.com
5611986Sandreas.sandberg@arm.com    Copy constructor
5711986Sandreas.sandberg@arm.com
5811986Sandreas.sandberg@arm.com.. function:: handle::handle(PyObject *)
5911986Sandreas.sandberg@arm.com
6011986Sandreas.sandberg@arm.com    Creates a :class:`handle` from the given raw Python object pointer.
6111986Sandreas.sandberg@arm.com
6211986Sandreas.sandberg@arm.com.. function:: PyObject * handle::ptr() const
6311986Sandreas.sandberg@arm.com
6411986Sandreas.sandberg@arm.com    Return the ``PyObject *`` underlying a :class:`handle`.
6511986Sandreas.sandberg@arm.com
6611986Sandreas.sandberg@arm.com.. function:: const handle& handle::inc_ref() const
6711986Sandreas.sandberg@arm.com
6811986Sandreas.sandberg@arm.com    Manually increase the reference count of the Python object. Usually, it is
6911986Sandreas.sandberg@arm.com    preferable to use the :class:`object` class which derives from
7011986Sandreas.sandberg@arm.com    :class:`handle` and calls this function automatically. Returns a reference
7111986Sandreas.sandberg@arm.com    to itself.
7211986Sandreas.sandberg@arm.com
7311986Sandreas.sandberg@arm.com.. function:: const handle& handle::dec_ref() const
7411986Sandreas.sandberg@arm.com
7511986Sandreas.sandberg@arm.com    Manually decrease the reference count of the Python object. Usually, it is
7611986Sandreas.sandberg@arm.com    preferable to use the :class:`object` class which derives from
7711986Sandreas.sandberg@arm.com    :class:`handle` and calls this function automatically. Returns a reference
7811986Sandreas.sandberg@arm.com    to itself.
7911986Sandreas.sandberg@arm.com
8011986Sandreas.sandberg@arm.com.. function:: void handle::ref_count() const
8111986Sandreas.sandberg@arm.com
8211986Sandreas.sandberg@arm.com    Return the object's current reference count
8311986Sandreas.sandberg@arm.com
8411986Sandreas.sandberg@arm.com.. function:: handle handle::get_type() const
8511986Sandreas.sandberg@arm.com
8611986Sandreas.sandberg@arm.com    Return a handle to the Python type object underlying the instance
8711986Sandreas.sandberg@arm.com
8811986Sandreas.sandberg@arm.com.. function detail::accessor handle::operator[](handle key) const
8911986Sandreas.sandberg@arm.com
9011986Sandreas.sandberg@arm.com    Return an internal functor to invoke the object's sequence protocol.
9111986Sandreas.sandberg@arm.com    Casting the returned ``detail::accessor`` instance to a :class:`handle` or
9211986Sandreas.sandberg@arm.com    :class:`object` subclass causes a corresponding call to ``__getitem__``.
9311986Sandreas.sandberg@arm.com    Assigning a :class:`handle` or :class:`object` subclass causes a call to
9411986Sandreas.sandberg@arm.com    ``__setitem__``.
9511986Sandreas.sandberg@arm.com
9611986Sandreas.sandberg@arm.com.. function detail::accessor handle::operator[](const char *key) const
9711986Sandreas.sandberg@arm.com
9811986Sandreas.sandberg@arm.com    See the above function (the only difference is that they key is provided as
9911986Sandreas.sandberg@arm.com    a string literal).
10011986Sandreas.sandberg@arm.com
10111986Sandreas.sandberg@arm.com.. function detail::accessor handle::attr(handle key) const
10211986Sandreas.sandberg@arm.com
10311986Sandreas.sandberg@arm.com    Return an internal functor to access the object's attributes.
10411986Sandreas.sandberg@arm.com    Casting the returned ``detail::accessor`` instance to a :class:`handle` or
10511986Sandreas.sandberg@arm.com    :class:`object` subclass causes a corresponding call to ``__getattr``.
10611986Sandreas.sandberg@arm.com    Assigning a :class:`handle` or :class:`object` subclass causes a call to
10711986Sandreas.sandberg@arm.com    ``__setattr``.
10811986Sandreas.sandberg@arm.com
10911986Sandreas.sandberg@arm.com.. function detail::accessor handle::attr(const char *key) const
11011986Sandreas.sandberg@arm.com
11111986Sandreas.sandberg@arm.com    See the above function (the only difference is that they key is provided as
11211986Sandreas.sandberg@arm.com    a string literal).
11311986Sandreas.sandberg@arm.com
11411986Sandreas.sandberg@arm.com.. function operator handle::bool() const
11511986Sandreas.sandberg@arm.com
11611986Sandreas.sandberg@arm.com    Return ``true`` when the :class:`handle` wraps a valid Python object.
11711986Sandreas.sandberg@arm.com
11811986Sandreas.sandberg@arm.com.. function str handle::str() const
11911986Sandreas.sandberg@arm.com
12011986Sandreas.sandberg@arm.com    Return a string representation of the object. This is analogous to
12111986Sandreas.sandberg@arm.com    the ``str()`` function in Python.
12211986Sandreas.sandberg@arm.com
12311986Sandreas.sandberg@arm.com.. function:: template <typename T> T handle::cast() const
12411986Sandreas.sandberg@arm.com
12511986Sandreas.sandberg@arm.com    Attempt to cast the Python object into the given C++ type. A
12611986Sandreas.sandberg@arm.com    :class:`cast_error` will be throw upon failure.
12711986Sandreas.sandberg@arm.com
12811986Sandreas.sandberg@arm.com.. function:: template <typename ... Args> object handle::call(Args&&... args) const
12911986Sandreas.sandberg@arm.com
13011986Sandreas.sandberg@arm.com    Assuming the Python object is a function or implements the ``__call__``
13111986Sandreas.sandberg@arm.com    protocol, ``call()`` invokes the underlying function, passing an arbitrary
13211986Sandreas.sandberg@arm.com    set of parameters. The result is returned as a :class:`object` and may need
13311986Sandreas.sandberg@arm.com    to be converted back into a Python object using :func:`handle::cast`.
13411986Sandreas.sandberg@arm.com
13511986Sandreas.sandberg@arm.com    When some of the arguments cannot be converted to Python objects, the
13611986Sandreas.sandberg@arm.com    function will throw a :class:`cast_error` exception. When the Python
13711986Sandreas.sandberg@arm.com    function call fails, a :class:`error_already_set` exception is thrown.
13811986Sandreas.sandberg@arm.com
13911986Sandreas.sandberg@arm.comWith reference counting
14011986Sandreas.sandberg@arm.com-----------------------
14111986Sandreas.sandberg@arm.com
14211986Sandreas.sandberg@arm.com.. class:: object : public handle
14311986Sandreas.sandberg@arm.com
14411986Sandreas.sandberg@arm.com    Like :class:`handle`, the object class is a thin wrapper around an
14511986Sandreas.sandberg@arm.com    arbitrary Python object (i.e. a ``PyObject *`` in Python's C API). In
14611986Sandreas.sandberg@arm.com    contrast to :class:`handle`, it optionally increases the object's reference
14711986Sandreas.sandberg@arm.com    count upon construction, and it *always* decreases the reference count when
14811986Sandreas.sandberg@arm.com    the :class:`object` instance goes out of scope and is destructed. When
14911986Sandreas.sandberg@arm.com    using :class:`object` instances consistently, it is much easier to get
15011986Sandreas.sandberg@arm.com    reference counting right at the first attempt.
15111986Sandreas.sandberg@arm.com
15211986Sandreas.sandberg@arm.com.. function:: object::object(const object &o)
15311986Sandreas.sandberg@arm.com
15411986Sandreas.sandberg@arm.com    Copy constructor; always increases the reference count
15511986Sandreas.sandberg@arm.com
15611986Sandreas.sandberg@arm.com.. function:: object::object(const handle &h, bool borrowed)
15711986Sandreas.sandberg@arm.com
15811986Sandreas.sandberg@arm.com    Creates a :class:`object` from the given :class:`handle`. The reference
15911986Sandreas.sandberg@arm.com    count is only increased if the ``borrowed`` parameter is set to ``true``.
16011986Sandreas.sandberg@arm.com
16111986Sandreas.sandberg@arm.com.. function:: object::object(PyObject *ptr, bool borrowed)
16211986Sandreas.sandberg@arm.com
16311986Sandreas.sandberg@arm.com    Creates a :class:`object` from the given raw Python object pointer. The
16411986Sandreas.sandberg@arm.com    reference  count is only increased if the ``borrowed`` parameter is set to
16511986Sandreas.sandberg@arm.com    ``true``.
16611986Sandreas.sandberg@arm.com
16711986Sandreas.sandberg@arm.com.. function:: object::object(object &&other)
16811986Sandreas.sandberg@arm.com
16911986Sandreas.sandberg@arm.com    Move constructor; steals the object from ``other`` and preserves its
17011986Sandreas.sandberg@arm.com    reference count.
17111986Sandreas.sandberg@arm.com
17211986Sandreas.sandberg@arm.com.. function:: handle object::release()
17311986Sandreas.sandberg@arm.com
17411986Sandreas.sandberg@arm.com    Resets the internal pointer to ``nullptr`` without without decreasing the
17511986Sandreas.sandberg@arm.com    object's reference count. The function returns a raw handle to the original
17611986Sandreas.sandberg@arm.com    Python object.
17711986Sandreas.sandberg@arm.com
17811986Sandreas.sandberg@arm.com.. function:: object::~object()
17911986Sandreas.sandberg@arm.com
18011986Sandreas.sandberg@arm.com    Destructor, which automatically calls :func:`handle::dec_ref()`.
18111986Sandreas.sandberg@arm.com
18211986Sandreas.sandberg@arm.comConvenience classes for specific Python types
18311986Sandreas.sandberg@arm.com=============================================
18411986Sandreas.sandberg@arm.com
18511986Sandreas.sandberg@arm.com
18611986Sandreas.sandberg@arm.com.. class:: module : public object
18711986Sandreas.sandberg@arm.com
18811986Sandreas.sandberg@arm.com.. function:: module::module(const char *name, const char *doc = nullptr)
18911986Sandreas.sandberg@arm.com
19011986Sandreas.sandberg@arm.com    Create a new top-level Python module with the given name and docstring
19111986Sandreas.sandberg@arm.com
19211986Sandreas.sandberg@arm.com.. function:: module module::def_submodule(const char *name, const char *doc = nullptr)
19311986Sandreas.sandberg@arm.com
19411986Sandreas.sandberg@arm.com    Create and return a new Python submodule with the given name and docstring.
19511986Sandreas.sandberg@arm.com    This also works recursively, i.e.
19611986Sandreas.sandberg@arm.com
19711986Sandreas.sandberg@arm.com    .. code-block:: cpp
19811986Sandreas.sandberg@arm.com
19911986Sandreas.sandberg@arm.com        pybind11::module m("example", "pybind11 example plugin");
20011986Sandreas.sandberg@arm.com        pybind11::module m2 = m.def_submodule("sub", "A submodule of 'example'");
20111986Sandreas.sandberg@arm.com        pybind11::module m3 = m2.def_submodule("subsub", "A submodule of 'example.sub'");
20211986Sandreas.sandberg@arm.com
20311986Sandreas.sandberg@arm.com.. cpp:function:: template <typename Func, typename ... Extra> module& module::def(const char *name, Func && f, Extra && ... extra)
20411986Sandreas.sandberg@arm.com
20511986Sandreas.sandberg@arm.com    Create Python binding for a new function within the module scope. ``Func``
20611986Sandreas.sandberg@arm.com    can be a plain C++ function, a function pointer, or a lambda function. For
20711986Sandreas.sandberg@arm.com    details on the ``Extra&& ... extra`` argument, see section :ref:`extras`.
20811986Sandreas.sandberg@arm.com
20911986Sandreas.sandberg@arm.com.. _extras:
21011986Sandreas.sandberg@arm.com
21111986Sandreas.sandberg@arm.comPassing extra arguments to the def function
21211986Sandreas.sandberg@arm.com===========================================
21311986Sandreas.sandberg@arm.com
21411986Sandreas.sandberg@arm.com.. class:: arg
21511986Sandreas.sandberg@arm.com
21611986Sandreas.sandberg@arm.com.. function:: arg::arg(const char *name)
21711986Sandreas.sandberg@arm.com
21811986Sandreas.sandberg@arm.com.. function:: template <typename T> arg_v arg::operator=(T &&value)
21911986Sandreas.sandberg@arm.com
22011986Sandreas.sandberg@arm.com.. class:: arg_v : public arg
22111986Sandreas.sandberg@arm.com
22211986Sandreas.sandberg@arm.com    Represents a named argument with a default value
22311986Sandreas.sandberg@arm.com
22411986Sandreas.sandberg@arm.com.. class:: sibling
22511986Sandreas.sandberg@arm.com
22611986Sandreas.sandberg@arm.com    Used to specify a handle to an existing sibling function; used internally
22711986Sandreas.sandberg@arm.com    to implement function overloading in :func:`module::def` and
22811986Sandreas.sandberg@arm.com    :func:`class_::def`.
22911986Sandreas.sandberg@arm.com
23011986Sandreas.sandberg@arm.com.. function:: sibling::sibling(handle handle)
23111986Sandreas.sandberg@arm.com
23211986Sandreas.sandberg@arm.com.. class doc
23311986Sandreas.sandberg@arm.com
23411986Sandreas.sandberg@arm.com    This is class is internally used by pybind11.
23511986Sandreas.sandberg@arm.com
23611986Sandreas.sandberg@arm.com.. function:: doc::doc(const char *value)
23711986Sandreas.sandberg@arm.com
23811986Sandreas.sandberg@arm.com    Create a new docstring with the specified value
23911986Sandreas.sandberg@arm.com
24011986Sandreas.sandberg@arm.com.. class name
24111986Sandreas.sandberg@arm.com
24211986Sandreas.sandberg@arm.com    This is class is internally used by pybind11.
24311986Sandreas.sandberg@arm.com
24411986Sandreas.sandberg@arm.com.. function:: name::name(const char *value)
24511986Sandreas.sandberg@arm.com
24611986Sandreas.sandberg@arm.com    Used to specify the function name
24711986Sandreas.sandberg@arm.com
248