ITADN

[BUG]: Objects created using __new__ are not registered for casting

#6025OpenTheDreadedAndy 创建于 2026-03-30
triage
T
TheDreadedAndycommented
### Required prerequisites - [x] Make sure you've read the [documentation](https://pybind11.readthedocs.io). Your issue may be addressed there. - [x] Search the [issue tracker](https://github.com/pybind/pybind11/issues) and [Discussions](https:/pybind/pybind11/discussions) to verify that this hasn't already been reported. +1 or comment there if it has. - [x] Consider asking first in the [Gitter chat room](https://gitter.im/pybind/Lobby) or in a [Discussion](https:/pybind/pybind11/discussions/new). ### What version (or hash if on master) of pybind11 are you using? 3.0.1 ### Problem description When a pybind-based object or a subclass of one is created using new, it is not added to the registry of pybind objects for casting. If the object has methods than require casting to return the correct type information for its actual subtype, then this silently breaks those methods. This is somewhat related to [issue #1693](https://github.com/pybind/pybind11/issues/1693), as using the listed workaround is how I found this problem. ### Reproducible example code ```text Suppose we have: #include <pybind11/pybind11.h> using namespace py = pybind11; struct Num { double n; Num(double n) : n(n) {} inline Num Mult2() { return Num(2 * n) } } PYBIND11_MODULE(this_example, m) { py::class_<Num> pyNum(m, "Num"); pyNum .def(py::init([]() { return Num(0); })) .def("mult2", [](const Num *self) { py::object selfObj = py::cast(self); PyTypeObject *const ty = Py_TYPE(selfObj.ptr()); auto newObj = py::reinterpret_steal<py::object>(ty->tp_new(ty, py::list().ptr(), py::dict().ptr())); *newObj.cast<Num*>() = self->Mult2(); return newObj; }) .def_readwrite("n", &Num::n); } from this_example import Num class MoreNum(Num): def mult4(self): ret = self.__class__.__new__(self.__class__) ret.n = self.n * self.n return ret # Works, but returned object is not registered. MoreNum().mult2() MoreNum().mult4() MoreNum().mult2().mult4() # mult4 is not implemented on Num MoreNum().mult2().mult2().mult4() ``` ### Is this a regression? Put the last known working version here if it is. Not a regression
0 条评论