From ac0fde988a7e376454a1563480cc284e3a01333b Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Thu, 1 Oct 2015 18:37:26 +0200 Subject: [PATCH] don't throw an exception when python deallocates an object which still exists on the C++ side --- include/pybind/cast.h | 3 +++ include/pybind/pybind.h | 2 ++ include/pybind/pytypes.h | 2 +- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/include/pybind/cast.h b/include/pybind/cast.h index b8eee92c..c237429c 100644 --- a/include/pybind/cast.h +++ b/include/pybind/cast.h @@ -292,6 +292,9 @@ public: PYBIND_TYPE_CASTER(void_type, "None"); }; +template <> class type_caster : public type_caster { +}; + template <> class type_caster { public: bool load(PyObject *src, bool) { diff --git a/include/pybind/pybind.h b/include/pybind/pybind.h index 699489c3..666cd69d 100644 --- a/include/pybind/pybind.h +++ b/include/pybind/pybind.h @@ -883,6 +883,8 @@ public: inline function get_overload(const void *this_ptr, const char *name) { handle py_object = detail::get_object_handle(this_ptr); + if (!py_object) + return function(); handle type = py_object.get_type(); auto key = std::make_pair(type.ptr(), name); diff --git a/include/pybind/pytypes.h b/include/pybind/pytypes.h index 8afc0887..6d98d7d4 100644 --- a/include/pybind/pytypes.h +++ b/include/pybind/pytypes.h @@ -400,7 +400,7 @@ inline handle get_object_handle(const void *ptr) { auto instances = get_internals().registered_instances; auto it = instances.find(ptr); if (it == instances.end()) - throw std::runtime_error("Internal error: could not acquire Python handle of a C++ object"); + return handle(); return it->second; }