From a3fd84dd8e5cc22f30a9f7a017613660cedae157 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Fri, 1 Dec 2023 10:32:16 -0800 Subject: [PATCH] Add `test_pyobject_ptr_from_handle_nullptr` (no production code changes), to firmly establish that this part works already. --- tests/test_type_caster_pyobject_ptr.cpp | 12 ++++++++++++ tests/test_type_caster_pyobject_ptr.py | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/tests/test_type_caster_pyobject_ptr.cpp b/tests/test_type_caster_pyobject_ptr.cpp index 8069f7dcd9..30029c88d1 100644 --- a/tests/test_type_caster_pyobject_ptr.cpp +++ b/tests/test_type_caster_pyobject_ptr.cpp @@ -127,4 +127,16 @@ TEST_SUBMODULE(type_caster_pyobject_ptr, m) { (void) py::cast(*ptr); } #endif + + m.def("pyobject_ptr_from_handle_nullptr", []() { + py::handle handle_nullptr; + if (handle_nullptr.ptr() != nullptr) { + return "UNEXPECTED: handle_nullptr.ptr() != nullptr"; + } + auto *pyobject_ptr_from_handle = py::cast(handle_nullptr); + if (pyobject_ptr_from_handle != nullptr) { + return "UNEXPECTED: pyobject_ptr_from_handle != nullptr"; + } + return "SUCCESS"; + }); } diff --git a/tests/test_type_caster_pyobject_ptr.py b/tests/test_type_caster_pyobject_ptr.py index 1f1ece2baf..c598f9e573 100644 --- a/tests/test_type_caster_pyobject_ptr.py +++ b/tests/test_type_caster_pyobject_ptr.py @@ -102,3 +102,7 @@ def test_return_list_pyobject_ptr_reference(): def test_type_caster_name_via_incompatible_function_arguments_type_error(): with pytest.raises(TypeError, match=r"1\. \(arg0: object, arg1: int\) -> None"): m.pass_pyobject_ptr_and_int(ValueHolder(101), ValueHolder(202)) + + +def test_pyobject_ptr_from_handle_nullptr(): + assert m.pyobject_ptr_from_handle_nullptr() == "SUCCESS"