diff options
author | Renato Filho <renato.filho@openbossa.org> | 2011-10-05 19:33:37 -0300 |
---|---|---|
committer | Hugo Parente Lima <hugo.pl@gmail.com> | 2012-03-08 16:18:20 -0300 |
commit | 56e713fd3ca68fe2fc615426469fe7a0e209c3ed (patch) | |
tree | a38380a0e1ee3e17e22b5d56a938547b36fe4205 /libshiboken | |
parent | f10f587d43a27500540bee7ae4251a78a4a444a2 (diff) |
Fixed char conversion to work with both python 3.x and 2.x
Diffstat (limited to 'libshiboken')
-rw-r--r-- | libshiboken/conversions.h | 32 |
1 files changed, 29 insertions, 3 deletions
diff --git a/libshiboken/conversions.h b/libshiboken/conversions.h index ee4666846..da6407985 100644 --- a/libshiboken/conversions.h +++ b/libshiboken/conversions.h @@ -363,8 +363,8 @@ struct CharConverter if (PyBytes_Check(pyobj)) { assert(PyBytes_GET_SIZE(pyobj) == 1); // This check is made on SbkChar_Check return PyBytes_AS_STRING(pyobj)[0]; - } else if (PyLong_Check(pyobj)) { - PY_LONG_LONG result = PyLong_AsLongLong(pyobj); + } else if (PyInt_Check(pyobj)) { + PY_LONG_LONG result = PyInt_AsUnsignedLongLongMask(pyobj); if (OverFlowChecker<CharType>::check(result)) PyErr_SetObject(PyExc_OverflowError, 0); return result; @@ -378,12 +378,38 @@ struct CharConverter template <> struct Converter<unsigned long> : Converter_PyULongInt<unsigned long> {}; template <> struct Converter<unsigned int> : Converter_PyULongInt<unsigned int> {}; -template <> struct Converter<char> : CharConverter<char> { +template <> struct Converter<char> : CharConverter<char> +{ // Should we really return a string? using CharConverter<char>::toPython; + using CharConverter<char>::isConvertible; + using CharConverter<char>::toCpp; + + + static inline bool isConvertible(PyObject* pyobj) { + return SbkChar_Check(pyobj); + } + static inline PyObject* toPython(const char& cppObj) { return Shiboken::String::fromFormat("%c", cppObj); } + + static char toCpp(PyObject* pyobj) + { + if (PyBytes_Check(pyobj)) { + assert(PyBytes_GET_SIZE(pyobj) == 1); // This check is made on SbkChar_Check + return PyBytes_AS_STRING(pyobj)[0]; + } else if (PyInt_Check(pyobj)) { + PY_LONG_LONG result = PyInt_AsUnsignedLongLongMask(pyobj); + if (OverFlowChecker<char>::check(result)) + PyErr_SetObject(PyExc_OverflowError, 0); + return result; + } else if (Shiboken::String::check(pyobj)) { + return Shiboken::String::toCString(pyobj)[0]; + } else { + return 0; + } + } }; template <> struct Converter<signed char> : CharConverter<signed char> {}; template <> struct Converter<unsigned char> : CharConverter<unsigned char> {}; |