Skip to content

add the capsule name to the py::capsule constructor #902

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -1016,8 +1016,8 @@ class capsule : public object {
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{})) { }

explicit capsule(const void *value)
: object(PyCapsule_New(const_cast<void *>(value), nullptr, nullptr), stolen_t{}) {
explicit capsule(const void *value, const char *name = nullptr, void (*destructor)(PyObject *) = nullptr)
: object(PyCapsule_New(const_cast<void *>(value), name, destructor), stolen_t{}) {
if (!m_ptr)
pybind11_fail("Could not allocate capsule object!");
}
Expand Down Expand Up @@ -1054,10 +1054,13 @@ class capsule : public object {
}

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!");
return result;
}

const char *name() const { return PyCapsule_GetName(m_ptr); }
};

class tuple : public object {
Expand Down
16 changes: 16 additions & 0 deletions tests/test_python_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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("cast_nullptr_t", []() { return std::nullptr_t{}; });

Expand Down
11 changes: 10 additions & 1 deletion tests/test_python_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,10 +603,19 @@ def test_capsule_with_destructor(capture):
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():
import pybind11_tests as m

assert m.load_nullptr_t(None) is None
assert m.cast_nullptr_t() is None

Expand Down