Skip to content

Commit

Permalink
Snapshot of debugging code (does NOT pass pre-commit checks).
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Jul 27, 2023
1 parent 8ea1b8c commit 1d4f9ff
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 7 deletions.
6 changes: 5 additions & 1 deletion include/pybind11/detail/class.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
27 changes: 23 additions & 4 deletions include/pybind11/detail/type_caster_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,38 +104,42 @@ all_type_info_get_cache(PyTypeObject *type);

// Populates a just-created cache entry.
PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vector<type_info *> &bases) {
printf("\nLOOOK all_type_info_populate[ %s:%d\n", __FILE__, __LINE__); fflush(stdout);
std::vector<PyTypeObject *> check;
for (handle parent : reinterpret_borrow<tuple>(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;
}

// Check `type` in the current set of registered python types:
auto it = type_dict.find(type);
printf("\nLOOOK type_dict.find(type) tp_name=%s found=%s %s:%d\n", type->tp_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);
}
}
Expand All @@ -154,6 +158,7 @@ PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vector<type_
}
}
}
printf("\nLOOOK all_type_info_populate] %s:%d\n", __FILE__, __LINE__); fflush(stdout);
}

/**
Expand Down Expand Up @@ -260,7 +265,14 @@ struct value_and_holder {
value_and_holder(instance *i, const detail::type_info *type, size_t vpos, size_t index)
: inst{i}, index{index}, type{type},
vh{inst->simple_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;
Expand All @@ -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<unsigned long>(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;
}
}
Expand Down
2 changes: 2 additions & 0 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<instance *>(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();
}
Expand Down
40 changes: 38 additions & 2 deletions tests/test_python_multiple_inheritance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 1d4f9ff

Please sign in to comment.