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
1b52246f
Commit
1b52246f
authored
Mar 10, 2016
by
Ben Pritchard
Browse files
Better testing for callable python functions
parent
17cdb06c
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
12 additions
and
2 deletions
+12
-2
example/example5.py
example/example5.py
+6
-0
example/example5.ref
example/example5.ref
+4
-0
include/pybind11/functional.h
include/pybind11/functional.h
+1
-1
include/pybind11/pytypes.h
include/pybind11/pytypes.h
+1
-1
No files found.
example/example5.py
View file @
1b52246f
#!/usr/bin/env python
#!/usr/bin/env python
from
__future__
import
print_function
from
__future__
import
print_function
from
functools
import
partial
import
sys
import
sys
sys
.
path
.
append
(
'.'
)
sys
.
path
.
append
(
'.'
)
...
@@ -37,8 +38,13 @@ def func2(a, b, c, d):
...
@@ -37,8 +38,13 @@ def func2(a, b, c, d):
print
(
'Callback function 2 called : '
+
str
(
a
)
+
", "
+
str
(
b
)
+
", "
+
str
(
c
)
+
", "
+
str
(
d
))
print
(
'Callback function 2 called : '
+
str
(
a
)
+
", "
+
str
(
b
)
+
", "
+
str
(
c
)
+
", "
+
str
(
d
))
return
d
return
d
def
func3
(
a
):
print
(
'Callback function 3 called : '
+
str
(
a
))
print
(
test_callback1
(
func1
))
print
(
test_callback1
(
func1
))
print
(
test_callback2
(
func2
))
print
(
test_callback2
(
func2
))
print
(
test_callback1
(
partial
(
func2
,
"Hello"
,
"from"
,
"partial"
,
"object"
)))
print
(
test_callback1
(
partial
(
func3
,
"Partial object with one argument"
)))
test_callback3
(
lambda
i
:
i
+
1
)
test_callback3
(
lambda
i
:
i
+
1
)
f
=
test_callback4
()
f
=
test_callback4
()
...
...
example/example5.ref
View file @
1b52246f
...
@@ -19,4 +19,8 @@ Callback function 1 called!
...
@@ -19,4 +19,8 @@ Callback function 1 called!
False
False
Callback function 2 called : Hello, x, True, 5
Callback function 2 called : Hello, x, True, 5
5
5
Callback function 2 called : Hello, from, partial, object
False
Callback function 3 called : Partial object with one argument
False
func(43) = 44
func(43) = 44
include/pybind11/functional.h
View file @
1b52246f
...
@@ -20,7 +20,7 @@ template <typename Return, typename... Args> struct type_caster<std::function<Re
...
@@ -20,7 +20,7 @@ template <typename Return, typename... Args> struct type_caster<std::function<Re
public:
public:
bool
load
(
handle
src_
,
bool
)
{
bool
load
(
handle
src_
,
bool
)
{
src_
=
detail
::
get_function
(
src_
);
src_
=
detail
::
get_function
(
src_
);
if
(
!
src_
||
!
(
Py
Function_Check
(
src_
.
ptr
())
||
PyCFunction
_Check
(
src_
.
ptr
()))
)
if
(
!
src_
||
!
Py
Callable
_Check
(
src_
.
ptr
()))
return
false
;
return
false
;
object
src
(
src_
,
true
);
object
src
(
src_
,
true
);
value
=
[
src
](
Args
...
args
)
->
Return
{
value
=
[
src
](
Args
...
args
)
->
Return
{
...
...
include/pybind11/pytypes.h
View file @
1b52246f
...
@@ -428,7 +428,7 @@ public:
...
@@ -428,7 +428,7 @@ public:
class
function
:
public
object
{
class
function
:
public
object
{
public:
public:
PYBIND11_OBJECT_DEFAULT
(
function
,
object
,
Py
Function
_Check
)
PYBIND11_OBJECT_DEFAULT
(
function
,
object
,
Py
Callable
_Check
)
bool
is_cpp_function
()
const
{
bool
is_cpp_function
()
const
{
handle
fun
=
detail
::
get_function
(
m_ptr
);
handle
fun
=
detail
::
get_function
(
m_ptr
);
return
fun
&&
PyCFunction_Check
(
fun
.
ptr
());
return
fun
&&
PyCFunction_Check
(
fun
.
ptr
());
...
...
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