-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Cannot pass arbitrary object as py::object #298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The real issue is that py::str doesn't convert the argument to a string, it simply attempts to cast it to a string. But that fails here, because what you are passing in obviously isn't a python string object, it's a What you want to do instead is explicitly call the .def( "bar", [](Foo& foo, py::object pathy) {
std::string path = py::str(((py::object) pathy.attr("__str__"))());
return foo.bar(path.c_str());
}
) |
Or just simply: std::string path = pathy.str(); |
Yes, that's much smarter (and better: it works as in Python for objects that define |
Works like a charm, thanks guys. |
I'll reopen this since it could serve as a nice example on providing a custom type_caster for an object that can be converted into a string. (i.e. a kind of filtered py::object) |
I have a function
bar
to which I want to allow to pass anything "stringy":Naively I thought I could pass arbitrary objects to
py::object
.When I do
foo_instance.bar( pathlib.Path("/path") )
it complains:I seem to have assumed something wrong.
Is there a way of passing arbitrary objects to C++?
The text was updated successfully, but these errors were encountered: