Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pybind11
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
pybind11
Commits
076c89fc
Unverified
Commit
076c89fc
authored
Oct 22, 2021
by
Dmitry Yershov
Committed by
GitHub
Oct 22, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tests: test recursive dispatch using visitor pattern (#3365)
parent
606f81a9
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
73 additions
and
0 deletions
+73
-0
tests/test_virtual_functions.cpp
+40
-0
tests/test_virtual_functions.py
+33
-0
No files found.
tests/test_virtual_functions.cpp
View file @
076c89fc
...
...
@@ -174,6 +174,25 @@ struct DispatchIssue : Base {
}
};
// An abstract adder class that uses visitor pattern to add two data
// objects and send the result to the visitor functor
struct
AdderBase
{
struct
Data
{};
using
DataVisitor
=
std
::
function
<
void
(
const
Data
&
)
>
;
virtual
void
operator
()(
const
Data
&
first
,
const
Data
&
second
,
const
DataVisitor
&
visitor
)
const
=
0
;
virtual
~
AdderBase
()
=
default
;
AdderBase
()
=
default
;
AdderBase
(
const
AdderBase
&
)
=
delete
;
};
struct
Adder
:
AdderBase
{
void
operator
()(
const
Data
&
first
,
const
Data
&
second
,
const
DataVisitor
&
visitor
)
const
override
{
PYBIND11_OVERRIDE_PURE_NAME
(
void
,
AdderBase
,
"__call__"
,
operator
(),
first
,
second
,
visitor
);
}
};
static
void
test_gil
()
{
{
py
::
gil_scoped_acquire
lock
;
...
...
@@ -295,6 +314,27 @@ TEST_SUBMODULE(virtual_functions, m) {
m
.
def
(
"dispatch_issue_go"
,
[](
const
Base
*
b
)
{
return
b
->
dispatch
();
});
// test_recursive_dispatch_issue
// #3357: Recursive dispatch fails to find python function override
pybind11
::
class_
<
AdderBase
,
Adder
>
(
m
,
"Adder"
)
.
def
(
pybind11
::
init
<>
())
.
def
(
"__call__"
,
&
AdderBase
::
operator
());
pybind11
::
class_
<
AdderBase
::
Data
>
(
m
,
"Data"
)
.
def
(
pybind11
::
init
<>
());
m
.
def
(
"add2"
,
[](
const
AdderBase
::
Data
&
first
,
const
AdderBase
::
Data
&
second
,
const
AdderBase
&
adder
,
const
AdderBase
::
DataVisitor
&
visitor
)
{
adder
(
first
,
second
,
visitor
);
});
m
.
def
(
"add3"
,
[](
const
AdderBase
::
Data
&
first
,
const
AdderBase
::
Data
&
second
,
const
AdderBase
::
Data
&
third
,
const
AdderBase
&
adder
,
const
AdderBase
::
DataVisitor
&
visitor
)
{
adder
(
first
,
second
,
[
&
]
(
const
AdderBase
::
Data
&
first_plus_second
)
{
adder
(
first_plus_second
,
third
,
visitor
);
});
});
// test_override_ref
// #392/397: overriding reference-returning functions
class
OverrideTest
{
...
...
tests/test_virtual_functions.py
View file @
076c89fc
...
...
@@ -257,6 +257,39 @@ def test_dispatch_issue(msg):
assert
m
.
dispatch_issue_go
(
b
)
==
"Yay.."
def
test_recursive_dispatch_issue
(
msg
):
"""#3357: Recursive dispatch fails to find python function override"""
class
Data
(
m
.
Data
):
def
__init__
(
self
,
value
):
super
(
Data
,
self
)
.
__init__
()
self
.
value
=
value
class
Adder
(
m
.
Adder
):
def
__call__
(
self
,
first
,
second
,
visitor
):
# lambda is a workaround, which adds extra frame to the
# current CPython thread. Removing lambda reveals the bug
# [https://github.com/pybind/pybind11/issues/3357]
(
lambda
:
visitor
(
Data
(
first
.
value
+
second
.
value
)))()
class
StoreResultVisitor
:
def
__init__
(
self
):
self
.
result
=
None
def
__call__
(
self
,
data
):
self
.
result
=
data
.
value
store
=
StoreResultVisitor
()
m
.
add2
(
Data
(
1
),
Data
(
2
),
Adder
(),
store
)
assert
store
.
result
==
3
# without lambda in Adder class, this function fails with
# RuntimeError: Tried to call pure virtual function "AdderBase::__call__"
m
.
add3
(
Data
(
1
),
Data
(
2
),
Data
(
3
),
Adder
(),
store
)
assert
store
.
result
==
6
def
test_override_ref
():
"""#392/397: overriding reference-returning functions"""
o
=
m
.
OverrideTest
(
"asdf"
)
...
...
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