diff --git a/setup.py b/setup.py index 118aacc..0a8c03b 100644 --- a/setup.py +++ b/setup.py @@ -69,11 +69,6 @@ def run_tests(self): "DEF PY2K = " + str(sys.version_info.major == 2) + "\n", "DEF PY3K = " + str(sys.version_info.major == 3) + "\n" ]) - if sys.version_info.major < 4: # pragma: no branch - Py_UNICODE_SIZE = array.array('u').itemsize - f.writelines([ - "DEF Py_UNICODE_SIZE = " + str(Py_UNICODE_SIZE) + "\n", - ]) with open("src/version.pxi", "w") as f: f.writelines([ diff --git a/src/cybuffer.pyx b/src/cybuffer.pyx index a56c30c..836df50 100644 --- a/src/cybuffer.pyx +++ b/src/cybuffer.pyx @@ -2,6 +2,9 @@ include "config.pxi" cimport cybuffer +cimport libc +cimport libc.string + cimport cython cimport cython.view @@ -38,8 +41,13 @@ cdef extern from "Python.h": cdef extern from *: """ - #define UCS2_TC "H" - #define UCS4_TC "I" + #define UINT8_TC "B" + #define UINT16_TC "H" + #define UINT32_TC "I" + + #define CHAR_TC "c" + #define UCS2_TC "u" + #define UCS4_TC "w" #define PyList_SET_ITEM_INC(l, i, o) \ Py_INCREF(o); PyList_SET_ITEM(l, i, o) @@ -47,6 +55,11 @@ cdef extern from *: Py_INCREF(o); PyTuple_SET_ITEM(l, i, o) """ + char* UINT8_TC + char* UINT16_TC + char* UINT32_TC + + char* CHAR_TC char* UCS2_TC char* UCS4_TC @@ -177,18 +190,18 @@ cdef class cybuffer(object): # Workaround some special cases with the builtin array if (PY2K or PY3K) and isinstance(self.obj, array): - # Cast to appropriate format with given itemsize - typecode = self.obj.typecode - if typecode == "u": - if PY2K: - self.itemsize = Py_UNICODE_SIZE - if Py_UNICODE_SIZE == 2: - self._format = UCS2_TC - elif Py_UNICODE_SIZE == 4: - self._format = UCS4_TC - elif PY2K and typecode not in "Bc": + # Correct itemsize and format on Python 2 + if PY2K: self.itemsize = self.obj.itemsize - self._format = typecode + self._format = self.obj.typecode + + # Cast to appropriate format + if libc.string.strcmp(self._format, UCS2_TC) == 0: + self._format = UINT16_TC + elif libc.string.strcmp(self._format, UCS4_TC) == 0: + self._format = UINT32_TC + elif PY2K and libc.string.strcmp(self._format, CHAR_TC) == 0: + self._format = UINT8_TC # Adjust shape and strides based on casting if PY2K and self.itemsize != 1: