mirror of
https://github.com/pybind/pybind11.git
synced 2026-06-04 13:34:24 +08:00
Enable defining custom __new__ (#3265)
* Enable defining custom __new__ * See if xfail needed * Qualify auto self * Unconditionally defining PYBIND11_DISABLE_NEW_STYLE_INIT_WARNING. Returning pointer from "__init__" instead of reference. * Use new style __init__ * Simplify __new__ creation * Reviewer suggestions * Match indentation Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
This commit is contained in:
@@ -47,10 +47,25 @@ TEST_SUBMODULE(class_, m) {
|
||||
}
|
||||
~NoConstructor() { print_destroyed(this); }
|
||||
};
|
||||
struct NoConstructorNew {
|
||||
NoConstructorNew() = default;
|
||||
NoConstructorNew(const NoConstructorNew &) = default;
|
||||
NoConstructorNew(NoConstructorNew &&) = default;
|
||||
static NoConstructorNew *new_instance() {
|
||||
auto *ptr = new NoConstructorNew();
|
||||
print_created(ptr, "via new_instance");
|
||||
return ptr;
|
||||
}
|
||||
~NoConstructorNew() { print_destroyed(this); }
|
||||
};
|
||||
|
||||
py::class_<NoConstructor>(m, "NoConstructor")
|
||||
.def_static("new_instance", &NoConstructor::new_instance, "Return an instance");
|
||||
|
||||
py::class_<NoConstructorNew>(m, "NoConstructorNew")
|
||||
.def(py::init([](NoConstructorNew *self) { return self; })) // Need a NOOP __init__
|
||||
.def_static("__new__", [](const py::object *) { return NoConstructorNew::new_instance(); } );
|
||||
|
||||
// test_inheritance
|
||||
class Pet {
|
||||
public:
|
||||
|
||||
Reference in New Issue
Block a user