Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
acae9301
Commit
acae9301
authored
Oct 27, 2021
by
Ralf W. Grosse-Kunstleve
Browse files
Merge branch 'master' into stable
parents
e315e1fe
f7b49961
Changes
46
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
87 additions
and
6 deletions
+87
-6
tests/test_stl_binders.py
tests/test_stl_binders.py
+1
-1
tests/test_virtual_functions.cpp
tests/test_virtual_functions.cpp
+40
-0
tests/test_virtual_functions.py
tests/test_virtual_functions.py
+33
-0
tools/pybind11Common.cmake
tools/pybind11Common.cmake
+1
-0
tools/pybind11NewTools.cmake
tools/pybind11NewTools.cmake
+6
-4
tools/pybind11Tools.cmake
tools/pybind11Tools.cmake
+6
-1
No files found.
tests/test_stl_binders.py
View file @
acae9301
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
import
pytest
import
pytest
import
env
# noqa: F401
import
env
from
pybind11_tests
import
stl_binders
as
m
from
pybind11_tests
import
stl_binders
as
m
...
...
tests/test_virtual_functions.cpp
View file @
acae9301
...
@@ -174,6 +174,25 @@ struct DispatchIssue : Base {
...
@@ -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
()
{
static
void
test_gil
()
{
{
{
py
::
gil_scoped_acquire
lock
;
py
::
gil_scoped_acquire
lock
;
...
@@ -295,6 +314,27 @@ TEST_SUBMODULE(virtual_functions, m) {
...
@@ -295,6 +314,27 @@ TEST_SUBMODULE(virtual_functions, m) {
m
.
def
(
"dispatch_issue_go"
,
[](
const
Base
*
b
)
{
return
b
->
dispatch
();
});
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
// test_override_ref
// #392/397: overriding reference-returning functions
// #392/397: overriding reference-returning functions
class
OverrideTest
{
class
OverrideTest
{
...
...
tests/test_virtual_functions.py
View file @
acae9301
...
@@ -257,6 +257,39 @@ def test_dispatch_issue(msg):
...
@@ -257,6 +257,39 @@ def test_dispatch_issue(msg):
assert
m
.
dispatch_issue_go
(
b
)
==
"Yay.."
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
():
def
test_override_ref
():
"""#392/397: overriding reference-returning functions"""
"""#392/397: overriding reference-returning functions"""
o
=
m
.
OverrideTest
(
"asdf"
)
o
=
m
.
OverrideTest
(
"asdf"
)
...
...
tools/pybind11Common.cmake
View file @
acae9301
...
@@ -20,6 +20,7 @@ Adds the following functions::
...
@@ -20,6 +20,7 @@ Adds the following functions::
#]======================================================]
#]======================================================]
# CMake 3.10 has an include_guard command, but we can't use that yet
# CMake 3.10 has an include_guard command, but we can't use that yet
# include_guard(global) (pre-CMake 3.10)
if
(
TARGET pybind11::lto
)
if
(
TARGET pybind11::lto
)
return
()
return
()
endif
()
endif
()
...
...
tools/pybind11NewTools.cmake
View file @
acae9301
...
@@ -5,6 +5,12 @@
...
@@ -5,6 +5,12 @@
# All rights reserved. Use of this source code is governed by a
# All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.
# BSD-style license that can be found in the LICENSE file.
if
(
CMAKE_VERSION VERSION_LESS 3.12
)
message
(
FATAL_ERROR
"You cannot use the new FindPython module with CMake < 3.12"
)
endif
()
include_guard
(
GLOBAL
)
get_property
(
get_property
(
is_config
is_config
TARGET pybind11::headers
TARGET pybind11::headers
...
@@ -16,10 +22,6 @@ else()
...
@@ -16,10 +22,6 @@ else()
set
(
_pybind11_quiet
""
)
set
(
_pybind11_quiet
""
)
endif
()
endif
()
if
(
CMAKE_VERSION VERSION_LESS 3.12
)
message
(
FATAL_ERROR
"You cannot use the new FindPython module with CMake < 3.12"
)
endif
()
if
(
NOT Python_FOUND
if
(
NOT Python_FOUND
AND NOT Python3_FOUND
AND NOT Python3_FOUND
AND NOT Python2_FOUND
)
AND NOT Python2_FOUND
)
...
...
tools/pybind11Tools.cmake
View file @
acae9301
...
@@ -5,6 +5,11 @@
...
@@ -5,6 +5,11 @@
# All rights reserved. Use of this source code is governed by a
# All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.
# BSD-style license that can be found in the LICENSE file.
# include_guard(global) (pre-CMake 3.10)
if
(
TARGET pybind11::python_headers
)
return
()
endif
()
# Built-in in CMake 3.5+
# Built-in in CMake 3.5+
include
(
CMakeParseArguments
)
include
(
CMakeParseArguments
)
...
@@ -38,7 +43,7 @@ endif()
...
@@ -38,7 +43,7 @@ endif()
# A user can set versions manually too
# A user can set versions manually too
set
(
Python_ADDITIONAL_VERSIONS
set
(
Python_ADDITIONAL_VERSIONS
"3.10;3.9;3.8;3.7;3.6;3.5;3.4"
"3.
11;3.
10;3.9;3.8;3.7;3.6;3.5;3.4"
CACHE INTERNAL
""
)
CACHE INTERNAL
""
)
list
(
APPEND CMAKE_MODULE_PATH
"
${
CMAKE_CURRENT_LIST_DIR
}
"
)
list
(
APPEND CMAKE_MODULE_PATH
"
${
CMAKE_CURRENT_LIST_DIR
}
"
)
...
...
Prev
1
2
3
Next
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