Skip to content
This repository was archived by the owner on Dec 10, 2018. It is now read-only.

Commit

Permalink
Drop to_bytes() in favor of inline UTF-8 encoding
Browse files Browse the repository at this point in the history
We should only need to perform the UTF-8 encoding step when we are handed
a Unicode string, and performing the encoding inline lets Cython translate
this directly into a call to PyUnicode_AsUTF8String().
  • Loading branch information
jparise committed Jun 23, 2016
1 parent 5aa0b90 commit 0cd962f
Showing 1 changed file with 4 additions and 9 deletions.
13 changes: 4 additions & 9 deletions thriftpy/transport/memory/cymemory.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ from thriftpy.transport.cybase cimport (
)


def to_bytes(s):
try:
return s.encode("utf-8")
except Exception:
return s


cdef class TCyMemoryBuffer(CyTransportBase):
cdef TCyBuffer buf

Expand Down Expand Up @@ -64,7 +57,8 @@ cdef class TCyMemoryBuffer(CyTransportBase):
return self.get_string(sz)

def write(self, data):
data = to_bytes(data)
if isinstance(data, unicode):
data = (<unicode>data).encode('utf-8')

cdef int sz = len(data)
return self.c_write(data, sz)
Expand All @@ -88,5 +82,6 @@ cdef class TCyMemoryBuffer(CyTransportBase):
return self._getvalue()

def setvalue(self, value):
value = to_bytes(value)
if isinstance(value, unicode):
value = (<unicode>value).encode('utf-8')
self._setvalue(len(value), value)

0 comments on commit 0cd962f

Please sign in to comment.