diff options
author | Hugo Parente Lima <hugo.lima@openbossa.org> | 2010-06-16 18:29:33 -0300 |
---|---|---|
committer | Hugo Parente Lima <hugo.lima@openbossa.org> | 2010-06-16 18:55:13 -0300 |
commit | 59af9acf0d9d12e34f97b7c6da4b2f4008ecceca (patch) | |
tree | 84f5b76fc7c2fb2b41deecbfa4c863e3ddaa2c58 /libshiboken | |
parent | 79c71a20c1e6d8830672b1c094ee132b99663de1 (diff) |
Fix bug#237 - "core dump when call wrong constructor inside of a class"
Reviewer: Luciano Wolf <luciano.wolf@openbossa.org>
Marcelo Lira <marcelo.lira@openbossa.org>
Diffstat (limited to 'libshiboken')
-rw-r--r-- | libshiboken/basewrapper.cpp | 32 | ||||
-rw-r--r-- | libshiboken/basewrapper.h | 5 |
2 files changed, 36 insertions, 1 deletions
diff --git a/libshiboken/basewrapper.cpp b/libshiboken/basewrapper.cpp index 05926d17d..cc3a70a61 100644 --- a/libshiboken/basewrapper.cpp +++ b/libshiboken/basewrapper.cpp @@ -286,7 +286,7 @@ void walkThroughClassHierarchy(PyTypeObject* currentType, HierarchyVisitor* visi continue; } else { SbkBaseWrapperType* sbkType = reinterpret_cast<SbkBaseWrapperType*>(type); - if (sbkType->is_multicpp) + if (sbkType->is_user_type) walkThroughClassHierarchy(type, visitor); else visitor->visit(sbkType); @@ -599,6 +599,36 @@ void TypeDiscovery::addTypeDiscoveryFunction(Shiboken::TypeDiscoveryFunc func) m_discoveryFunctions.push_back(func); } +class FindBaseTypeVisitor : public HierarchyVisitor +{ + public: + FindBaseTypeVisitor(PyTypeObject* typeToFind) : m_found(false), m_typeToFind(typeToFind) {} + virtual void visit(SbkBaseWrapperType* node) + { + if (reinterpret_cast<PyTypeObject*>(node) == m_typeToFind) { + m_found = true; + finish(); + } + } + bool found() const { return m_found; } + + private: + bool m_found; + PyTypeObject* m_typeToFind; +}; + +bool canCallConstructor(PyTypeObject* myType, PyTypeObject* ctorType) +{ + FindBaseTypeVisitor visitor(ctorType); + walkThroughClassHierarchy(myType, &visitor); + if (!visitor.found()) { + PyErr_Format(PyExc_TypeError, "%s isn't a direct base class of %s", ctorType->tp_name, myType->tp_name); + return false; + } + return true; +} + + } // namespace Shiboken diff --git a/libshiboken/basewrapper.h b/libshiboken/basewrapper.h index b6a9b7950..4031fb856 100644 --- a/libshiboken/basewrapper.h +++ b/libshiboken/basewrapper.h @@ -190,6 +190,11 @@ LIBSHIBOKEN_API bool setCppPointer(SbkBaseWrapper* wrapper, PyTypeObject* desire LIBSHIBOKEN_API void setTypeUserData(SbkBaseWrapper* wrapper, void* user_data, DeleteUserDataFunc d_func); LIBSHIBOKEN_API void* getTypeUserData(SbkBaseWrapper* wrapper); +/** +* Returns true if the constructor of \p ctorType can be called for a instance of type \p myType. +* \note This function set a python error when returning false. +*/ +LIBSHIBOKEN_API bool canCallConstructor(PyTypeObject* myType, PyTypeObject* ctorType); /** * Shiboken_TypeCheck macro performs a type check using the values registered with SbkType<>() template. |