From bf8169e97bbdf05d813aee5ab187bd1d18eeeb9c Mon Sep 17 00:00:00 2001 From: David Vierra Date: Thu, 3 Sep 2015 10:11:31 -1000 Subject: [PATCH 1/2] BindingManagerPrivate::assignWrapper will override existing bindings. This is needed to handle the case where the underlying C++ object is deleted, but its Python wrapper is still alive. If a new C++ object is allocated at the same address, it needs to override the old binding to prevent the situation where the old binding is reused. If the old binding is not replaced, a C++ object may be bound to a Python wrapper for different object type. (Or worse, a C++ object is bound to a Python object which no longer exists.) --- libshiboken/bindingmanager.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/libshiboken/bindingmanager.cpp b/libshiboken/bindingmanager.cpp index cb610000..011fac52 100644 --- a/libshiboken/bindingmanager.cpp +++ b/libshiboken/bindingmanager.cpp @@ -140,9 +140,7 @@ void BindingManager::BindingManagerPrivate::releaseWrapper(void* cptr) void BindingManager::BindingManagerPrivate::assignWrapper(SbkObject* wrapper, const void* cptr) { assert(cptr); - WrapperMap::iterator iter = wrapperMapper.find(cptr); - if (iter == wrapperMapper.end()) - wrapperMapper.insert(std::make_pair(cptr, wrapper)); + wrapperMapper[cptr] = wrapper; } BindingManager::BindingManager() From 817d49a866949b757344dfacefc2f709836a2116 Mon Sep 17 00:00:00 2001 From: David Vierra Date: Thu, 3 Sep 2015 10:12:42 -1000 Subject: [PATCH 2/2] Add a method to the `shiboken` module for discovering the binding registered for a C++ object. This allows you to assert that `shiboken.getBinding(shiboken.getCppPointer(obj)[0]) is obj`, which was not true in the issue I was investigating. --- shibokenmodule/typesystem_shiboken.xml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/shibokenmodule/typesystem_shiboken.xml b/shibokenmodule/typesystem_shiboken.xml index b3d20594..7228c519 100644 --- a/shibokenmodule/typesystem_shiboken.xml +++ b/shibokenmodule/typesystem_shiboken.xml @@ -4,6 +4,7 @@ + bool isValid = Shiboken::Object::isValid(%1, false); @@ -39,6 +40,15 @@ + + + %PYARG_0 = (PyObject*)Shiboken::BindingManager::instance().retrieveWrapper((void *)%1); + if(!%PYARG_0) { + PyErr_SetString(PyExc_ValueError, "No binding registered."); + } + + + if (Shiboken::Object::checkType(%1)) {