Skip to content

Commit c2db53d

Browse files
authored
fix: catch missing self argument in overloads constructor (#2914)
1 parent 3df0ee6 commit c2db53d

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

include/pybind11/pybind11.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,8 +555,8 @@ class cpp_function : public function {
555555

556556
auto self_value_and_holder = value_and_holder();
557557
if (overloads->is_constructor) {
558-
if (!PyObject_TypeCheck(parent.ptr(), (PyTypeObject *) overloads->scope.ptr())) {
559-
PyErr_SetString(PyExc_TypeError, "__init__(self, ...) called with invalid `self` argument");
558+
if (!parent || !PyObject_TypeCheck(parent.ptr(), (PyTypeObject *) overloads->scope.ptr())) {
559+
PyErr_SetString(PyExc_TypeError, "__init__(self, ...) called with invalid or missing `self` argument");
560560
return nullptr;
561561
}
562562

tests/test_factory_constructors.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,9 @@ def __init__(self, bad):
486486
# Same as above, but for a class with an alias:
487487
class BrokenTF6(m.TestFactory6):
488488
def __init__(self, bad):
489-
if bad == 1:
489+
if bad == 0:
490+
m.TestFactory6.__init__()
491+
elif bad == 1:
490492
a = m.TestFactory2(tag.pointer, 1)
491493
m.TestFactory6.__init__(a, tag.base, 1)
492494
elif bad == 2:
@@ -506,13 +508,13 @@ def __init__(self, bad):
506508
BrokenTF1(arg)
507509
assert (
508510
str(excinfo.value)
509-
== "__init__(self, ...) called with invalid `self` argument"
511+
== "__init__(self, ...) called with invalid or missing `self` argument"
510512
)
511513

512-
for arg in (1, 2, 3, 4):
514+
for arg in (0, 1, 2, 3, 4):
513515
with pytest.raises(TypeError) as excinfo:
514516
BrokenTF6(arg)
515517
assert (
516518
str(excinfo.value)
517-
== "__init__(self, ...) called with invalid `self` argument"
519+
== "__init__(self, ...) called with invalid or missing `self` argument"
518520
)

0 commit comments

Comments
 (0)