diff --git a/include/pybind11/detail/class.h b/include/pybind11/detail/class.h index bc2b40c50a..057a867bb7 100644 --- a/include/pybind11/detail/class.h +++ b/include/pybind11/detail/class.h @@ -184,6 +184,7 @@ extern "C" inline PyObject *pybind11_meta_getattro(PyObject *obj, PyObject *name extern "C" inline PyObject *pybind11_meta_call(PyObject *type, PyObject *args, PyObject *kwargs) { // use the default metaclass call to create/initialize the object +printf("\nLOOOK %s:%d\n", __FILE__, __LINE__); fflush(stdout); PyObject *self = PyType_Type.tp_call(type, args, kwargs); if (self == nullptr) { return nullptr; @@ -366,7 +367,10 @@ inline PyObject *make_new_instance(PyTypeObject *type) { /// Instance creation function for all pybind11 types. It only allocates space for the /// C++ object, but doesn't call the constructor -- an `__init__` function must do that. extern "C" inline PyObject *pybind11_object_new(PyTypeObject *type, PyObject *, PyObject *) { - return make_new_instance(type); +printf("\nLOOOK [pybind11_object_new (called via tp_new) %s:%d\n", __FILE__, __LINE__); fflush(stdout); + PyObject *retval = make_new_instance(type); +printf("\nLOOOK ]pybind11_object_new (called via tp_new) %s:%d\n", __FILE__, __LINE__); fflush(stdout); + return retval; } /// An `__init__` function constructs the C++ object. Users should provide at least one diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index 16387506cf..5e182cb4dd 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -104,14 +104,16 @@ all_type_info_get_cache(PyTypeObject *type); // Populates a just-created cache entry. PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vector &bases) { +printf("\nLOOOK all_type_info_populate[ %s:%d\n", __FILE__, __LINE__); fflush(stdout); std::vector check; for (handle parent : reinterpret_borrow(t->tp_bases)) { check.push_back((PyTypeObject *) parent.ptr()); } +printf("\nLOOOK:REG:POP tp_name=%s check.size()=%lu %s:%d\n", t->tp_name, (unsigned long) check.size(), __FILE__, __LINE__); fflush(stdout); auto const &type_dict = get_internals().registered_types_py; for (size_t i = 0; i < check.size(); i++) { - auto *type = check[i]; + PyTypeObject *type = check[i]; // Ignore Python2 old-style class super type: if (!PyType_Check((PyObject *) type)) { continue; @@ -119,23 +121,25 @@ PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vectortp_name, (it != type_dict.end() ? "yes" : "no"), __FILE__, __LINE__); fflush(stdout); if (it != type_dict.end()) { // We found a cache entry for it, so it's either pybind-registered or has pre-computed // pybind bases, but we have to make sure we haven't already seen the type(s) before: // we want to follow Python/virtual C++ rules that there should only be one instance of // a common base. - for (auto *tinfo : it->second) { + for (type_info *tinfo : it->second) { // NB: Could use a second set here, rather than doing a linear search, but since // having a large number of immediate pybind11-registered types seems fairly // unlikely, that probably isn't worthwhile. bool found = false; - for (auto *known : bases) { + for (type_info *known : bases) { if (known == tinfo) { found = true; break; } } if (!found) { +printf("\nLOOOK:REG:ADD bases.push_back(tinfo) %s %s:%d\n", tinfo->cpptype->name(), __FILE__, __LINE__); fflush(stdout); bases.push_back(tinfo); } } @@ -154,6 +158,7 @@ PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vectorsimple_layout ? inst->simple_value_holder - : &inst->nonsimple.values_and_holders[vpos]} {} + : &inst->nonsimple.values_and_holders[vpos]} { +if (type && type->cpptype) { +const char *nm = type->cpptype->name(); +if (strcmp(nm, "N32test_python_multiple_inheritance7CppBaseE") == 0 || strcmp(nm, "N32test_python_multiple_inheritance7CppDrvdE") == 0) { +printf("\nLOOOK %s value_and_holder ctor %s:%d\n", nm, __FILE__, __LINE__); fflush(stdout); +} +} + } // Default constructor (used to signal a value-and-holder not found by get_value_and_holder()) value_and_holder() = default; @@ -286,11 +298,18 @@ struct value_and_holder { } // NOLINTNEXTLINE(readability-make-member-function-const) void set_holder_constructed(bool v = true) { +//printf("\nLOOOK set_holder_constructed inst=%lu %s %s:%d\n", reinterpret_cast(inst), type->cpptype->name(), __FILE__, __LINE__); fflush(stdout); +if (strcmp("N32test_python_multiple_inheritance7CppBaseE", type->cpptype->name()) == 0) { + //long *BAD = nullptr; *BAD = 101; +} if (inst->simple_layout) { +//printf("\nLOOOK %s set_holder_constructed simple_layout %s:%d\n", type->cpptype->name(), __FILE__, __LINE__); fflush(stdout); inst->simple_holder_constructed = v; } else if (v) { +//printf("\nLOOOK %s set_holder_constructed v %s:%d\n", type->cpptype->name(), __FILE__, __LINE__); fflush(stdout); inst->nonsimple.status[index] |= instance::status_holder_constructed; } else { +//printf("\nLOOOK %s set_holder_constructed not v %s:%d\n", type->cpptype->name(), __FILE__, __LINE__); fflush(stdout); inst->nonsimple.status[index] &= (std::uint8_t) ~instance::status_holder_constructed; } } diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 3bce1a01ba..1acde6f461 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1142,8 +1142,10 @@ class cpp_function : public function { return nullptr; } if (overloads->is_constructor && !self_value_and_holder.holder_constructed()) { +printf("\nLOOOK %s BEFORE self_value_and_holder.type->init_instance %s:%d\n", self_value_and_holder.type->cpptype->name(), __FILE__, __LINE__); fflush(stdout); auto *pi = reinterpret_cast(parent.ptr()); self_value_and_holder.type->init_instance(pi, nullptr); +printf("\nLOOOK %s AFTER self_value_and_holder.type->init_instance %s:%d\n", self_value_and_holder.type->cpptype->name(), __FILE__, __LINE__); fflush(stdout); } return result.ptr(); } diff --git a/tests/test_python_multiple_inheritance.py b/tests/test_python_multiple_inheritance.py index cdd34d9ceb..035a14b9ff 100644 --- a/tests/test_python_multiple_inheritance.py +++ b/tests/test_python_multiple_inheritance.py @@ -10,13 +10,28 @@ class PC(m.CppBase): class PPCCInit(PC, m.CppDrvd): def __init__(self, value): + print("\nLOOOK PPCCInit PC", flush=True) PC.__init__(self, value) + print("\nLOOOK PPCCInit CppDrvd", flush=True) m.CppDrvd.__init__(self, value + 1) + print("\nLOOOK PPCCInit Done", flush=True) + + +def NOtest_PC_AAA(): + print("\nLOOOK BEFORE PC(11) AAA", flush=True) + d = PC(11) + print("\nLOOOK AFTER PC(11) AAA", flush=True) + assert d.get_base_value() == 11 + d.reset_base_value(13) + assert d.get_base_value() == 13 # Moving this test after test_PC() changes the behavior! -def test_PPCCInit(): +def test_PPCCInit_BBB(): + print("\nLOOOK BEFORE PPCCInit(11) BBB", flush=True) d = PPCCInit(11) + print("\nLOOOK AFTER PPCCInit(11) BBB", flush=True) + print("\nLOOOK", flush=True) assert d.get_drvd_value() == 36 d.reset_drvd_value(55) assert d.get_drvd_value() == 55 @@ -31,8 +46,29 @@ def test_PPCCInit(): assert d.get_base_value_from_drvd() == 30 -def test_PC(): +def NOtest_PC_CCC(): + print("\nLOOOK BEFORE PC(11) CCC", flush=True) d = PC(11) + print("\nLOOOK AFTER PC(11) CCC", flush=True) assert d.get_base_value() == 11 d.reset_base_value(13) assert d.get_base_value() == 13 + +# Moving this test after test_PC() changes the behavior! +def NOtest_PPCCInit_DDD(): + print("\nLOOOK BEFORE PPCCInit(11) DDD", flush=True) + d = PPCCInit(11) + print("\nLOOOK AFTER PPCCInit(11) DDD", flush=True) + print("\nLOOOK", flush=True) + assert d.get_drvd_value() == 36 + d.reset_drvd_value(55) + assert d.get_drvd_value() == 55 + + assert d.get_base_value() == 12 + assert d.get_base_value_from_drvd() == 12 + d.reset_base_value(20) + assert d.get_base_value() == 20 + assert d.get_base_value_from_drvd() == 20 + d.reset_base_value_from_drvd(30) + assert d.get_base_value() == 30 + assert d.get_base_value_from_drvd() == 30