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
13d8cd2c
Commit
13d8cd2c
authored
Jun 15, 2017
by
Philip Austin
Committed by
Jason Rhinelander
Jun 15, 2017
Browse files
add the capsule name to the py::capsule constructor
This allows named capsules to be constructed with `py::capsule`.
parent
28f3df7f
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
32 additions
and
4 deletions
+32
-4
include/pybind11/pytypes.h
include/pybind11/pytypes.h
+6
-3
tests/test_python_types.cpp
tests/test_python_types.cpp
+16
-0
tests/test_python_types.py
tests/test_python_types.py
+10
-1
No files found.
include/pybind11/pytypes.h
View file @
13d8cd2c
...
@@ -1016,8 +1016,8 @@ public:
...
@@ -1016,8 +1016,8 @@ public:
PYBIND11_DEPRECATED
(
"Use reinterpret_borrow<capsule>() or reinterpret_steal<capsule>()"
)
PYBIND11_DEPRECATED
(
"Use reinterpret_borrow<capsule>() or reinterpret_steal<capsule>()"
)
capsule
(
PyObject
*
ptr
,
bool
is_borrowed
)
:
object
(
is_borrowed
?
object
(
ptr
,
borrowed_t
{})
:
object
(
ptr
,
stolen_t
{}))
{
}
capsule
(
PyObject
*
ptr
,
bool
is_borrowed
)
:
object
(
is_borrowed
?
object
(
ptr
,
borrowed_t
{})
:
object
(
ptr
,
stolen_t
{}))
{
}
explicit
capsule
(
const
void
*
value
)
explicit
capsule
(
const
void
*
value
,
const
char
*
name
=
nullptr
,
void
(
*
destructor
)(
PyObject
*
)
=
nullptr
)
:
object
(
PyCapsule_New
(
const_cast
<
void
*>
(
value
),
n
ullptr
,
nullpt
r
),
stolen_t
{})
{
:
object
(
PyCapsule_New
(
const_cast
<
void
*>
(
value
),
n
ame
,
destructo
r
),
stolen_t
{})
{
if
(
!
m_ptr
)
if
(
!
m_ptr
)
pybind11_fail
(
"Could not allocate capsule object!"
);
pybind11_fail
(
"Could not allocate capsule object!"
);
}
}
...
@@ -1054,10 +1054,13 @@ public:
...
@@ -1054,10 +1054,13 @@ public:
}
}
template
<
typename
T
>
operator
T
*
()
const
{
template
<
typename
T
>
operator
T
*
()
const
{
T
*
result
=
static_cast
<
T
*>
(
PyCapsule_GetPointer
(
m_ptr
,
nullptr
));
auto
name
=
this
->
name
();
T
*
result
=
static_cast
<
T
*>
(
PyCapsule_GetPointer
(
m_ptr
,
name
));
if
(
!
result
)
pybind11_fail
(
"Unable to extract capsule contents!"
);
if
(
!
result
)
pybind11_fail
(
"Unable to extract capsule contents!"
);
return
result
;
return
result
;
}
}
const
char
*
name
()
const
{
return
PyCapsule_GetName
(
m_ptr
);
}
};
};
class
tuple
:
public
object
{
class
tuple
:
public
object
{
...
...
tests/test_python_types.cpp
View file @
13d8cd2c
...
@@ -573,6 +573,22 @@ test_initializer python_types([](py::module &m) {
...
@@ -573,6 +573,22 @@ test_initializer python_types([](py::module &m) {
}
}
);
);
m
.
def
(
"return_capsule_with_name_and_destructor_3"
,
[]()
{
py
::
print
(
"creating capsule"
);
auto
capsule
=
py
::
capsule
((
void
*
)
1234
,
"pointer type description"
,
[](
PyObject
*
ptr
)
{
if
(
ptr
)
{
py
::
print
(
"destructing capsule"
);
}
});
auto
name
=
capsule
.
name
();
void
*
contents
=
capsule
;
py
::
print
(
"created capsule with name --{}-- and contents {}"
_s
.
format
(
name
,(
size_t
)
contents
));
return
capsule
;
}
);
m
.
def
(
"load_nullptr_t"
,
[](
std
::
nullptr_t
)
{});
// not useful, but it should still compile
m
.
def
(
"load_nullptr_t"
,
[](
std
::
nullptr_t
)
{});
// not useful, but it should still compile
m
.
def
(
"cast_nullptr_t"
,
[]()
{
return
std
::
nullptr_t
{};
});
m
.
def
(
"cast_nullptr_t"
,
[]()
{
return
std
::
nullptr_t
{};
});
...
...
tests/test_python_types.py
View file @
13d8cd2c
...
@@ -603,10 +603,19 @@ def test_capsule_with_destructor(capture):
...
@@ -603,10 +603,19 @@ def test_capsule_with_destructor(capture):
destructing capsule: 1234
destructing capsule: 1234
"""
"""
with
capture
:
a
=
m
.
return_capsule_with_name_and_destructor_3
()
del
a
pytest
.
gc_collect
()
assert
capture
.
unordered
==
"""
created capsule with name --pointer type description-- and contents 1234
creating capsule
destructing capsule
"""
def
test_void_caster
():
def
test_void_caster
():
import
pybind11_tests
as
m
import
pybind11_tests
as
m
assert
m
.
load_nullptr_t
(
None
)
is
None
assert
m
.
load_nullptr_t
(
None
)
is
None
assert
m
.
cast_nullptr_t
()
is
None
assert
m
.
cast_nullptr_t
()
is
None
...
...
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