Skip to content

Commit 1d4f9ff

Browse files
committed
Snapshot of debugging code (does NOT pass pre-commit checks).
1 parent 8ea1b8c commit 1d4f9ff

File tree

4 files changed

+68
-7
lines changed

4 files changed

+68
-7
lines changed

include/pybind11/detail/class.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ extern "C" inline PyObject *pybind11_meta_getattro(PyObject *obj, PyObject *name
184184
extern "C" inline PyObject *pybind11_meta_call(PyObject *type, PyObject *args, PyObject *kwargs) {
185185

186186
// use the default metaclass call to create/initialize the object
187+
printf("\nLOOOK %s:%d\n", __FILE__, __LINE__); fflush(stdout);
187188
PyObject *self = PyType_Type.tp_call(type, args, kwargs);
188189
if (self == nullptr) {
189190
return nullptr;
@@ -366,7 +367,10 @@ inline PyObject *make_new_instance(PyTypeObject *type) {
366367
/// Instance creation function for all pybind11 types. It only allocates space for the
367368
/// C++ object, but doesn't call the constructor -- an `__init__` function must do that.
368369
extern "C" inline PyObject *pybind11_object_new(PyTypeObject *type, PyObject *, PyObject *) {
369-
return make_new_instance(type);
370+
printf("\nLOOOK [pybind11_object_new (called via tp_new) %s:%d\n", __FILE__, __LINE__); fflush(stdout);
371+
PyObject *retval = make_new_instance(type);
372+
printf("\nLOOOK ]pybind11_object_new (called via tp_new) %s:%d\n", __FILE__, __LINE__); fflush(stdout);
373+
return retval;
370374
}
371375

372376
/// An `__init__` function constructs the C++ object. Users should provide at least one

include/pybind11/detail/type_caster_base.h

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,38 +104,42 @@ all_type_info_get_cache(PyTypeObject *type);
104104

105105
// Populates a just-created cache entry.
106106
PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vector<type_info *> &bases) {
107+
printf("\nLOOOK all_type_info_populate[ %s:%d\n", __FILE__, __LINE__); fflush(stdout);
107108
std::vector<PyTypeObject *> check;
108109
for (handle parent : reinterpret_borrow<tuple>(t->tp_bases)) {
109110
check.push_back((PyTypeObject *) parent.ptr());
110111
}
112+
printf("\nLOOOK:REG:POP tp_name=%s check.size()=%lu %s:%d\n", t->tp_name, (unsigned long) check.size(), __FILE__, __LINE__); fflush(stdout);
111113

112114
auto const &type_dict = get_internals().registered_types_py;
113115
for (size_t i = 0; i < check.size(); i++) {
114-
auto *type = check[i];
116+
PyTypeObject *type = check[i];
115117
// Ignore Python2 old-style class super type:
116118
if (!PyType_Check((PyObject *) type)) {
117119
continue;
118120
}
119121

120122
// Check `type` in the current set of registered python types:
121123
auto it = type_dict.find(type);
124+
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);
122125
if (it != type_dict.end()) {
123126
// We found a cache entry for it, so it's either pybind-registered or has pre-computed
124127
// pybind bases, but we have to make sure we haven't already seen the type(s) before:
125128
// we want to follow Python/virtual C++ rules that there should only be one instance of
126129
// a common base.
127-
for (auto *tinfo : it->second) {
130+
for (type_info *tinfo : it->second) {
128131
// NB: Could use a second set here, rather than doing a linear search, but since
129132
// having a large number of immediate pybind11-registered types seems fairly
130133
// unlikely, that probably isn't worthwhile.
131134
bool found = false;
132-
for (auto *known : bases) {
135+
for (type_info *known : bases) {
133136
if (known == tinfo) {
134137
found = true;
135138
break;
136139
}
137140
}
138141
if (!found) {
142+
printf("\nLOOOK:REG:ADD bases.push_back(tinfo) %s %s:%d\n", tinfo->cpptype->name(), __FILE__, __LINE__); fflush(stdout);
139143
bases.push_back(tinfo);
140144
}
141145
}
@@ -154,6 +158,7 @@ PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vector<type_
154158
}
155159
}
156160
}
161+
printf("\nLOOOK all_type_info_populate] %s:%d\n", __FILE__, __LINE__); fflush(stdout);
157162
}
158163

159164
/**
@@ -260,7 +265,14 @@ struct value_and_holder {
260265
value_and_holder(instance *i, const detail::type_info *type, size_t vpos, size_t index)
261266
: inst{i}, index{index}, type{type},
262267
vh{inst->simple_layout ? inst->simple_value_holder
263-
: &inst->nonsimple.values_and_holders[vpos]} {}
268+
: &inst->nonsimple.values_and_holders[vpos]} {
269+
if (type && type->cpptype) {
270+
const char *nm = type->cpptype->name();
271+
if (strcmp(nm, "N32test_python_multiple_inheritance7CppBaseE") == 0 || strcmp(nm, "N32test_python_multiple_inheritance7CppDrvdE") == 0) {
272+
printf("\nLOOOK %s value_and_holder ctor %s:%d\n", nm, __FILE__, __LINE__); fflush(stdout);
273+
}
274+
}
275+
}
264276

265277
// Default constructor (used to signal a value-and-holder not found by get_value_and_holder())
266278
value_and_holder() = default;
@@ -286,11 +298,18 @@ struct value_and_holder {
286298
}
287299
// NOLINTNEXTLINE(readability-make-member-function-const)
288300
void set_holder_constructed(bool v = true) {
301+
//printf("\nLOOOK set_holder_constructed inst=%lu %s %s:%d\n", reinterpret_cast<unsigned long>(inst), type->cpptype->name(), __FILE__, __LINE__); fflush(stdout);
302+
if (strcmp("N32test_python_multiple_inheritance7CppBaseE", type->cpptype->name()) == 0) {
303+
//long *BAD = nullptr; *BAD = 101;
304+
}
289305
if (inst->simple_layout) {
306+
//printf("\nLOOOK %s set_holder_constructed simple_layout %s:%d\n", type->cpptype->name(), __FILE__, __LINE__); fflush(stdout);
290307
inst->simple_holder_constructed = v;
291308
} else if (v) {
309+
//printf("\nLOOOK %s set_holder_constructed v %s:%d\n", type->cpptype->name(), __FILE__, __LINE__); fflush(stdout);
292310
inst->nonsimple.status[index] |= instance::status_holder_constructed;
293311
} else {
312+
//printf("\nLOOOK %s set_holder_constructed not v %s:%d\n", type->cpptype->name(), __FILE__, __LINE__); fflush(stdout);
294313
inst->nonsimple.status[index] &= (std::uint8_t) ~instance::status_holder_constructed;
295314
}
296315
}

include/pybind11/pybind11.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,8 +1142,10 @@ class cpp_function : public function {
11421142
return nullptr;
11431143
}
11441144
if (overloads->is_constructor && !self_value_and_holder.holder_constructed()) {
1145+
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);
11451146
auto *pi = reinterpret_cast<instance *>(parent.ptr());
11461147
self_value_and_holder.type->init_instance(pi, nullptr);
1148+
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);
11471149
}
11481150
return result.ptr();
11491151
}

tests/test_python_multiple_inheritance.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,28 @@ class PC(m.CppBase):
1010

1111
class PPCCInit(PC, m.CppDrvd):
1212
def __init__(self, value):
13+
print("\nLOOOK PPCCInit PC", flush=True)
1314
PC.__init__(self, value)
15+
print("\nLOOOK PPCCInit CppDrvd", flush=True)
1416
m.CppDrvd.__init__(self, value + 1)
17+
print("\nLOOOK PPCCInit Done", flush=True)
18+
19+
20+
def NOtest_PC_AAA():
21+
print("\nLOOOK BEFORE PC(11) AAA", flush=True)
22+
d = PC(11)
23+
print("\nLOOOK AFTER PC(11) AAA", flush=True)
24+
assert d.get_base_value() == 11
25+
d.reset_base_value(13)
26+
assert d.get_base_value() == 13
1527

1628

1729
# Moving this test after test_PC() changes the behavior!
18-
def test_PPCCInit():
30+
def test_PPCCInit_BBB():
31+
print("\nLOOOK BEFORE PPCCInit(11) BBB", flush=True)
1932
d = PPCCInit(11)
33+
print("\nLOOOK AFTER PPCCInit(11) BBB", flush=True)
34+
print("\nLOOOK", flush=True)
2035
assert d.get_drvd_value() == 36
2136
d.reset_drvd_value(55)
2237
assert d.get_drvd_value() == 55
@@ -31,8 +46,29 @@ def test_PPCCInit():
3146
assert d.get_base_value_from_drvd() == 30
3247

3348

34-
def test_PC():
49+
def NOtest_PC_CCC():
50+
print("\nLOOOK BEFORE PC(11) CCC", flush=True)
3551
d = PC(11)
52+
print("\nLOOOK AFTER PC(11) CCC", flush=True)
3653
assert d.get_base_value() == 11
3754
d.reset_base_value(13)
3855
assert d.get_base_value() == 13
56+
57+
# Moving this test after test_PC() changes the behavior!
58+
def NOtest_PPCCInit_DDD():
59+
print("\nLOOOK BEFORE PPCCInit(11) DDD", flush=True)
60+
d = PPCCInit(11)
61+
print("\nLOOOK AFTER PPCCInit(11) DDD", flush=True)
62+
print("\nLOOOK", flush=True)
63+
assert d.get_drvd_value() == 36
64+
d.reset_drvd_value(55)
65+
assert d.get_drvd_value() == 55
66+
67+
assert d.get_base_value() == 12
68+
assert d.get_base_value_from_drvd() == 12
69+
d.reset_base_value(20)
70+
assert d.get_base_value() == 20
71+
assert d.get_base_value_from_drvd() == 20
72+
d.reset_base_value_from_drvd(30)
73+
assert d.get_base_value() == 30
74+
assert d.get_base_value_from_drvd() == 30

0 commit comments

Comments
 (0)