mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-26 15:12:01 +00:00
373da82486
Currently types that are capable of conversion always call their convert function when invoked with a `py::object` which is actually the correct type. This means that code such as `py::cast<py::list>(obj)` and `py::list l(obj.attr("list"))` make copies, which was an oversight rather than an intentional feature. While at first glance there might be something behind having `py::list(obj)` make a copy (as it would in Python), this would be inconsistent when you dig a little deeper because `py::list(l)` *doesn't* make a copy for an existing `py::list l`, and having an inconsistency within C++ would be worse than a C++ <-> Python inconsistency. It is possible to get around the copying using a `reinterpret_borrow<list>(o)` (and this commit fixes one place, in `embed.h`, that does so), but that seems a misuse of `reinterpret_borrow`, which is really supposed to be just for dealing with raw python-returned values, not `py::object`-derived wrappers which are supposed to be higher level. This changes the constructor of such converting types (i.e. anything using PYBIND11_OBJECT_CVT -- `str`, `bool_`, `int_`, `float_`, `tuple`, `dict`, `list`, `set`, `memoryview`) to reference rather than copy when the check function passes. It also adds an `object &&` constructor that is slightly more efficient by avoiding an inc_ref when the check function passes. |
||
---|---|---|
.. | ||
pybind11 |