summaryrefslogtreecommitdiffstats
path: root/libshiboken
diff options
context:
space:
mode:
authorRenato Filho <renato.filho@openbossa.org>2011-10-05 19:33:37 -0300
committerHugo Parente Lima <hugo.pl@gmail.com>2011-10-06 17:04:20 -0300
commit6b8ff873c97a63b67a0a5bddf98373df47eac67c (patch)
treeeab03664d90abae20ee8491ed3c1dffd2387c1bc /libshiboken
parent20461c9aff5e89cba3be0198052f016c4899191e (diff)
downloadshiboken-6b8ff873c97a63b67a0a5bddf98373df47eac67c.tar.gz
shiboken-6b8ff873c97a63b67a0a5bddf98373df47eac67c.tar.xz
shiboken-6b8ff873c97a63b67a0a5bddf98373df47eac67c.zip
Fixed char conversion to work with both python 3.x and 2.x
Diffstat (limited to 'libshiboken')
-rw-r--r--libshiboken/conversions.h32
1 files changed, 29 insertions, 3 deletions
diff --git a/libshiboken/conversions.h b/libshiboken/conversions.h
index ee466684..da640798 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> {};