diff options
author | Marcelo Lira <marcelo.lira@openbossa.org> | 2009-12-07 16:58:56 -0300 |
---|---|---|
committer | Marcelo Lira <marcelo.lira@openbossa.org> | 2009-12-07 17:13:20 -0300 |
commit | 16ff7b614c9caf8837f72e7143984b252b71f63e (patch) | |
tree | 1643537cc031b92dea230121f2c97d094717f7f6 /libshiboken | |
parent | c1c196a2338d3529c2ec71cc6e375ef64e926689 (diff) |
Added Shiboken_TypeCheck macro that makes use of PyType<T>().
The Shiboken_TypeCheck calls Python's PyObject_TypeCheck using the
type pointer stored in PyType<T> for the type being checked.
Conversion<T*>::toCpp(pyobj) converter tries first to convert pyobj
to the Python wrapper for type T and only second to any of the
convertible types. If pyobj is neither of those, 0 is returned as
the C++ object. This works fine for Py_None and invalid values are
not expected to be passed because the generated code checked the
types first.
Diffstat (limited to 'libshiboken')
-rw-r--r-- | libshiboken/basewrapper.h | 6 | ||||
-rw-r--r-- | libshiboken/conversions.h | 6 |
2 files changed, 9 insertions, 3 deletions
diff --git a/libshiboken/basewrapper.h b/libshiboken/basewrapper.h index fd7825f1a..a9279ef6c 100644 --- a/libshiboken/basewrapper.h +++ b/libshiboken/basewrapper.h @@ -118,6 +118,12 @@ LIBSHIBOKEN_API void removeParent(PyBaseWrapper* child); */ LIBSHIBOKEN_API void destroyParentInfo(PyBaseWrapper* obj, bool removeFromParent = true); + +/** + * Shiboken_TypeCheck macro performs a type check using the values registered with PyType<>() template. + */ +#define Shiboken_TypeCheck(pyobj, type) (PyObject_TypeCheck(pyobj, PyType<type>())) + #define PyBaseWrapper_Check(op) PyObject_TypeCheck(op, &Shiboken::PyBaseWrapper_Type) #define PyBaseWrapper_CheckExact(op) ((op)->ob_type == &Shiboken::PyBaseWrapper_Type) diff --git a/libshiboken/conversions.h b/libshiboken/conversions.h index ed7d8b72b..391190331 100644 --- a/libshiboken/conversions.h +++ b/libshiboken/conversions.h @@ -126,11 +126,11 @@ struct Converter<T*> : Converter<T> } static T* toCpp(PyObject* pyobj) { - if (pyobj == Py_None) - return 0; + if (Shiboken_TypeCheck(pyobj, T)) + return (T*) ((Shiboken::PyBaseWrapper*) pyobj)->cptr; else if (Converter<T>::isConvertible(pyobj)) return Converter<T>::copyCppObject(Converter<T>::toCpp(pyobj)); - return (T*) ((Shiboken::PyBaseWrapper*) pyobj)->cptr; + return 0; } }; template <typename T> struct Converter<const T*> : Converter<T*> {}; |