use self.payload[:self.payload_len] for getting python string

This is more Cythonic way, documented here
http://cython.readthedocs.io/en/latest/src/tutorial/strings.html#passing-byte-strings

It compiles to
    __Pyx_PyBytes_FromStringAndSize(
        __pyx_v_self->payload + 0,
        __pyx_v_self->payload_len - 0)

Which is the Cython macro wrapper which handles both Python 2 and
Python3.

Fixes #17
This commit is contained in:
WGH 2016-10-27 23:49:16 +03:00
parent 9c74558250
commit 949f894256
2 changed files with 385 additions and 426 deletions

File diff suppressed because it is too large Load Diff

View File

@ -100,12 +100,7 @@ cdef class Packet:
def get_payload(self):
"""Return payload as Python string."""
cdef object py_string
if cpython.version.PY_MAJOR_VERSION >= 3:
py_string = PyBytes_FromStringAndSize(
self.payload, self.payload_len)
else:
py_string = PyString_FromStringAndSize(
self.payload, self.payload_len)
py_string = self.payload[:self.payload_len]
return py_string
cpdef Py_ssize_t get_payload_len(self):