pybind / pybind/pybind11

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

Open
#2,969 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

(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!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal example using pybind11/embed.h, py::cast, mymodule, and myscript.py, then compare it with the real application's type-registration path. The issue names no source files or tests and provides no concrete change target; done would be identifying why MyClass is unregistered in the real application and documenting the diagnosis or required correction.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.