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
13d8cd2c
Commit
13d8cd2c
authored
Jun 15, 2017
by
Philip Austin
Committed by
Jason Rhinelander
Jun 15, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add the capsule name to the py::capsule constructor
This allows named capsules to be constructed with `py::capsule`.
parent
28f3df7f
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
32 additions
and
4 deletions
+32
-4
include/pybind11/pytypes.h
+6
-3
tests/test_python_types.cpp
+16
-0
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