Skip to content

Commit

Permalink
Merge b30ad91 into a3249f1
Browse files Browse the repository at this point in the history
  • Loading branch information
jakirkham committed Nov 24, 2018
2 parents a3249f1 + b30ad91 commit 944886f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
5 changes: 0 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
39 changes: 26 additions & 13 deletions src/cybuffer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ include "config.pxi"

cimport cybuffer

cimport libc
cimport libc.string

cimport cython
cimport cython.view

Expand Down Expand Up @@ -38,18 +41,28 @@ cdef extern from "Python.h":

cdef extern from *:
"""
#define UCS2_TC "H"
#define UCS4_TC "I"
#define CHAR_TC "c"
#define UCS2_TC "u"
#define UCS4_TC "w"
#define UINT8_TC "B"
#define UINT16_TC "H"
#define UINT32_TC "I"
#define PyList_SET_ITEM_INC(l, i, o) \
Py_INCREF(o); PyList_SET_ITEM(l, i, o)
#define PyTuple_SET_ITEM_INC(l, i, o) \
Py_INCREF(o); PyTuple_SET_ITEM(l, i, o)
"""

char* CHAR_TC
char* UCS2_TC
char* UCS4_TC

char* UINT8_TC
char* UINT16_TC
char* UINT32_TC

void PyList_SET_ITEM_INC(object, Py_ssize_t, object)
void PyTuple_SET_ITEM_INC(object, Py_ssize_t, object)

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 944886f

Please sign in to comment.