You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a C++ function, let's call it makeStuff() that returns a vector of object of a certain class, say A.
However it doesn't truly return a vector of A, but instead a vector of unique pointer to A.
This allows the vector to be filled with objects which are not A, but a subclass of A, say B.
However, if I expose makeStuff, A and B, pybind11 fails to detect that some of the items in the return value of makeStuff are actually B and not A.
Which that the python code considers every object in the converted list to be of type A, regardless of the actual type pointed to in the C++ vector.
I would have loved that using py::list directly in C++ and return that, but even though there's plethora of documentation on how to cast a py::object to a pure C++ type using py::cast, there no documentation about the inverse: how to wrap in C++ a concrete C++ object into a py::object.
So I guess I would like to...
...either have a flag to tell pybind11 to detect the actually types of object pointed by unique_ptr's or shared_ptr's, see if it has a type registered for that concrete type, and prefer to use that type to wrap around the object.
...or have a simple and well-documented way to cast form a custom C++ type registered with py::class_ to a py::object, move-only if needed.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello !
I have a C++ function, let's call it
makeStuff()
that returns a vector of object of a certain class, sayA
.However it doesn't truly return a vector of
A
, but instead a vector of unique pointer toA
.This allows the vector to be filled with objects which are not
A
, but a subclass ofA
, sayB
.However, if I expose
makeStuff
,A
andB
, pybind11 fails to detect that some of the items in the return value ofmakeStuff
are actuallyB
and notA
.Which that the python code considers every object in the converted list to be of type
A
, regardless of the actual type pointed to in the C++ vector.I would have loved that using
py::list
directly in C++ and return that, but even though there's plethora of documentation on how to cast apy::object
to a pure C++ type usingpy::cast
, there no documentation about the inverse: how to wrap in C++ a concrete C++ object into apy::object
.So I guess I would like to...
...either have a flag to tell pybind11 to detect the actually types of object pointed by
unique_ptr
's orshared_ptr
's, see if it has a type registered for that concrete type, and prefer to use that type to wrap around the object....or have a simple and well-documented way to cast form a custom C++ type registered with
py::class_
to apy::object
, move-only if needed.Below is a minimal reproduction of the issue:
CMakeLists.txt:
stackoverflowexample.cpp
the actual python call:
what I would like to have in python:
Beta Was this translation helpful? Give feedback.
All reactions