pybind / pybind11

Seamless operability between C++11 and Python
https://pybind11.readthedocs.io/
Other
15.66k stars 2.1k forks source link

[QUESTION] py::cast and "unregistered type" #2969

Open haianos opened 3 years ago

haianos commented 3 years ago

(After looking into the documentation, examples, etc I could not solve myself)

I have a small module mymodule which does wrap my class MyClass and seems to work fine in basic tests.

Then I want to use this module embedding python with pybind11/embed.h and passing from C++ to python an object instance of MyClass that will be used in a script loaded later on in my application.

The following simple snipped is what I used for try things out

  py::scoped_interpreter guard{};
  auto obj = std::make_shared<MyClass>(MyClass());

  py::object mm = py::module::import("mymodule").attr("__dict__");
  py::object pyobj = py::cast(obj, py::return_value_policy::reference);
  mm["obj"] = pyobj;
  py::eval_file("myscript.py"); // this script uses obj

and it seems to work fine.

However, when I apply this in my "real" application, when mm["obj"] = pyobj I get

terminate called after throwing an instance of 'pybind11::error_already_set'
  what():  TypeError: Unregistered type : MyClass

and I can't figure it out why this is the case? Does anyone have a tips/hint to investigate this...?

Thanks!

matusnovak commented 1 year ago

I am having the exact same problem and I am unable to figure out what is wrong.