Skip to content

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

Closed
abergmeier-dsfishlabs opened this issue Jul 28, 2016 · 5 comments
Closed

Cannot pass arbitrary object as py::object #298

abergmeier-dsfishlabs opened this issue Jul 28, 2016 · 5 comments

Comments

@abergmeier-dsfishlabs
Copy link
Contributor

I have a function bar to which I want to allow to pass anything "stringy":

py::class_< Foo >( m, "Foo" )
  ...
  .def( "bar", [](Foo& foo, py::object pathy) {
            auto const path = py::str(pathy).cast<std::string>();
            return foo.bar(path.c_str());
        }
  )
  ...

Naively I thought I could pass arbitrary objects to py::object.
When I do foo_instance.bar( pathlib.Path("/path") ) it complains:

TypeError: Incompatible function arguments. The following argument types are supported:
1. (module.Foo, object) -> Ret
Invoked with: <module.Foo object at 0x7fc175b2e3b0>, /path

I seem to have assumed something wrong.
Is there a way of passing arbitrary objects to C++?

@jagerman
Copy link
Member

py::object is working just as you expect—the error you're getting is misleading you here. (That's been fixed in master, which gives the more helpful error message RuntimeError: Unable to cast Python instance to C++ type (compile in debug mode for details) rather than an argument error; that change isn't in 1.8.1 however).

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 pathlib.Path object.

What you want to do instead is explicitly call the __str__ method of that object, which will give you back the string you expect:

  .def( "bar", [](Foo& foo, py::object pathy) {
            std::string path = py::str(((py::object) pathy.attr("__str__"))());
            return foo.bar(path.c_str());
        }
  )

@aldanor
Copy link
Member

aldanor commented Jul 28, 2016

Or just simply:

std::string path = pathy.str();

@jagerman
Copy link
Member

jagerman commented Jul 29, 2016

Yes, that's much smarter (and better: it works as in Python for objects that define __repr__ but not __str__).

@abergmeier-dsfishlabs
Copy link
Contributor Author

std::string path = pathy.str();

Works like a charm, thanks guys.

@wjakob
Copy link
Member

wjakob commented Aug 1, 2016

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants