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
13484a20
Commit
13484a20
authored
Mar 10, 2016
by
Wenzel Jakob
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #135 from bennybp/master
Check for other callable python objects
parents
17cdb06c
1b52246f
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
12 additions
and
2 deletions
+12
-2
example/example5.py
+6
-0
example/example5.ref
+4
-0
include/pybind11/functional.h
+1
-1
include/pybind11/pytypes.h
+1
-1
No files found.
example/example5.py
View file @
13484a20
#!/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 @
13484a20
...
@@ -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 @
13484a20
...
@@ -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_
||
!
(
PyFunction_Check
(
src_
.
ptr
())
||
PyCFunction_Check
(
src_
.
ptr
()
)))
if
(
!
src_
||
!
PyCallable_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 @
13484a20
...
@@ -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