diff --git a/src/cybuffer.pyx b/src/cybuffer.pyx index bd39f0b..883d42d 100644 --- a/src/cybuffer.pyx +++ b/src/cybuffer.pyx @@ -27,7 +27,7 @@ from array import array from struct import Struct IF PY2K: - import binascii + from binascii import hexlify include "version.pxi" @@ -35,6 +35,8 @@ include "version.pxi" cdef extern from "Python.h": size_t Py_UNICODE_SIZE + object PyMemoryView_FromObject(object obj) + cdef extern from *: """ @@ -269,11 +271,18 @@ cdef class cybuffer(object): cpdef str hex(self): + cdef object d cdef str s + + if self.c_contiguous: + d = self + else: + d = self.tobytes() + if PY2K: - s = binascii.hexlify(self.tobytes()) + s = hexlify(d) else: - s = self.tobytes().hex() + s = PyMemoryView_FromObject(d).hex() return s