casting c++-base class to py-derived class failes
Open
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Issue description
Hello,
Consider a derived py-class (PClass) with a C++ class as base class (CClass).
If I want cast a CClass-Object to a PClass-Object, python gives me the following error:
TypeError: class assignment: 'PClass' deallocator differs from 'cpp_module.CClass'
I tested this with python3, clang-6.0, gcc 5.4, c++11 and c++17
Reproducible example code
#include <pybind11/pybind11.h>
class CClass {};
namespace py = pybind11;
PYBIND11_MODULE(cpp_module, m) {
py::class_<CClass>(m, "CClass").def(py::init<>());
}
working example with python
class PClassA():
def __init__(self):
pass
class PClassB(PClassA):
def __init__(self):
super().__init__()
# py -> py example
a_class = PClassA()
b_class = PClassB()
a_class.__class__ = PClassB # this works
failing example with C++
from cpp_module import CClass
class PClass(CClass):
def __init__(self):
super().__init__()
# c++ -> py example
c_class = CClass()
p_class = PClass()
c_class.__class__ = PClass # this failes
output
Traceback (most recent call last):
File "test.py", line 25, in <module>
c_class.__class__ = PClass # this failes
TypeError: __class__ assignment: 'PClass' deallocator differs from 'cpp_module.CClass'
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
The reproducible entry point is the CClass binding in pybind11/pybind11.h; first run the supplied C++ and Python examples to confirm the class assignment failure. Done means assigning CClass.class to PClass no longer raises the reported deallocator error.
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
- 35/100