Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
c2db53da
Unverified
Commit
c2db53da
authored
Apr 02, 2021
by
Robert Haschke
Committed by
GitHub
Apr 02, 2021
Browse files
fix: catch missing self argument in overloads constructor (#2914)
parent
3df0ee6f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
8 additions
and
6 deletions
+8
-6
include/pybind11/pybind11.h
include/pybind11/pybind11.h
+2
-2
tests/test_factory_constructors.py
tests/test_factory_constructors.py
+6
-4
No files found.
include/pybind11/pybind11.h
View file @
c2db53da
...
@@ -555,8 +555,8 @@ protected:
...
@@ -555,8 +555,8 @@ protected:
auto
self_value_and_holder
=
value_and_holder
();
auto
self_value_and_holder
=
value_and_holder
();
if
(
overloads
->
is_constructor
)
{
if
(
overloads
->
is_constructor
)
{
if
(
!
PyObject_TypeCheck
(
parent
.
ptr
(),
(
PyTypeObject
*
)
overloads
->
scope
.
ptr
()))
{
if
(
!
parent
||
!
PyObject_TypeCheck
(
parent
.
ptr
(),
(
PyTypeObject
*
)
overloads
->
scope
.
ptr
()))
{
PyErr_SetString
(
PyExc_TypeError
,
"__init__(self, ...) called with invalid `self` argument"
);
PyErr_SetString
(
PyExc_TypeError
,
"__init__(self, ...) called with invalid
or missing
`self` argument"
);
return
nullptr
;
return
nullptr
;
}
}
...
...
tests/test_factory_constructors.py
View file @
c2db53da
...
@@ -486,7 +486,9 @@ def test_invalid_self():
...
@@ -486,7 +486,9 @@ def test_invalid_self():
# Same as above, but for a class with an alias:
# Same as above, but for a class with an alias:
class
BrokenTF6
(
m
.
TestFactory6
):
class
BrokenTF6
(
m
.
TestFactory6
):
def
__init__
(
self
,
bad
):
def
__init__
(
self
,
bad
):
if
bad
==
1
:
if
bad
==
0
:
m
.
TestFactory6
.
__init__
()
elif
bad
==
1
:
a
=
m
.
TestFactory2
(
tag
.
pointer
,
1
)
a
=
m
.
TestFactory2
(
tag
.
pointer
,
1
)
m
.
TestFactory6
.
__init__
(
a
,
tag
.
base
,
1
)
m
.
TestFactory6
.
__init__
(
a
,
tag
.
base
,
1
)
elif
bad
==
2
:
elif
bad
==
2
:
...
@@ -506,13 +508,13 @@ def test_invalid_self():
...
@@ -506,13 +508,13 @@ def test_invalid_self():
BrokenTF1
(
arg
)
BrokenTF1
(
arg
)
assert
(
assert
(
str
(
excinfo
.
value
)
str
(
excinfo
.
value
)
==
"__init__(self, ...) called with invalid `self` argument"
==
"__init__(self, ...) called with invalid
or missing
`self` argument"
)
)
for
arg
in
(
1
,
2
,
3
,
4
):
for
arg
in
(
0
,
1
,
2
,
3
,
4
):
with
pytest
.
raises
(
TypeError
)
as
excinfo
:
with
pytest
.
raises
(
TypeError
)
as
excinfo
:
BrokenTF6
(
arg
)
BrokenTF6
(
arg
)
assert
(
assert
(
str
(
excinfo
.
value
)
str
(
excinfo
.
value
)
==
"__init__(self, ...) called with invalid `self` argument"
==
"__init__(self, ...) called with invalid
or missing
`self` argument"
)
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment