aboutsummaryrefslogtreecommitdiffstats
path: root/3rdparty/pybind11/docs/advanced
diff options
context:
space:
mode:
Diffstat (limited to '3rdparty/pybind11/docs/advanced')
-rw-r--r--3rdparty/pybind11/docs/advanced/cast/custom.rst6
-rw-r--r--3rdparty/pybind11/docs/advanced/cast/eigen.rst2
-rw-r--r--3rdparty/pybind11/docs/advanced/cast/index.rst3
-rw-r--r--3rdparty/pybind11/docs/advanced/cast/stl.rst4
-rw-r--r--3rdparty/pybind11/docs/advanced/classes.rst213
-rw-r--r--3rdparty/pybind11/docs/advanced/embedding.rst16
-rw-r--r--3rdparty/pybind11/docs/advanced/exceptions.rst190
-rw-r--r--3rdparty/pybind11/docs/advanced/functions.rst64
-rw-r--r--3rdparty/pybind11/docs/advanced/misc.rst71
-rw-r--r--3rdparty/pybind11/docs/advanced/pycpp/numpy.rst84
-rw-r--r--3rdparty/pybind11/docs/advanced/pycpp/object.rst95
-rw-r--r--3rdparty/pybind11/docs/advanced/pycpp/utilities.rst4
12 files changed, 629 insertions, 123 deletions
diff --git a/3rdparty/pybind11/docs/advanced/cast/custom.rst b/3rdparty/pybind11/docs/advanced/cast/custom.rst
index e4f99ac5..a779444c 100644
--- a/3rdparty/pybind11/docs/advanced/cast/custom.rst
+++ b/3rdparty/pybind11/docs/advanced/cast/custom.rst
@@ -29,9 +29,9 @@ The following Python snippet demonstrates the intended usage from the Python sid
from example import print
print(A())
-To register the necessary conversion routines, it is necessary to add
-a partial overload to the ``pybind11::detail::type_caster<T>`` template.
-Although this is an implementation detail, adding partial overloads to this
+To register the necessary conversion routines, it is necessary to add an
+instantiation of the ``pybind11::detail::type_caster<T>`` template.
+Although this is an implementation detail, adding an instantiation of this
type is explicitly allowed.
.. code-block:: cpp
diff --git a/3rdparty/pybind11/docs/advanced/cast/eigen.rst b/3rdparty/pybind11/docs/advanced/cast/eigen.rst
index 59ba08c3..e01472d5 100644
--- a/3rdparty/pybind11/docs/advanced/cast/eigen.rst
+++ b/3rdparty/pybind11/docs/advanced/cast/eigen.rst
@@ -274,7 +274,7 @@ Vectors versus column/row matrices
Eigen and numpy have fundamentally different notions of a vector. In Eigen, a
vector is simply a matrix with the number of columns or rows set to 1 at
-compile time (for a column vector or row vector, respectively). Numpy, in
+compile time (for a column vector or row vector, respectively). NumPy, in
contrast, has comparable 2-dimensional 1xN and Nx1 arrays, but *also* has
1-dimensional arrays of size N.
diff --git a/3rdparty/pybind11/docs/advanced/cast/index.rst b/3rdparty/pybind11/docs/advanced/cast/index.rst
index 54c10570..3ce9ea02 100644
--- a/3rdparty/pybind11/docs/advanced/cast/index.rst
+++ b/3rdparty/pybind11/docs/advanced/cast/index.rst
@@ -1,3 +1,5 @@
+.. _type-conversions:
+
Type conversions
################
@@ -39,4 +41,3 @@ the last case of the above list.
chrono
eigen
custom
-
diff --git a/3rdparty/pybind11/docs/advanced/cast/stl.rst b/3rdparty/pybind11/docs/advanced/cast/stl.rst
index e48409f0..70fde0d2 100644
--- a/3rdparty/pybind11/docs/advanced/cast/stl.rst
+++ b/3rdparty/pybind11/docs/advanced/cast/stl.rst
@@ -5,7 +5,7 @@ Automatic conversion
====================
When including the additional header file :file:`pybind11/stl.h`, conversions
-between ``std::vector<>``/``std::deque<>``/``std::list<>``/``std::array<>``,
+between ``std::vector<>``/``std::deque<>``/``std::list<>``/``std::array<>``/``std::valarray<>``,
``std::set<>``/``std::unordered_set<>``, and
``std::map<>``/``std::unordered_map<>`` and the Python ``list``, ``set`` and
``dict`` data structures are automatically enabled. The types ``std::pair<>``
@@ -157,7 +157,7 @@ the declaration
before any binding code (e.g. invocations to ``class_::def()``, etc.). This
macro must be specified at the top level (and outside of any namespaces), since
-it instantiates a partial template overload. If your binding code consists of
+it adds a template instantiation of ``type_caster``. If your binding code consists of
multiple compilation units, it must be present in every file (typically via a
common header) preceding any usage of ``std::vector<int>``. Opaque types must
also have a corresponding ``class_`` declaration to associate them with a name
diff --git a/3rdparty/pybind11/docs/advanced/classes.rst b/3rdparty/pybind11/docs/advanced/classes.rst
index ae5907de..49279020 100644
--- a/3rdparty/pybind11/docs/advanced/classes.rst
+++ b/3rdparty/pybind11/docs/advanced/classes.rst
@@ -71,7 +71,7 @@ helper class that is defined as follows:
/* Trampoline (need one for each virtual function) */
std::string go(int n_times) override {
- PYBIND11_OVERLOAD_PURE(
+ PYBIND11_OVERRIDE_PURE(
std::string, /* Return type */
Animal, /* Parent class */
go, /* Name of function in C++ (must match Python name) */
@@ -80,10 +80,10 @@ helper class that is defined as follows:
}
};
-The macro :c:macro:`PYBIND11_OVERLOAD_PURE` should be used for pure virtual
-functions, and :c:macro:`PYBIND11_OVERLOAD` should be used for functions which have
-a default implementation. There are also two alternate macros
-:c:macro:`PYBIND11_OVERLOAD_PURE_NAME` and :c:macro:`PYBIND11_OVERLOAD_NAME` which
+The macro :c:macro:`PYBIND11_OVERRIDE_PURE` should be used for pure virtual
+functions, and :c:macro:`PYBIND11_OVERRIDE` should be used for functions which have
+a default implementation. There are also two alternate macros
+:c:macro:`PYBIND11_OVERRIDE_PURE_NAME` and :c:macro:`PYBIND11_OVERRIDE_NAME` which
take a string-valued name argument between the *Parent class* and *Name of the
function* slots, which defines the name of function in Python. This is required
when the C++ and Python versions of the
@@ -122,7 +122,7 @@ Bindings should be made against the actual class, not the trampoline helper clas
Note, however, that the above is sufficient for allowing python classes to
extend ``Animal``, but not ``Dog``: see :ref:`virtual_and_inheritance` for the
-necessary steps required to providing proper overload support for inherited
+necessary steps required to providing proper overriding support for inherited
classes.
The Python session below shows how to override ``Animal::go`` and invoke it via
@@ -149,13 +149,17 @@ memory for the C++ portion of the instance will be left uninitialized, which
will generally leave the C++ instance in an invalid state and cause undefined
behavior if the C++ instance is subsequently used.
+.. versionchanged:: 2.6
+ The default pybind11 metaclass will throw a ``TypeError`` when it detects
+ that ``__init__`` was not called by a derived class.
+
Here is an example:
.. code-block:: python
class Dachshund(Dog):
def __init__(self, name):
- Dog.__init__(self) # Without this, undefined behavior may occur if the C++ portions are referenced.
+ Dog.__init__(self) # Without this, a TypeError is raised.
self.name = name
def bark(self):
return "yap!"
@@ -177,15 +181,24 @@ Please take a look at the :ref:`macro_notes` before using this feature.
- because in these cases there is no C++ variable to reference (the value
is stored in the referenced Python variable), pybind11 provides one in
- the PYBIND11_OVERLOAD macros (when needed) with static storage duration.
- Note that this means that invoking the overloaded method on *any*
+ the PYBIND11_OVERRIDE macros (when needed) with static storage duration.
+ Note that this means that invoking the overridden method on *any*
instance will change the referenced value stored in *all* instances of
that type.
- Attempts to modify a non-const reference will not have the desired
effect: it will change only the static cache variable, but this change
will not propagate to underlying Python instance, and the change will be
- replaced the next time the overload is invoked.
+ replaced the next time the override is invoked.
+
+.. warning::
+
+ The :c:macro:`PYBIND11_OVERRIDE` and accompanying macros used to be called
+ ``PYBIND11_OVERLOAD`` up until pybind11 v2.5.0, and :func:`get_override`
+ used to be called ``get_overload``. This naming was corrected and the older
+ macro and function names may soon be deprecated, in order to reduce
+ confusion with overloaded functions and methods and ``py::overload_cast``
+ (see :ref:`classes`).
.. seealso::
@@ -233,20 +246,20 @@ override the ``name()`` method):
class PyAnimal : public Animal {
public:
using Animal::Animal; // Inherit constructors
- std::string go(int n_times) override { PYBIND11_OVERLOAD_PURE(std::string, Animal, go, n_times); }
- std::string name() override { PYBIND11_OVERLOAD(std::string, Animal, name, ); }
+ std::string go(int n_times) override { PYBIND11_OVERRIDE_PURE(std::string, Animal, go, n_times); }
+ std::string name() override { PYBIND11_OVERRIDE(std::string, Animal, name, ); }
};
class PyDog : public Dog {
public:
using Dog::Dog; // Inherit constructors
- std::string go(int n_times) override { PYBIND11_OVERLOAD(std::string, Dog, go, n_times); }
- std::string name() override { PYBIND11_OVERLOAD(std::string, Dog, name, ); }
- std::string bark() override { PYBIND11_OVERLOAD(std::string, Dog, bark, ); }
+ std::string go(int n_times) override { PYBIND11_OVERRIDE(std::string, Dog, go, n_times); }
+ std::string name() override { PYBIND11_OVERRIDE(std::string, Dog, name, ); }
+ std::string bark() override { PYBIND11_OVERRIDE(std::string, Dog, bark, ); }
};
.. note::
- Note the trailing commas in the ``PYBIND11_OVERLOAD`` calls to ``name()``
+ Note the trailing commas in the ``PYBIND11_OVERIDE`` calls to ``name()``
and ``bark()``. These are needed to portably implement a trampoline for a
function that does not take any arguments. For functions that take
a nonzero number of arguments, the trailing comma must be omitted.
@@ -261,9 +274,9 @@ declare or override any virtual methods itself:
class PyHusky : public Husky {
public:
using Husky::Husky; // Inherit constructors
- std::string go(int n_times) override { PYBIND11_OVERLOAD_PURE(std::string, Husky, go, n_times); }
- std::string name() override { PYBIND11_OVERLOAD(std::string, Husky, name, ); }
- std::string bark() override { PYBIND11_OVERLOAD(std::string, Husky, bark, ); }
+ std::string go(int n_times) override { PYBIND11_OVERRIDE_PURE(std::string, Husky, go, n_times); }
+ std::string name() override { PYBIND11_OVERRIDE(std::string, Husky, name, ); }
+ std::string bark() override { PYBIND11_OVERRIDE(std::string, Husky, bark, ); }
};
There is, however, a technique that can be used to avoid this duplication
@@ -276,15 +289,15 @@ follows:
template <class AnimalBase = Animal> class PyAnimal : public AnimalBase {
public:
using AnimalBase::AnimalBase; // Inherit constructors
- std::string go(int n_times) override { PYBIND11_OVERLOAD_PURE(std::string, AnimalBase, go, n_times); }
- std::string name() override { PYBIND11_OVERLOAD(std::string, AnimalBase, name, ); }
+ std::string go(int n_times) override { PYBIND11_OVERRIDE_PURE(std::string, AnimalBase, go, n_times); }
+ std::string name() override { PYBIND11_OVERRIDE(std::string, AnimalBase, name, ); }
};
template <class DogBase = Dog> class PyDog : public PyAnimal<DogBase> {
public:
using PyAnimal<DogBase>::PyAnimal; // Inherit constructors
// Override PyAnimal's pure virtual go() with a non-pure one:
- std::string go(int n_times) override { PYBIND11_OVERLOAD(std::string, DogBase, go, n_times); }
- std::string bark() override { PYBIND11_OVERLOAD(std::string, DogBase, bark, ); }
+ std::string go(int n_times) override { PYBIND11_OVERRIDE(std::string, DogBase, go, n_times); }
+ std::string bark() override { PYBIND11_OVERRIDE(std::string, DogBase, bark, ); }
};
This technique has the advantage of requiring just one trampoline method to be
@@ -298,8 +311,8 @@ The classes are then registered with pybind11 using:
.. code-block:: cpp
py::class_<Animal, PyAnimal<>> animal(m, "Animal");
- py::class_<Dog, PyDog<>> dog(m, "Dog");
- py::class_<Husky, PyDog<Husky>> husky(m, "Husky");
+ py::class_<Dog, Animal, PyDog<>> dog(m, "Dog");
+ py::class_<Husky, Dog, PyDog<Husky>> husky(m, "Husky");
// ... add animal, dog, husky definitions
Note that ``Husky`` did not require a dedicated trampoline template class at
@@ -337,7 +350,7 @@ valid for the trampoline class but not the registered class. This is primarily
for performance reasons: when the trampoline class is not needed for anything
except virtual method dispatching, not initializing the trampoline class
improves performance by avoiding needing to do a run-time check to see if the
-inheriting python instance has an overloaded method.
+inheriting python instance has an overridden method.
Sometimes, however, it is useful to always initialize a trampoline class as an
intermediate class that does more than just handle virtual method dispatching.
@@ -368,7 +381,7 @@ references (See also :ref:`faq_reference_arguments`). Another way of solving
this is to use the method body of the trampoline class to do conversions to the
input and return of the Python method.
-The main building block to do so is the :func:`get_overload`, this function
+The main building block to do so is the :func:`get_override`, this function
allows retrieving a method implemented in Python from within the trampoline's
methods. Consider for example a C++ method which has the signature
``bool myMethod(int32_t& value)``, where the return indicates whether
@@ -380,10 +393,10 @@ Python side by allowing the Python function to return ``None`` or an ``int``:
bool MyClass::myMethod(int32_t& value)
{
pybind11::gil_scoped_acquire gil; // Acquire the GIL while in this scope.
- // Try to look up the overloaded method on the Python side.
- pybind11::function overload = pybind11::get_overload(this, "myMethod");
- if (overload) { // method is found
- auto obj = overload(value); // Call the Python function.
+ // Try to look up the overridden method on the Python side.
+ pybind11::function override = pybind11::get_override(this, "myMethod");
+ if (override) { // method is found
+ auto obj = override(value); // Call the Python function.
if (py::isinstance<py::int_>(obj)) { // check if it returned a Python integer type
value = obj.cast<int32_t>(); // Cast it and assign it to the value.
return true; // Return true; value should be used.
@@ -554,6 +567,46 @@ crucial that instances are deallocated on the C++ side to avoid memory leaks.
py::class_<MyClass, std::unique_ptr<MyClass, py::nodelete>>(m, "MyClass")
.def(py::init<>())
+.. _destructors_that_call_python:
+
+Destructors that call Python
+============================
+
+If a Python function is invoked from a C++ destructor, an exception may be thrown
+of type :class:`error_already_set`. If this error is thrown out of a class destructor,
+``std::terminate()`` will be called, terminating the process. Class destructors
+must catch all exceptions of type :class:`error_already_set` to discard the Python
+exception using :func:`error_already_set::discard_as_unraisable`.
+
+Every Python function should be treated as *possibly throwing*. When a Python generator
+stops yielding items, Python will throw a ``StopIteration`` exception, which can pass
+though C++ destructors if the generator's stack frame holds the last reference to C++
+objects.
+
+For more information, see :ref:`the documentation on exceptions <unraisable_exceptions>`.
+
+.. code-block:: cpp
+
+ class MyClass {
+ public:
+ ~MyClass() {
+ try {
+ py::print("Even printing is dangerous in a destructor");
+ py::exec("raise ValueError('This is an unraisable exception')");
+ } catch (py::error_already_set &e) {
+ // error_context should be information about where/why the occurred,
+ // e.g. use __func__ to get the name of the current function
+ e.discard_as_unraisable(__func__);
+ }
+ }
+ };
+
+.. note::
+
+ pybind11 does not support C++ destructors marked ``noexcept(false)``.
+
+.. versionadded:: 2.6
+
.. _implicit_conversions:
Implicit conversions
@@ -768,13 +821,17 @@ An instance can now be pickled as follows:
p.setExtra(15)
data = pickle.dumps(p, 2)
-Note that only the cPickle module is supported on Python 2.7. The second
-argument to ``dumps`` is also crucial: it selects the pickle protocol version
-2, since the older version 1 is not supported. Newer versions are also fine—for
-instance, specify ``-1`` to always use the latest available version. Beware:
-failure to follow these instructions will cause important pybind11 memory
-allocation routines to be skipped during unpickling, which will likely lead to
-memory corruption and/or segmentation faults.
+
+.. note::
+ Note that only the cPickle module is supported on Python 2.7.
+
+ The second argument to ``dumps`` is also crucial: it selects the pickle
+ protocol version 2, since the older version 1 is not supported. Newer
+ versions are also fine—for instance, specify ``-1`` to always use the
+ latest available version. Beware: failure to follow these instructions
+ will cause important pybind11 memory allocation routines to be skipped
+ during unpickling, which will likely lead to memory corruption and/or
+ segmentation faults.
.. seealso::
@@ -784,6 +841,38 @@ memory corruption and/or segmentation faults.
.. [#f3] http://docs.python.org/3/library/pickle.html#pickling-class-instances
+Deepcopy support
+================
+
+Python normally uses references in assignments. Sometimes a real copy is needed
+to prevent changing all copies. The ``copy`` module [#f5]_ provides these
+capabilities.
+
+On Python 3, a class with pickle support is automatically also (deep)copy
+compatible. However, performance can be improved by adding custom
+``__copy__`` and ``__deepcopy__`` methods. With Python 2.7, these custom methods
+are mandatory for (deep)copy compatibility, because pybind11 only supports
+cPickle.
+
+For simple classes (deep)copy can be enabled by using the copy constructor,
+which should look as follows:
+
+.. code-block:: cpp
+
+ py::class_<Copyable>(m, "Copyable")
+ .def("__copy__", [](const Copyable &self) {
+ return Copyable(self);
+ })
+ .def("__deepcopy__", [](const Copyable &self, py::dict) {
+ return Copyable(self);
+ }, "memo"_a);
+
+.. note::
+
+ Dynamic attributes will not be copied in this example.
+
+.. [#f5] https://docs.python.org/3/library/copy.html
+
Multiple Inheritance
====================
@@ -1024,7 +1113,7 @@ described trampoline:
class Trampoline : public A {
public:
- int foo() const override { PYBIND11_OVERLOAD(int, A, foo, ); }
+ int foo() const override { PYBIND11_OVERRIDE(int, A, foo, ); }
};
class Publicist : public A {
@@ -1042,6 +1131,34 @@ described trampoline:
``.def("foo", static_cast<int (A::*)() const>(&Publicist::foo));``
where ``int (A::*)() const`` is the type of ``A::foo``.
+Binding final classes
+=====================
+
+Some classes may not be appropriate to inherit from. In C++11, classes can
+use the ``final`` specifier to ensure that a class cannot be inherited from.
+The ``py::is_final`` attribute can be used to ensure that Python classes
+cannot inherit from a specified type. The underlying C++ type does not need
+to be declared final.
+
+.. code-block:: cpp
+
+ class IsFinal final {};
+
+ py::class_<IsFinal>(m, "IsFinal", py::is_final());
+
+When you try to inherit from such a class in Python, you will now get this
+error:
+
+.. code-block:: pycon
+
+ >>> class PyFinalChild(IsFinal):
+ ... pass
+ TypeError: type 'IsFinal' is not an acceptable base type
+
+.. note:: This attribute is currently ignored on PyPy
+
+.. versionadded:: 2.6
+
Custom automatic downcasters
============================
@@ -1124,3 +1241,21 @@ appropriate derived-class pointer (e.g. using
more complete example, including a demonstration of how to provide
automatic downcasting for an entire class hierarchy without
writing one get() function for each class.
+
+Accessing the type object
+=========================
+
+You can get the type object from a C++ class that has already been registered using:
+
+.. code-block:: python
+
+ py::type T_py = py::type::of<T>();
+
+You can directly use ``py::type::of(ob)`` to get the type object from any python
+object, just like ``type(ob)`` in Python.
+
+.. note::
+
+ Other types, like ``py::type::of<int>()``, do not work, see :ref:`type-conversions`.
+
+.. versionadded:: 2.6
diff --git a/3rdparty/pybind11/docs/advanced/embedding.rst b/3rdparty/pybind11/docs/advanced/embedding.rst
index 39303160..dfdaad2d 100644
--- a/3rdparty/pybind11/docs/advanced/embedding.rst
+++ b/3rdparty/pybind11/docs/advanced/embedding.rst
@@ -18,7 +18,7 @@ information, see :doc:`/compiling`.
.. code-block:: cmake
- cmake_minimum_required(VERSION 3.0)
+ cmake_minimum_required(VERSION 3.4)
project(example)
find_package(pybind11 REQUIRED) # or `add_subdirectory(pybind11)`
@@ -108,11 +108,11 @@ The two approaches can also be combined:
Importing modules
=================
-Python modules can be imported using `module::import()`:
+Python modules can be imported using `module_::import()`:
.. code-block:: cpp
- py::module sys = py::module::import("sys");
+ py::module_ sys = py::module_::import("sys");
py::print(sys.attr("path"));
For convenience, the current working directory is included in ``sys.path`` when
@@ -128,12 +128,12 @@ embedding the interpreter. This makes it easy to import local Python files:
.. code-block:: cpp
- py::module calc = py::module::import("calc");
+ py::module_ calc = py::module_::import("calc");
py::object result = calc.attr("add")(1, 2);
int n = result.cast<int>();
assert(n == 3);
-Modules can be reloaded using `module::reload()` if the source is modified e.g.
+Modules can be reloaded using `module_::reload()` if the source is modified e.g.
by an external process. This can be useful in scenarios where the application
imports a user defined data processing script which needs to be updated after
changes by the user. Note that this function does not reload modules recursively.
@@ -153,7 +153,7 @@ like any other module.
namespace py = pybind11;
PYBIND11_EMBEDDED_MODULE(fast_calc, m) {
- // `m` is a `py::module` which is used to bind functions and classes
+ // `m` is a `py::module_` which is used to bind functions and classes
m.def("add", [](int i, int j) {
return i + j;
});
@@ -162,7 +162,7 @@ like any other module.
int main() {
py::scoped_interpreter guard{};
- auto fast_calc = py::module::import("fast_calc");
+ auto fast_calc = py::module_::import("fast_calc");
auto result = fast_calc.attr("add")(1, 2).cast<int>();
assert(result == 3);
}
@@ -196,7 +196,7 @@ naturally:
int main() {
py::scoped_interpreter guard{};
- auto py_module = py::module::import("py_module");
+ auto py_module = py::module_::import("py_module");
auto locals = py::dict("fmt"_a="{} + {} = {}", **py_module.attr("__dict__"));
assert(locals["a"].cast<int>() == 1);
diff --git a/3rdparty/pybind11/docs/advanced/exceptions.rst b/3rdparty/pybind11/docs/advanced/exceptions.rst
index 75ad7f7f..5eae5562 100644
--- a/3rdparty/pybind11/docs/advanced/exceptions.rst
+++ b/3rdparty/pybind11/docs/advanced/exceptions.rst
@@ -1,18 +1,24 @@
Exceptions
##########
-Built-in exception translation
-==============================
+Built-in C++ to Python exception translation
+============================================
+
+When Python calls C++ code through pybind11, pybind11 provides a C++ exception handler
+that will trap C++ exceptions, translate them to the corresponding Python exception,
+and raise them so that Python code can handle them.
-When C++ code invoked from Python throws an ``std::exception``, it is
-automatically converted into a Python ``Exception``. pybind11 defines multiple
-special exception classes that will map to different types of Python
-exceptions:
+pybind11 defines translations for ``std::exception`` and its standard
+subclasses, and several special exception classes that translate to specific
+Python exceptions. Note that these are not actually Python exceptions, so they
+cannot be examined using the Python C API. Instead, they are pure C++ objects
+that pybind11 will translate the corresponding Python exception when they arrive
+at its exception handler.
.. tabularcolumns:: |p{0.5\textwidth}|p{0.45\textwidth}|
+--------------------------------------+--------------------------------------+
-| C++ exception type | Python exception type |
+| Exception thrown by C++ | Translated to Python exception type |
+======================================+======================================+
| :class:`std::exception` | ``RuntimeError`` |
+--------------------------------------+--------------------------------------+
@@ -46,16 +52,11 @@ exceptions:
| | ``__setitem__`` in dict-like |
| | objects, etc.) |
+--------------------------------------+--------------------------------------+
-| :class:`pybind11::error_already_set` | Indicates that the Python exception |
-| | flag has already been set via Python |
-| | API calls from C++ code; this C++ |
-| | exception is used to propagate such |
-| | a Python exception back to Python. |
-+--------------------------------------+--------------------------------------+
-When a Python function invoked from C++ throws an exception, it is converted
-into a C++ exception of type :class:`error_already_set` whose string payload
-contains a textual summary.
+Exception translation is not bidirectional. That is, *catching* the C++
+exceptions defined above above will not trap exceptions that originate from
+Python. For that, catch :class:`pybind11::error_already_set`. See :ref:`below
+<handling_python_exceptions_cpp>` for further details.
There is also a special exception :class:`cast_error` that is thrown by
:func:`handle::call` when the input arguments cannot be converted to Python
@@ -78,6 +79,19 @@ This call creates a Python exception class with the name ``PyExp`` in the given
module and automatically converts any encountered exceptions of type ``CppExp``
into Python exceptions of type ``PyExp``.
+It is possible to specify base class for the exception using the third
+parameter, a `handle`:
+
+.. code-block:: cpp
+
+ py::register_exception<CppExp>(module, "PyExp", PyExc_RuntimeError);
+
+Then `PyExp` can be caught both as `PyExp` and `RuntimeError`.
+
+The class objects of the built-in Python exceptions are listed in the Python
+documentation on `Standard Exceptions <https://docs.python.org/3/c-api/exceptions.html#standard-exceptions>`_.
+The default base class is `PyExc_Exception`.
+
When more advanced exception translation is needed, the function
``py::register_exception_translator(translator)`` can be used to register
functions that can translate arbitrary exception types (and which may include
@@ -100,7 +114,6 @@ and use this in the associated exception translator (note: it is often useful
to make this a static declaration when using it inside a lambda expression
without requiring capturing).
-
The following example demonstrates this for a hypothetical exception classes
``MyCustomException`` and ``OtherException``: the first is translated to a
custom python exception ``MyCustomError``, while the second is translated to a
@@ -134,7 +147,7 @@ section.
.. note::
- You must call either ``PyErr_SetString`` or a custom exception's call
+ Call either ``PyErr_SetString`` or a custom exception's call
operator (``exc(string)``) for every exception caught in a custom exception
translator. Failure to do so will cause Python to crash with ``SystemError:
error return without exception set``.
@@ -142,3 +155,144 @@ section.
Exceptions that you do not plan to handle should simply not be caught, or
may be explicitly (re-)thrown to delegate it to the other,
previously-declared existing exception translators.
+
+.. _handling_python_exceptions_cpp:
+
+Handling exceptions from Python in C++
+======================================
+
+When C++ calls Python functions, such as in a callback function or when
+manipulating Python objects, and Python raises an ``Exception``, pybind11
+converts the Python exception into a C++ exception of type
+:class:`pybind11::error_already_set` whose payload contains a C++ string textual
+summary and the actual Python exception. ``error_already_set`` is used to
+propagate Python exception back to Python (or possibly, handle them in C++).
+
+.. tabularcolumns:: |p{0.5\textwidth}|p{0.45\textwidth}|
+
++--------------------------------------+--------------------------------------+
+| Exception raised in Python | Thrown as C++ exception type |
++======================================+======================================+
+| Any Python ``Exception`` | :class:`pybind11::error_already_set` |
++--------------------------------------+--------------------------------------+
+
+For example:
+
+.. code-block:: cpp
+
+ try {
+ // open("missing.txt", "r")
+ auto file = py::module_::import("io").attr("open")("missing.txt", "r");
+ auto text = file.attr("read")();
+ file.attr("close")();
+ } catch (py::error_already_set &e) {
+ if (e.matches(PyExc_FileNotFoundError)) {
+ py::print("missing.txt not found");
+ } else if (e.match(PyExc_PermissionError)) {
+ py::print("missing.txt found but not accessible");
+ } else {
+ throw;
+ }
+ }
+
+Note that C++ to Python exception translation does not apply here, since that is
+a method for translating C++ exceptions to Python, not vice versa. The error raised
+from Python is always ``error_already_set``.
+
+This example illustrates this behavior:
+
+.. code-block:: cpp
+
+ try {
+ py::eval("raise ValueError('The Ring')");
+ } catch (py::value_error &boromir) {
+ // Boromir never gets the ring
+ assert(false);
+ } catch (py::error_already_set &frodo) {
+ // Frodo gets the ring
+ py::print("I will take the ring");
+ }
+
+ try {
+ // py::value_error is a request for pybind11 to raise a Python exception
+ throw py::value_error("The ball");
+ } catch (py::error_already_set &cat) {
+ // cat won't catch the ball since
+ // py::value_error is not a Python exception
+ assert(false);
+ } catch (py::value_error &dog) {
+ // dog will catch the ball
+ py::print("Run Spot run");
+ throw; // Throw it again (pybind11 will raise ValueError)
+ }
+
+Handling errors from the Python C API
+=====================================
+
+Where possible, use :ref:`pybind11 wrappers <wrappers>` instead of calling
+the Python C API directly. When calling the Python C API directly, in
+addition to manually managing reference counts, one must follow the pybind11
+error protocol, which is outlined here.
+
+After calling the Python C API, if Python returns an error,
+``throw py::error_already_set();``, which allows pybind11 to deal with the
+exception and pass it back to the Python interpreter. This includes calls to
+the error setting functions such as ``PyErr_SetString``.
+
+.. code-block:: cpp
+
+ PyErr_SetString(PyExc_TypeError, "C API type error demo");
+ throw py::error_already_set();
+
+ // But it would be easier to simply...
+ throw py::type_error("pybind11 wrapper type error");
+
+Alternately, to ignore the error, call `PyErr_Clear
+<https://docs.python.org/3/c-api/exceptions.html#c.PyErr_Clear>`_.
+
+Any Python error must be thrown or cleared, or Python/pybind11 will be left in
+an invalid state.
+
+.. _unraisable_exceptions:
+
+Handling unraisable exceptions
+==============================
+
+If a Python function invoked from a C++ destructor or any function marked
+``noexcept(true)`` (collectively, "noexcept functions") throws an exception, there
+is no way to propagate the exception, as such functions may not throw.
+Should they throw or fail to catch any exceptions in their call graph,
+the C++ runtime calls ``std::terminate()`` to abort immediately.
+
+Similarly, Python exceptions raised in a class's ``__del__`` method do not
+propagate, but are logged by Python as an unraisable error. In Python 3.8+, a
+`system hook is triggered
+<https://docs.python.org/3/library/sys.html#sys.unraisablehook>`_
+and an auditing event is logged.
+
+Any noexcept function should have a try-catch block that traps
+class:`error_already_set` (or any other exception that can occur). Note that
+pybind11 wrappers around Python exceptions such as
+:class:`pybind11::value_error` are *not* Python exceptions; they are C++
+exceptions that pybind11 catches and converts to Python exceptions. Noexcept
+functions cannot propagate these exceptions either. A useful approach is to
+convert them to Python exceptions and then ``discard_as_unraisable`` as shown
+below.
+
+.. code-block:: cpp
+
+ void nonthrowing_func() noexcept(true) {
+ try {
+ // ...
+ } catch (py::error_already_set &eas) {
+ // Discard the Python error using Python APIs, using the C++ magic
+ // variable __func__. Python already knows the type and value and of the
+ // exception object.
+ eas.discard_as_unraisable(__func__);
+ } catch (const std::exception &e) {
+ // Log and discard C++ exceptions.
+ third_party::log(e);
+ }
+ }
+
+.. versionadded:: 2.6
diff --git a/3rdparty/pybind11/docs/advanced/functions.rst b/3rdparty/pybind11/docs/advanced/functions.rst
index 3e1a3ff0..ebdff9c9 100644
--- a/3rdparty/pybind11/docs/advanced/functions.rst
+++ b/3rdparty/pybind11/docs/advanced/functions.rst
@@ -17,7 +17,7 @@ bindings for functions that return a non-trivial type. Just by looking at the
type information, it is not clear whether Python should take charge of the
returned value and eventually free its resources, or if this is handled on the
C++ side. For this reason, pybind11 provides a several *return value policy*
-annotations that can be passed to the :func:`module::def` and
+annotations that can be passed to the :func:`module_::def` and
:func:`class_::def` functions. The default policy is
:enum:`return_value_policy::automatic`.
@@ -360,7 +360,55 @@ like so:
.. code-block:: cpp
py::class_<MyClass>("MyClass")
- .def("myFunction", py::arg("arg") = (SomeType *) nullptr);
+ .def("myFunction", py::arg("arg") = static_cast<SomeType *>(nullptr));
+
+Keyword-only arguments
+======================
+
+Python 3 introduced keyword-only arguments by specifying an unnamed ``*``
+argument in a function definition:
+
+.. code-block:: python
+
+ def f(a, *, b): # a can be positional or via keyword; b must be via keyword
+ pass
+
+ f(a=1, b=2) # good
+ f(b=2, a=1) # good
+ f(1, b=2) # good
+ f(1, 2) # TypeError: f() takes 1 positional argument but 2 were given
+
+Pybind11 provides a ``py::kw_only`` object that allows you to implement
+the same behaviour by specifying the object between positional and keyword-only
+argument annotations when registering the function:
+
+.. code-block:: cpp
+
+ m.def("f", [](int a, int b) { /* ... */ },
+ py::arg("a"), py::kw_only(), py::arg("b"));
+
+Note that you currently cannot combine this with a ``py::args`` argument. This
+feature does *not* require Python 3 to work.
+
+.. versionadded:: 2.6
+
+Positional-only arguments
+=========================
+
+Python 3.8 introduced a new positional-only argument syntax, using ``/`` in the
+function definition (note that this has been a convention for CPython
+positional arguments, such as in ``pow()``, since Python 2). You can
+do the same thing in any version of Python using ``py::pos_only()``:
+
+.. code-block:: cpp
+
+ m.def("f", [](int a, int b) { /* ... */ },
+ py::arg("a"), py::pos_only(), py::arg("b"));
+
+You now cannot give argument ``a`` by keyword. This can be combined with
+keyword-only arguments, as well.
+
+.. versionadded:: 2.6
.. _nonconverting_arguments:
@@ -492,11 +540,13 @@ an explicit ``py::arg().noconvert()`` attribute in the function definition).
If the second pass also fails a ``TypeError`` is raised.
Within each pass, overloads are tried in the order they were registered with
-pybind11.
+pybind11. If the ``py::prepend()`` tag is added to the definition, a function
+can be placed at the beginning of the overload sequence instead, allowing user
+overloads to proceed built in functions.
What this means in practice is that pybind11 will prefer any overload that does
-not require conversion of arguments to an overload that does, but otherwise prefers
-earlier-defined overloads to later-defined ones.
+not require conversion of arguments to an overload that does, but otherwise
+prefers earlier-defined overloads to later-defined ones.
.. note::
@@ -505,3 +555,7 @@ earlier-defined overloads to later-defined ones.
requiring one conversion over one requiring three, but only prioritizes
overloads requiring no conversion at all to overloads that require
conversion of at least one argument.
+
+.. versionadded:: 2.6
+
+ The ``py::prepend()`` tag.
diff --git a/3rdparty/pybind11/docs/advanced/misc.rst b/3rdparty/pybind11/docs/advanced/misc.rst
index 5b38ec75..b3f3b226 100644
--- a/3rdparty/pybind11/docs/advanced/misc.rst
+++ b/3rdparty/pybind11/docs/advanced/misc.rst
@@ -7,14 +7,14 @@ General notes regarding convenience macros
==========================================
pybind11 provides a few convenience macros such as
-:func:`PYBIND11_DECLARE_HOLDER_TYPE` and ``PYBIND11_OVERLOAD_*``. Since these
+:func:`PYBIND11_DECLARE_HOLDER_TYPE` and ``PYBIND11_OVERRIDE_*``. Since these
are "just" macros that are evaluated in the preprocessor (which has no concept
of types), they *will* get confused by commas in a template argument; for
example, consider:
.. code-block:: cpp
- PYBIND11_OVERLOAD(MyReturnType<T1, T2>, Class<T3, T4>, func)
+ PYBIND11_OVERRIDE(MyReturnType<T1, T2>, Class<T3, T4>, func)
The limitation of the C preprocessor interprets this as five arguments (with new
arguments beginning after each comma) rather than three. To get around this,
@@ -26,10 +26,10 @@ using the ``PYBIND11_TYPE`` macro:
// Version 1: using a type alias
using ReturnType = MyReturnType<T1, T2>;
using ClassType = Class<T3, T4>;
- PYBIND11_OVERLOAD(ReturnType, ClassType, func);
+ PYBIND11_OVERRIDE(ReturnType, ClassType, func);
// Version 2: using the PYBIND11_TYPE macro:
- PYBIND11_OVERLOAD(PYBIND11_TYPE(MyReturnType<T1, T2>),
+ PYBIND11_OVERRIDE(PYBIND11_TYPE(MyReturnType<T1, T2>),
PYBIND11_TYPE(Class<T3, T4>), func)
The ``PYBIND11_MAKE_OPAQUE`` macro does *not* require the above workarounds.
@@ -59,7 +59,7 @@ could be realized as follows (important changes highlighted):
/* Acquire GIL before calling Python code */
py::gil_scoped_acquire acquire;
- PYBIND11_OVERLOAD_PURE(
+ PYBIND11_OVERRIDE_PURE(
std::string, /* Return type */
Animal, /* Parent class */
go, /* Name of function */
@@ -132,7 +132,7 @@ However, it can be acquired as follows:
.. code-block:: cpp
- py::object pet = (py::object) py::module::import("basic").attr("Pet");
+ py::object pet = (py::object) py::module_::import("basic").attr("Pet");
py::class_<Dog>(m, "Dog", pet)
.def(py::init<const std::string &>())
@@ -146,7 +146,7 @@ has been executed:
.. code-block:: cpp
- py::module::import("basic");
+ py::module_::import("basic");
py::class_<Dog, Pet>(m, "Dog")
.def(py::init<const std::string &>())
@@ -176,9 +176,9 @@ pybind11 version. Consider the following example:
.. code-block:: cpp
- auto data = (MyData *) py::get_shared_data("mydata");
+ auto data = reinterpret_cast<MyData *>(py::get_shared_data("mydata"));
if (!data)
- data = (MyData *) py::set_shared_data("mydata", new MyData(42));
+ data = static_cast<MyData *>(py::set_shared_data("mydata", new MyData(42)));
If the above snippet was used in several separately compiled extension modules,
the first one to be imported would create a ``MyData`` instance and associate
@@ -218,12 +218,12 @@ collected:
Both approaches also expose a potentially dangerous ``_cleanup`` attribute in
Python, which may be undesirable from an API standpoint (a premature explicit
-call from Python might lead to undefined behavior). Yet another approach that
+call from Python might lead to undefined behavior). Yet another approach that
avoids this issue involves weak reference with a cleanup callback:
.. code-block:: cpp
- // Register a callback function that is invoked when the BaseClass object is colelcted
+ // Register a callback function that is invoked when the BaseClass object is collected
py::cpp_function cleanup_callback(
[](py::handle weakref) {
// perform cleanup here -- this function is called with the GIL held
@@ -237,13 +237,13 @@ avoids this issue involves weak reference with a cleanup callback:
.. note::
- PyPy (at least version 5.9) does not garbage collect objects when the
- interpreter exits. An alternative approach (which also works on CPython) is to use
- the :py:mod:`atexit` module [#f7]_, for example:
+ PyPy does not garbage collect objects when the interpreter exits. An alternative
+ approach (which also works on CPython) is to use the :py:mod:`atexit` module [#f7]_,
+ for example:
.. code-block:: cpp
- auto atexit = py::module::import("atexit");
+ auto atexit = py::module_::import("atexit");
atexit.attr("register")(py::cpp_function([]() {
// perform cleanup here -- this function is called with the GIL held
}));
@@ -283,9 +283,9 @@ work, it is important that all lines are indented consistently, i.e.:
----------
)mydelimiter");
-By default, pybind11 automatically generates and prepends a signature to the docstring of a function
-registered with ``module::def()`` and ``class_::def()``. Sometimes this
-behavior is not desirable, because you want to provide your own signature or remove
+By default, pybind11 automatically generates and prepends a signature to the docstring of a function
+registered with ``module_::def()`` and ``class_::def()``. Sometimes this
+behavior is not desirable, because you want to provide your own signature or remove
the docstring completely to exclude the function from the Sphinx documentation.
The class ``options`` allows you to selectively suppress auto-generated signatures:
@@ -298,9 +298,40 @@ The class ``options`` allows you to selectively suppress auto-generated signatur
m.def("add", [](int a, int b) { return a + b; }, "A function which adds two numbers");
}
-Note that changes to the settings affect only function bindings created during the
-lifetime of the ``options`` instance. When it goes out of scope at the end of the module's init function,
+Note that changes to the settings affect only function bindings created during the
+lifetime of the ``options`` instance. When it goes out of scope at the end of the module's init function,
the default settings are restored to prevent unwanted side effects.
.. [#f4] http://www.sphinx-doc.org
.. [#f5] http://github.com/pybind/python_example
+
+.. _avoiding-cpp-types-in-docstrings:
+
+Avoiding C++ types in docstrings
+================================
+
+Docstrings are generated at the time of the declaration, e.g. when ``.def(...)`` is called.
+At this point parameter and return types should be known to pybind11.
+If a custom type is not exposed yet through a ``py::class_`` constructor or a custom type caster,
+its C++ type name will be used instead to generate the signature in the docstring:
+
+.. code-block:: text
+
+ | __init__(...)
+ | __init__(self: example.Foo, arg0: ns::Bar) -> None
+ ^^^^^^^
+
+
+This limitation can be circumvented by ensuring that C++ classes are registered with pybind11
+before they are used as a parameter or return type of a function:
+
+.. code-block:: cpp
+
+ PYBIND11_MODULE(example, m) {
+
+ auto pyFoo = py::class_<ns::Foo>(m, "Foo");
+ auto pyBar = py::class_<ns::Bar>(m, "Bar");
+
+ pyFoo.def(py::init<const ns::Bar&>());
+ pyBar.def(py::init<const ns::Foo&>());
+ }
diff --git a/3rdparty/pybind11/docs/advanced/pycpp/numpy.rst b/3rdparty/pybind11/docs/advanced/pycpp/numpy.rst
index 458f99e9..19ed10b3 100644
--- a/3rdparty/pybind11/docs/advanced/pycpp/numpy.rst
+++ b/3rdparty/pybind11/docs/advanced/pycpp/numpy.rst
@@ -57,17 +57,17 @@ specification.
struct buffer_info {
void *ptr;
- ssize_t itemsize;
+ py::ssize_t itemsize;
std::string format;
- ssize_t ndim;
- std::vector<ssize_t> shape;
- std::vector<ssize_t> strides;
+ py::ssize_t ndim;
+ std::vector<py::ssize_t> shape;
+ std::vector<py::ssize_t> strides;
};
To create a C++ function that can take a Python buffer object as an argument,
simply use the type ``py::buffer`` as one of its arguments. Buffers can exist
in a great variety of configurations, hence some safety checks are usually
-necessary in the function body. Below, you can see an basic example on how to
+necessary in the function body. Below, you can see a basic example on how to
define a custom constructor for the Eigen double precision matrix
(``Eigen::MatrixXd``) type, which supports initialization from compatible
buffer objects (e.g. a NumPy matrix).
@@ -81,7 +81,7 @@ buffer objects (e.g. a NumPy matrix).
constexpr bool rowMajor = Matrix::Flags & Eigen::RowMajorBit;
py::class_<Matrix>(m, "Matrix", py::buffer_protocol())
- .def("__init__", [](Matrix &m, py::buffer b) {
+ .def(py::init([](py::buffer b) {
typedef Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic> Strides;
/* Request a buffer descriptor from Python */
@@ -101,8 +101,8 @@ buffer objects (e.g. a NumPy matrix).
auto map = Eigen::Map<Matrix, 0, Strides>(
static_cast<Scalar *>(info.ptr), info.shape[0], info.shape[1], strides);
- new (&m) Matrix(map);
- });
+ return Matrix(map);
+ }));
For reference, the ``def_buffer()`` call for this Eigen data type should look
as follows:
@@ -274,9 +274,9 @@ simply using ``vectorize``).
py::buffer_info buf3 = result.request();
- double *ptr1 = (double *) buf1.ptr,
- *ptr2 = (double *) buf2.ptr,
- *ptr3 = (double *) buf3.ptr;
+ double *ptr1 = static_cast<double *>(buf1.ptr);
+ double *ptr2 = static_cast<double *>(buf2.ptr);
+ double *ptr3 = static_cast<double *>(buf3.ptr);
for (size_t idx = 0; idx < buf1.shape[0]; idx++)
ptr3[idx] = ptr1[idx] + ptr2[idx];
@@ -309,17 +309,17 @@ where ``N`` gives the required dimensionality of the array:
m.def("sum_3d", [](py::array_t<double> x) {
auto r = x.unchecked<3>(); // x must have ndim = 3; can be non-writeable
double sum = 0;
- for (ssize_t i = 0; i < r.shape(0); i++)
- for (ssize_t j = 0; j < r.shape(1); j++)
- for (ssize_t k = 0; k < r.shape(2); k++)
+ for (py::ssize_t i = 0; i < r.shape(0); i++)
+ for (py::ssize_t j = 0; j < r.shape(1); j++)
+ for (py::ssize_t k = 0; k < r.shape(2); k++)
sum += r(i, j, k);
return sum;
});
m.def("increment_3d", [](py::array_t<double> x) {
auto r = x.mutable_unchecked<3>(); // Will throw if ndim != 3 or flags.writeable is false
- for (ssize_t i = 0; i < r.shape(0); i++)
- for (ssize_t j = 0; j < r.shape(1); j++)
- for (ssize_t k = 0; k < r.shape(2); k++)
+ for (py::ssize_t i = 0; i < r.shape(0); i++)
+ for (py::ssize_t j = 0; j < r.shape(1); j++)
+ for (py::ssize_t k = 0; k < r.shape(2); k++)
r(i, j, k) += 1.0;
}, py::arg().noconvert());
@@ -371,6 +371,8 @@ Ellipsis
Python 3 provides a convenient ``...`` ellipsis notation that is often used to
slice multidimensional arrays. For instance, the following snippet extracts the
middle dimensions of a tensor with the first and last index set to zero.
+In Python 2, the syntactic sugar ``...`` is not available, but the singleton
+``Ellipsis`` (of type ``ellipsis``) can still be used directly.
.. code-block:: python
@@ -384,3 +386,51 @@ operation on the C++ side:
py::array a = /* A NumPy array */;
py::array b = a[py::make_tuple(0, py::ellipsis(), 0)];
+
+.. versionchanged:: 2.6
+ ``py::ellipsis()`` is now also avaliable in Python 2.
+
+Memory view
+===========
+
+For a case when we simply want to provide a direct accessor to C/C++ buffer
+without a concrete class object, we can return a ``memoryview`` object. Suppose
+we wish to expose a ``memoryview`` for 2x4 uint8_t array, we can do the
+following:
+
+.. code-block:: cpp
+
+ const uint8_t buffer[] = {
+ 0, 1, 2, 3,
+ 4, 5, 6, 7
+ };
+ m.def("get_memoryview2d", []() {
+ return py::memoryview::from_buffer(
+ buffer, // buffer pointer
+ { 2, 4 }, // shape (rows, cols)
+ { sizeof(uint8_t) * 4, sizeof(uint8_t) } // strides in bytes
+ );
+ })
+
+This approach is meant for providing a ``memoryview`` for a C/C++ buffer not
+managed by Python. The user is responsible for managing the lifetime of the
+buffer. Using a ``memoryview`` created in this way after deleting the buffer in
+C++ side results in undefined behavior.
+
+We can also use ``memoryview::from_memory`` for a simple 1D contiguous buffer:
+
+.. code-block:: cpp
+
+ m.def("get_memoryview1d", []() {
+ return py::memoryview::from_memory(
+ buffer, // buffer pointer
+ sizeof(uint8_t) * 8 // buffer size
+ );
+ })
+
+.. note::
+
+ ``memoryview::from_memory`` is not available in Python 2.
+
+.. versionchanged:: 2.6
+ ``memoryview::from_memory`` added.
diff --git a/3rdparty/pybind11/docs/advanced/pycpp/object.rst b/3rdparty/pybind11/docs/advanced/pycpp/object.rst
index 117131ed..6c7525ce 100644
--- a/3rdparty/pybind11/docs/advanced/pycpp/object.rst
+++ b/3rdparty/pybind11/docs/advanced/pycpp/object.rst
@@ -1,6 +1,8 @@
Python types
############
+.. _wrappers:
+
Available wrappers
==================
@@ -13,6 +15,13 @@ Available types include :class:`handle`, :class:`object`, :class:`bool_`,
:class:`iterable`, :class:`iterator`, :class:`function`, :class:`buffer`,
:class:`array`, and :class:`array_t`.
+.. warning::
+
+ Be sure to review the :ref:`pytypes_gotchas` before using this heavily in
+ your C++ API.
+
+.. _casting_back_and_forth:
+
Casting back and forth
======================
@@ -47,20 +56,21 @@ This example obtains a reference to the Python ``Decimal`` class.
.. code-block:: cpp
// Equivalent to "from decimal import Decimal"
- py::object Decimal = py::module::import("decimal").attr("Decimal");
+ py::object Decimal = py::module_::import("decimal").attr("Decimal");
.. code-block:: cpp
// Try to import scipy
- py::object scipy = py::module::import("scipy");
+ py::object scipy = py::module_::import("scipy");
return scipy.attr("__version__");
+
.. _calling_python_functions:
Calling Python functions
========================
-It is also possible to call Python classes, functions and methods
+It is also possible to call Python classes, functions and methods
via ``operator()``.
.. code-block:: cpp
@@ -71,11 +81,11 @@ via ``operator()``.
.. code-block:: cpp
// Use Python to make our directories
- py::object os = py::module::import("os");
+ py::object os = py::module_::import("os");
py::object makedirs = os.attr("makedirs");
makedirs("/tmp/path/to/somewhere");
-One can convert the result obtained from Python to a pure C++ version
+One can convert the result obtained from Python to a pure C++ version
if a ``py::class_`` or type conversion is defined.
.. code-block:: cpp
@@ -99,8 +109,8 @@ Python method.
py::print(py::str(exp_pi));
In the example above ``pi.attr("exp")`` is a *bound method*: it will always call
-the method for that same instance of the class. Alternately one can create an
-*unbound method* via the Python class (instead of instance) and pass the ``self``
+the method for that same instance of the class. Alternately one can create an
+*unbound method* via the Python class (instead of instance) and pass the ``self``
object explicitly, followed by other arguments.
.. code-block:: cpp
@@ -168,3 +178,74 @@ Generalized unpacking according to PEP448_ is also supported:
Python functions from C++, including keywords arguments and unpacking.
.. _PEP448: https://www.python.org/dev/peps/pep-0448/
+
+.. _implicit_casting:
+
+Implicit casting
+================
+
+When using the C++ interface for Python types, or calling Python functions,
+objects of type :class:`object` are returned. It is possible to invoke implicit
+conversions to subclasses like :class:`dict`. The same holds for the proxy objects
+returned by ``operator[]`` or ``obj.attr()``.
+Casting to subtypes improves code readability and allows values to be passed to
+C++ functions that require a specific subtype rather than a generic :class:`object`.
+
+.. code-block:: cpp
+
+ #include <pybind11/numpy.h>
+ using namespace pybind11::literals;
+
+ py::module_ os = py::module_::import("os");
+ py::module_ path = py::module_::import("os.path"); // like 'import os.path as path'
+ py::module_ np = py::module_::import("numpy"); // like 'import numpy as np'
+
+ py::str curdir_abs = path.attr("abspath")(path.attr("curdir"));
+ py::print(py::str("Current directory: ") + curdir_abs);
+ py::dict environ = os.attr("environ");
+ py::print(environ["HOME"]);
+ py::array_t<float> arr = np.attr("ones")(3, "dtype"_a="float32");
+ py::print(py::repr(arr + py::int_(1)));
+
+These implicit conversions are available for subclasses of :class:`object`; there
+is no need to call ``obj.cast()`` explicitly as for custom classes, see
+:ref:`casting_back_and_forth`.
+
+.. note::
+ If a trivial conversion via move constructor is not possible, both implicit and
+ explicit casting (calling ``obj.cast()``) will attempt a "rich" conversion.
+ For instance, ``py::list env = os.attr("environ");`` will succeed and is
+ equivalent to the Python code ``env = list(os.environ)`` that produces a
+ list of the dict keys.
+
+.. TODO: Adapt text once PR #2349 has landed
+
+Handling exceptions
+===================
+
+Python exceptions from wrapper classes will be thrown as a ``py::error_already_set``.
+See :ref:`Handling exceptions from Python in C++
+<handling_python_exceptions_cpp>` for more information on handling exceptions
+raised when calling C++ wrapper classes.
+
+.. _pytypes_gotchas:
+
+Gotchas
+=======
+
+Default-Constructed Wrappers
+----------------------------
+
+When a wrapper type is default-constructed, it is **not** a valid Python object (i.e. it is not ``py::none()``). It is simply the same as
+``PyObject*`` null pointer. To check for this, use
+``static_cast<bool>(my_wrapper)``.
+
+Assigning py::none() to wrappers
+--------------------------------
+
+You may be tempted to use types like ``py::str`` and ``py::dict`` in C++
+signatures (either pure C++, or in bound signatures), and assign them default
+values of ``py::none()``. However, in a best case scenario, it will fail fast
+because ``None`` is not convertible to that type (e.g. ``py::dict``), or in a
+worse case scenario, it will silently work but corrupt the types you want to
+work with (e.g. ``py::str(py::none())`` will yield ``"None"`` in Python).
diff --git a/3rdparty/pybind11/docs/advanced/pycpp/utilities.rst b/3rdparty/pybind11/docs/advanced/pycpp/utilities.rst
index 369e7c94..c15051fb 100644
--- a/3rdparty/pybind11/docs/advanced/pycpp/utilities.rst
+++ b/3rdparty/pybind11/docs/advanced/pycpp/utilities.rst
@@ -42,7 +42,7 @@ redirects output to the corresponding Python streams:
m.def("noisy_func", []() {
py::scoped_ostream_redirect stream(
std::cout, // std::ostream&
- py::module::import("sys").attr("stdout") // Python output
+ py::module_::import("sys").attr("stdout") // Python output
);
call_noisy_func();
});
@@ -104,7 +104,7 @@ can be used.
...
// Evaluate in scope of main module
- py::object scope = py::module::import("__main__").attr("__dict__");
+ py::object scope = py::module_::import("__main__").attr("__dict__");
// Evaluate an isolated expression
int result = py::eval("my_variable + 10", scope).cast<int>();