diff options
-rw-r--r-- | libshiboken/sbkstring.cpp | 73 |
1 files changed, 47 insertions, 26 deletions
diff --git a/libshiboken/sbkstring.cpp b/libshiboken/sbkstring.cpp index ebe1efb2f..c20c487e7 100644 --- a/libshiboken/sbkstring.cpp +++ b/libshiboken/sbkstring.cpp @@ -30,28 +30,24 @@ namespace String bool checkType(PyTypeObject* type) { -#if PY_MAJOR_VERSION >= 3 - return type == &PyUnicode_Type; -#else - return type == &PyString_Type || - type == &PyUnicode_Type; + return type == &PyUnicode_Type +#if PY_MAJOR_VERSION < 3 + || type == &PyString_Type #endif + ; } bool check(PyObject* obj) { return obj == Py_None || -#if PY_MAJOR_VERSION >= 3 - PyUnicode_Check(obj); -#else - PyString_Check(obj); +#if PY_MAJOR_VERSION < 3 + PyString_Check(obj) || #endif + PyUnicode_Check(obj); } bool checkChar(PyObject* pyobj) { - if (PyBytes_Check(pyobj) && (PyBytes_GET_SIZE(pyobj) == 1)) - return true; if (check(pyobj) && (len(pyobj) == 1)) return true; @@ -85,15 +81,25 @@ const char* toCString(PyObject* str) bool concat(PyObject** val1, PyObject* val2) { -#if PY_MAJOR_VERSION >= 3 - PyObject* result = PyUnicode_Concat(*val1, val2); - Py_DECREF(*val1); - *val1 = result; - return true; -#else - PyString_Concat(val1, val2); - return true; + if (PyUnicode_Check(val1) && PyUnicode_Check(val2)) { + PyObject* result = PyUnicode_Concat(*val1, val2); + Py_DECREF(*val1); + *val1 = result; + return true; + } + + if (PyBytes_Check(val1) && PyBytes_Check(val2)) { + PyBytes_Concat(val1, val2); + return true; + } + +#if PY_MAJOR_VERSION < 3 + if (PyString_Check(val1) && PyString_Check(val2)) { + PyString_Concat(val1, val2); + return true; + } #endif + return false; } PyObject* fromFormat(const char* format, ...) @@ -121,25 +127,40 @@ PyObject* fromStringAndSize(const char* str, Py_ssize_t size) int compare(PyObject* val1, const char* val2) { + if (PyUnicode_Check(val1)) #if PY_MAJOR_VERSION >= 3 - return PyUnicode_CompareWithASCIIString(val1, val2); + return PyUnicode_CompareWithASCIIString(val1, val2); #else - return strcmp(PyString_AS_STRING(val1), val2); + { + PyObject* uVal2 = PyUnicode_FromFormat("%s", val2); + bool result = PyUnicode_Compare(val1, uVal2); + Py_XDECREF(uVal2); + return result; + } + if (PyString_Check(val1)) + return strcmp(PyString_AS_STRING(val1), val2); #endif + return 0; + } Py_ssize_t len(PyObject* str) { - printf("TYPE NAME: %s\n", Py_TYPE(str)->tp_name); if (str == Py_None) return 0; -#if PY_MAJOR_VERSION >= 3 - return PyUnicode_GET_SIZE(str); -#else - return PyString_GET_SIZE(str); + if (PyUnicode_Check(str)) + return PyUnicode_GET_SIZE(str); + + if (PyBytes_Check(str)) + return PyBytes_GET_SIZE(str); + +#if PY_MAJOR_VERSION < 3 + if (PyString_Check(str)) + return PyString_GET_SIZE(str); #endif + return 0; } } // namespace String |