Cannot register a class with an abstract method.
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Hi,
I am trying to register a class that has an abstract method. The idea is for clients to define the method in their subclasses. However, the registration fails.
Before I forget, I am running on Ubuntu 16.04, using pybind11 version 2.2.2, python 2.7.12, g++ version 5.4.0, and -std=c++11.
Here is what I am trying to do:
py::module abc = py::module::import("abc");
py::object abc_meta = abc.attr("ABCMeta");
py::object abstract = abc.attr("abstractmethod");
py::module mod{"example"};
py::class_<Base> base(mod, "Base", py::metaclass(abc_meta));
base.def("draw", &Base::draw, "Abstract method that subclasses must define");
base.attr("draw") = abstract(base.attr("draw"));
base.def("save", &Base::save, "Save into file");
Here, I have a class that offers the save() function. But I require clients to create a subclass and define the draw() function.
When I compile and run the code, I get the following error:
terminate called after throwing an instance of 'pybind11::error_already_set'
what(): AttributeError: type object 'example.Base' has no attribute '_abc_cache'
At:
/usr/lib/python2.7/abc.py(151): __subclasscheck__
Since ABCMeta is trying to add _abc_cache and other attributes to my base class, I thought adding dynamic_attr() might help. I get the same error message with the following:
py::class_<Base> base(mod, "Base", py::metaclass(abc_meta), py::dynamic_attr());
I would appreciate any help on this. Thank you.
In the attached file, I am embedding the python interpreter so that I do not need a python script to import my example module.
main.cpp.txt
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the failure with the attached main.cpp.txt under the stated Python 2.7, C++11, and pybind11 2.2.2 environment. Trace the ABCMeta registration path that raises the missing _abc_cache error and verify behavior with the abstract draw() and concrete save() methods; done means the example registers successfully while preserving the abstract-method requirement.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, python
- Domain
- backend-api-design
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100