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
772c6d54
Commit
772c6d54
authored
Apr 30, 2016
by
Wenzel Jakob
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
enable passing C++ instances to void*-valued arguments
parent
e8b9dd26
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
40 additions
and
15 deletions
+40
-15
example/example14.cpp
+3
-3
example/example14.py
+6
-1
example/example14.ref
+9
-3
include/pybind11/cast.h
+21
-7
include/pybind11/pybind11.h
+1
-1
No files found.
example/example14.cpp
View file @
772c6d54
...
...
@@ -49,10 +49,10 @@ void init_ex14(py::module &m) {
std
::
cout
<<
"]"
<<
std
::
endl
;
});
m
.
def
(
"return_void_ptr"
,
[]()
{
return
(
void
*
)
1234
;
});
m
.
def
(
"print_void_ptr"
,
[](
void
*
ptr
)
{
std
::
cout
<<
"Got void ptr :
"
<<
(
uint64_t
)
ptr
<<
std
::
endl
;
});
m
.
def
(
"return_void_ptr"
,
[]()
{
return
(
void
*
)
0x
1234
;
});
m
.
def
(
"print_void_ptr"
,
[](
void
*
ptr
)
{
std
::
cout
<<
"Got void ptr :
0x"
<<
std
::
hex
<<
(
uint64_t
)
ptr
<<
std
::
endl
;
});
m
.
def
(
"return_null_str"
,
[]()
{
return
(
char
*
)
nullptr
;
});
m
.
def
(
"print_null_str"
,
[](
char
*
ptr
)
{
std
::
cout
<<
"Got null str :
"
<<
(
uint64_t
)
ptr
<<
std
::
endl
;
});
m
.
def
(
"print_null_str"
,
[](
char
*
ptr
)
{
std
::
cout
<<
"Got null str :
0x"
<<
std
::
hex
<<
(
uint64_t
)
ptr
<<
std
::
endl
;
});
m
.
def
(
"return_unique_ptr"
,
[]()
->
std
::
unique_ptr
<
StringList
>
{
StringList
*
result
=
new
StringList
();
...
...
example/example14.py
View file @
772c6d54
...
...
@@ -33,7 +33,12 @@ print_opaque_list(cvp.stringList)
#####
print_void_ptr
(
return_void_ptr
())
print_void_ptr
(
Example1
())
# Should also work for other C++ types
print_void_ptr
(
Example1
())
# Should also work for other C++ types
try
:
print_void_ptr
([
1
,
2
,
3
])
# This should not work
except
Exception
as
e
:
print
(
"Caught expected exception: "
+
str
(
e
))
print
(
return_null_str
())
print_null_str
(
return_null_str
())
...
...
example/example14.ref
View file @
772c6d54
...
...
@@ -5,8 +5,14 @@ Back element is Element 2
Opaque list: [Element 1]
Opaque list: []
Opaque list: [Element 1, Element 3]
Got void ptr : 1234
Got void ptr : 0x1234
Called Example1 default constructor..
Got void ptr : 0x7f9ba0f3c430
Called Example1 destructor (0)
Caught expected exception: Incompatible function arguments. The following argument types are supported:
1. (capsule) -> NoneType
None
Got null str : 0
<example.StringList object at 0x10
4a4750
0>
Got null str : 0
x0
<example.StringList object at 0x10
d3277a
0>
Opaque list: [some value]
include/pybind11/cast.h
View file @
772c6d54
...
...
@@ -52,15 +52,18 @@ PYBIND11_NOINLINE inline internals &get_internals() {
return
*
internals_ptr
;
}
PYBIND11_NOINLINE
inline
detail
::
type_info
*
get_type_info
(
PyTypeObject
*
type
)
{
PYBIND11_NOINLINE
inline
detail
::
type_info
*
get_type_info
(
PyTypeObject
*
type
,
bool
throw_if_missing
=
true
)
{
auto
const
&
type_dict
=
get_internals
().
registered_types_py
;
do
{
auto
it
=
type_dict
.
find
(
type
);
if
(
it
!=
type_dict
.
end
())
return
(
detail
::
type_info
*
)
it
->
second
;
type
=
type
->
tp_base
;
if
(
!
type
)
pybind11_fail
(
"pybind11::detail::get_type_info: unable to find type object!"
);
if
(
!
type
)
{
if
(
throw_if_missing
)
pybind11_fail
(
"pybind11::detail::get_type_info: unable to find type object!"
);
return
nullptr
;
}
}
while
(
true
);
}
...
...
@@ -382,11 +385,22 @@ public:
value
=
nullptr
;
return
true
;
}
/* Check if this is a capsule */
capsule
c
(
h
,
true
);
if
(
!
c
.
check
())
return
false
;
value
=
(
void
*
)
c
;
return
true
;
if
(
c
.
check
())
{
value
=
(
void
*
)
c
;
return
true
;
}
/* Check if this is a C++ type */
if
(
get_type_info
((
PyTypeObject
*
)
h
.
get_type
().
ptr
(),
false
))
{
value
=
((
instance
<
void
>
*
)
h
.
ptr
())
->
value
;
return
true
;
}
/* Fail */
return
false
;
}
static
handle
cast
(
const
void
*
ptr
,
return_value_policy
/* policy */
,
handle
/* parent */
)
{
...
...
include/pybind11/pybind11.h
View file @
772c6d54
...
...
@@ -450,7 +450,7 @@ protected:
return
nullptr
;
}
else
{
if
(
overloads
->
is_constructor
)
{
/* When a construtor ran successfully, the corresponding
/* When a constru
c
tor ran successfully, the corresponding
holder type (e.g. std::unique_ptr) must still be initialized. */
PyObject
*
inst
=
PyTuple_GetItem
(
args
,
0
);
auto
tinfo
=
detail
::
get_type_info
(
Py_TYPE
(
inst
));
...
...
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