From 6311b08623a5275251ba761be1d30deff8b22e9f Mon Sep 17 00:00:00 2001 From: Phil Austin Date: Tue, 13 Jun 2017 15:09:48 -0700 Subject: [PATCH 1/7] add the capsule name to the py::capsule constructor --- include/pybind11/pytypes.h | 4 ++-- tests/test_python_types.cpp | 12 ++++++++++++ tests/test_python_types.py | 11 +++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index c8a5014e30..8231023c99 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -1016,8 +1016,8 @@ class capsule : public object { PYBIND11_DEPRECATED("Use reinterpret_borrow() or reinterpret_steal()") 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(value), nullptr, nullptr), stolen_t{}) { + explicit capsule(const void *value, const char *name = nullptr, void (*destructor)(PyObject *) = nullptr) + : object(PyCapsule_New(const_cast(value), name, destructor), stolen_t{}) { if (!m_ptr) pybind11_fail("Could not allocate capsule object!"); } diff --git a/tests/test_python_types.cpp b/tests/test_python_types.cpp index 0d7ea502b6..888451a3bc 100644 --- a/tests/test_python_types.cpp +++ b/tests/test_python_types.cpp @@ -573,6 +573,18 @@ test_initializer python_types([](py::module &m) { } ); + m.def("return_capsule_with_name_and_destructor_3", + []() { + py::print("creating capsule"); + auto capsule=py::object(py::capsule((void *) 1234, "pointer type description", + [](PyObject *ptr) {py::print("destructing capsule");})); + auto name=PyCapsule_GetName(capsule.ptr()); + py::print(name); + 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{}; }); diff --git a/tests/test_python_types.py b/tests/test_python_types.py index 08bb3abe99..d263eec442 100644 --- a/tests/test_python_types.py +++ b/tests/test_python_types.py @@ -603,6 +603,17 @@ 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 == """ + creating capsule + pointer type description + destructing capsule + """ + + def test_void_caster(): import pybind11_tests as m From 56040cc5220f8f82c3eb747ef0d79c065c0c94f0 Mon Sep 17 00:00:00 2001 From: Phil Austin Date: Tue, 13 Jun 2017 16:51:39 -0700 Subject: [PATCH 2/7] remove unused variable warning --- tests/test_python_types.cpp | 11 +++++++---- tests/test_python_types.py | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/test_python_types.cpp b/tests/test_python_types.cpp index 888451a3bc..ee3e143dd5 100644 --- a/tests/test_python_types.cpp +++ b/tests/test_python_types.cpp @@ -575,15 +575,18 @@ test_initializer python_types([](py::module &m) { m.def("return_capsule_with_name_and_destructor_3", []() { - py::print("creating capsule"); auto capsule=py::object(py::capsule((void *) 1234, "pointer type description", - [](PyObject *ptr) {py::print("destructing capsule");})); + [](PyObject *ptr) { + if(ptr){ + py::print("destructing capsule"); + } + })); auto name=PyCapsule_GetName(capsule.ptr()); - py::print(name); + auto contents=PyCapsule_GetPointer(capsule.ptr(),name); + 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{}; }); diff --git a/tests/test_python_types.py b/tests/test_python_types.py index d263eec442..1d00a42dc4 100644 --- a/tests/test_python_types.py +++ b/tests/test_python_types.py @@ -608,8 +608,8 @@ def test_capsule_with_destructor(capture): del a pytest.gc_collect() assert capture.unordered == """ + created capsule with name --pointer type description-- and contents 1234 creating capsule - pointer type description destructing capsule """ From d5a972671659ff3514b8733e5c600a66c01305b7 Mon Sep 17 00:00:00 2001 From: Phil Austin Date: Tue, 13 Jun 2017 20:23:14 -0700 Subject: [PATCH 3/7] add name() member function --- include/pybind11/pytypes.h | 5 ++++- tests/test_python_types.cpp | 8 ++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 8231023c99..a5aff662a2 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -1054,10 +1054,13 @@ class capsule : public object { } template operator T *() const { - T * result = static_cast(PyCapsule_GetPointer(m_ptr, nullptr)); + auto name = this->name(); + T * result = static_cast(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 { diff --git a/tests/test_python_types.cpp b/tests/test_python_types.cpp index ee3e143dd5..adac424d2a 100644 --- a/tests/test_python_types.cpp +++ b/tests/test_python_types.cpp @@ -575,14 +575,14 @@ test_initializer python_types([](py::module &m) { m.def("return_capsule_with_name_and_destructor_3", []() { - auto capsule=py::object(py::capsule((void *) 1234, "pointer type description", + auto capsule=py::capsule((void *) 1234, "pointer type description", [](PyObject *ptr) { if(ptr){ py::print("destructing capsule"); } - })); - auto name=PyCapsule_GetName(capsule.ptr()); - auto contents=PyCapsule_GetPointer(capsule.ptr(),name); + }); + auto name = capsule.name(); + void *contents = capsule; py::print("created capsule with name --{}-- and contents {}"_s.format(name,(size_t) contents)); return capsule; } From d31547b4d4b9079c380bbb65f7020c840091f502 Mon Sep 17 00:00:00 2001 From: Phil Austin Date: Tue, 13 Jun 2017 20:46:26 -0700 Subject: [PATCH 4/7] add print before creation --- tests/test_python_types.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_python_types.cpp b/tests/test_python_types.cpp index adac424d2a..bdde60047e 100644 --- a/tests/test_python_types.cpp +++ b/tests/test_python_types.cpp @@ -575,6 +575,7 @@ 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){ From 94fbf776ba1562e831bb37000a574a3b07f65cf4 Mon Sep 17 00:00:00 2001 From: Phil Austin Date: Tue, 13 Jun 2017 21:22:08 -0700 Subject: [PATCH 5/7] whitespace fixes --- tests/test_python_types.cpp | 4 ++-- tests/test_python_types.py | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/test_python_types.cpp b/tests/test_python_types.cpp index bdde60047e..7b0bec36b9 100644 --- a/tests/test_python_types.cpp +++ b/tests/test_python_types.cpp @@ -578,7 +578,7 @@ test_initializer python_types([](py::module &m) { py::print("creating capsule"); auto capsule=py::capsule((void *) 1234, "pointer type description", [](PyObject *ptr) { - if(ptr){ + if (ptr){ py::print("destructing capsule"); } }); @@ -588,7 +588,7 @@ test_initializer python_types([](py::module &m) { 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{}; }); diff --git a/tests/test_python_types.py b/tests/test_python_types.py index 1d00a42dc4..234ea62def 100644 --- a/tests/test_python_types.py +++ b/tests/test_python_types.py @@ -613,8 +613,6 @@ def test_capsule_with_destructor(capture): destructing capsule """ - - def test_void_caster(): import pybind11_tests as m From 58bddfaee844c695162d40e63b9c3c9159d5c237 Mon Sep 17 00:00:00 2001 From: Phil Austin Date: Wed, 14 Jun 2017 08:59:10 -0700 Subject: [PATCH 6/7] more whitespace tweaks --- tests/test_python_types.cpp | 2 +- tests/test_python_types.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_python_types.cpp b/tests/test_python_types.cpp index 7b0bec36b9..a3ed2895b8 100644 --- a/tests/test_python_types.cpp +++ b/tests/test_python_types.cpp @@ -578,7 +578,7 @@ test_initializer python_types([](py::module &m) { py::print("creating capsule"); auto capsule=py::capsule((void *) 1234, "pointer type description", [](PyObject *ptr) { - if (ptr){ + if (ptr) { py::print("destructing capsule"); } }); diff --git a/tests/test_python_types.py b/tests/test_python_types.py index 234ea62def..df064b33ef 100644 --- a/tests/test_python_types.py +++ b/tests/test_python_types.py @@ -615,7 +615,6 @@ def test_capsule_with_destructor(capture): def test_void_caster(): import pybind11_tests as m - assert m.load_nullptr_t(None) is None assert m.cast_nullptr_t() is None From 06feedbc01521352dd18bf51033e25c5bb8e3466 Mon Sep 17 00:00:00 2001 From: Phil Austin Date: Wed, 14 Jun 2017 10:14:18 -0700 Subject: [PATCH 7/7] whitespace again --- tests/test_python_types.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_python_types.py b/tests/test_python_types.py index df064b33ef..ecd317e20c 100644 --- a/tests/test_python_types.py +++ b/tests/test_python_types.py @@ -613,6 +613,7 @@ def test_capsule_with_destructor(capture): destructing capsule """ + def test_void_caster(): import pybind11_tests as m assert m.load_nullptr_t(None) is None