Skip to content

Commit

Permalink
Merge pull request #12 from jakirkham/hex_ensure_c_contiguous
Browse files Browse the repository at this point in the history
Use `tobytes` in `hex` to make data C contiguous
  • Loading branch information
jakirkham committed Nov 24, 2018
2 parents 60b6e99 + 38d2abd commit 3a3d414
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/cybuffer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ from array import array
from struct import Struct

IF PY2K:
import binascii
from binascii import hexlify

include "version.pxi"


cdef extern from "Python.h":
size_t Py_UNICODE_SIZE

object PyMemoryView_FromObject(object obj)


cdef extern from *:
"""
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 3a3d414

Please sign in to comment.