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
8de0437e
Commit
8de0437e
authored
Aug 18, 2016
by
Wenzel Jakob
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
type_caster<std::function>: allow None values in both directions
parent
0b63231b
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
12 additions
and
0 deletions
+12
-0
example/example-callbacks.cpp
+3
-0
example/example-callbacks.py
+2
-0
example/example-callbacks.ref
+1
-0
include/pybind11/functional.h
+6
-0
No files found.
example/example-callbacks.cpp
View file @
8de0437e
...
@@ -38,6 +38,9 @@ py::cpp_function test_callback5() {
...
@@ -38,6 +38,9 @@ py::cpp_function test_callback5() {
int
dummy_function
(
int
i
)
{
return
i
+
1
;
}
int
dummy_function
(
int
i
)
{
return
i
+
1
;
}
int
dummy_function2
(
int
i
,
int
j
)
{
return
i
+
j
;
}
int
dummy_function2
(
int
i
,
int
j
)
{
return
i
+
j
;
}
std
::
function
<
int
(
int
)
>
roundtrip
(
std
::
function
<
int
(
int
)
>
f
)
{
std
::
function
<
int
(
int
)
>
roundtrip
(
std
::
function
<
int
(
int
)
>
f
)
{
if
(
!
f
)
std
::
cout
<<
"roundtrip (got None).."
<<
std
::
endl
;
else
std
::
cout
<<
"roundtrip.."
<<
std
::
endl
;
std
::
cout
<<
"roundtrip.."
<<
std
::
endl
;
return
f
;
return
f
;
}
}
...
...
example/example-callbacks.py
View file @
8de0437e
...
@@ -68,6 +68,8 @@ from example import roundtrip
...
@@ -68,6 +68,8 @@ from example import roundtrip
test_dummy_function
(
dummy_function
)
test_dummy_function
(
dummy_function
)
test_dummy_function
(
roundtrip
(
dummy_function
))
test_dummy_function
(
roundtrip
(
dummy_function
))
if
roundtrip
(
None
)
is
not
None
:
print
(
"Problem!"
)
test_dummy_function
(
lambda
x
:
x
+
2
)
test_dummy_function
(
lambda
x
:
x
+
2
)
try
:
try
:
...
...
example/example-callbacks.ref
View file @
8de0437e
...
@@ -30,6 +30,7 @@ Copy constructions: 1
...
@@ -30,6 +30,7 @@ Copy constructions: 1
Move constructions: True
Move constructions: True
argument matches dummy_function
argument matches dummy_function
eval(1) = 2
eval(1) = 2
roundtrip (got None)..
roundtrip..
roundtrip..
argument matches dummy_function
argument matches dummy_function
eval(1) = 2
eval(1) = 2
...
...
include/pybind11/functional.h
View file @
8de0437e
...
@@ -20,6 +20,9 @@ template <typename Return, typename... Args> struct type_caster<std::function<Re
...
@@ -20,6 +20,9 @@ template <typename Return, typename... Args> struct type_caster<std::function<Re
typedef
typename
std
::
conditional
<
std
::
is_same
<
Return
,
void
>::
value
,
void_type
,
Return
>::
type
retval_type
;
typedef
typename
std
::
conditional
<
std
::
is_same
<
Return
,
void
>::
value
,
void_type
,
Return
>::
type
retval_type
;
public
:
public
:
bool
load
(
handle
src_
,
bool
)
{
bool
load
(
handle
src_
,
bool
)
{
if
(
src_
.
ptr
()
==
Py_None
)
return
true
;
src_
=
detail
::
get_function
(
src_
);
src_
=
detail
::
get_function
(
src_
);
if
(
!
src_
||
!
PyCallable_Check
(
src_
.
ptr
()))
if
(
!
src_
||
!
PyCallable_Check
(
src_
.
ptr
()))
return
false
;
return
false
;
...
@@ -58,6 +61,9 @@ public:
...
@@ -58,6 +61,9 @@ public:
template
<
typename
Func
>
template
<
typename
Func
>
static
handle
cast
(
Func
&&
f_
,
return_value_policy
policy
,
handle
/* parent */
)
{
static
handle
cast
(
Func
&&
f_
,
return_value_policy
policy
,
handle
/* parent */
)
{
if
(
!
f_
)
return
handle
(
Py_None
).
inc_ref
();
auto
result
=
f_
.
template
target
<
Return
(
*
)(
Args
...)
>
();
auto
result
=
f_
.
template
target
<
Return
(
*
)(
Args
...)
>
();
if
(
result
)
if
(
result
)
return
cpp_function
(
*
result
,
policy
).
release
();
return
cpp_function
(
*
result
,
policy
).
release
();
...
...
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