Refactor Cython code (#328)

_msgpack -> _cmsgpack
This commit is contained in:
INADA Naoki 2018-11-14 16:35:37 +09:00 committed by GitHub
parent 2b5f59166b
commit d782464c91
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 17 additions and 40 deletions

View file

@ -22,7 +22,7 @@ if os.environ.get('MSGPACK_PUREPYTHON'):
from msgpack.fallback import Packer, unpackb, Unpacker
else:
try:
from msgpack._msgpack import Packer, unpackb, Unpacker
from msgpack._cmsgpack import Packer, unpackb, Unpacker
except ImportError:
from msgpack.fallback import Packer, unpackb, Unpacker

4
msgpack/_cmsgpack.pyx Normal file
View file

@ -0,0 +1,4 @@
# coding: utf-8
#cython: embedsignature=True, c_string_encoding=ascii, language_level=2
include "_packer.pyx"
include "_unpacker.pyx"

View file

@ -1,4 +0,0 @@
# coding: utf-8
#cython: embedsignature=True, c_string_encoding=ascii
include "_packer.pyx"
include "_unpacker.pyx"

View file

@ -1,9 +1,7 @@
# coding: utf-8
#cython: embedsignature=True, c_string_encoding=ascii
from cpython cimport *
from cpython.version cimport PY_MAJOR_VERSION
from cpython.exc cimport PyErr_WarnEx
from cpython.bytearray cimport PyByteArray_Check, PyByteArray_CheckExact
from msgpack import ExtType
@ -11,8 +9,6 @@ from msgpack import ExtType
cdef extern from "Python.h":
int PyMemoryView_Check(object obj)
int PyByteArray_Check(object obj)
int PyByteArray_CheckExact(object obj)
char* PyUnicode_AsUTF8AndSize(object obj, Py_ssize_t *l) except NULL
@ -204,7 +200,7 @@ cdef class Packer(object):
elif PyBytesLike_CheckExact(o) if strict_types else PyBytesLike_Check(o):
L = len(o)
if L > ITEM_LIMIT:
raise ValueError("%s is too large" % type(o).__name__)
PyErr_Format(ValueError, b"%.200s object is too large", Py_TYPE(o).tp_name)
rawval = o
ret = msgpack_pack_bin(&self.pk, L)
if ret == 0:
@ -280,7 +276,7 @@ cdef class Packer(object):
default_used = 1
continue
else:
raise TypeError("can't serialize %r" % (o,))
PyErr_Format(TypeError, b"can not serialize '%.200s' object", Py_TYPE(o).tp_name)
return ret
cpdef pack(self, object obj):

View file

@ -1,26 +1,6 @@
# coding: utf-8
#cython: embedsignature=True, c_string_encoding=ascii
from cpython.version cimport PY_MAJOR_VERSION
from cpython.bytes cimport (
PyBytes_AsString,
PyBytes_FromStringAndSize,
PyBytes_Size,
)
from cpython.buffer cimport (
Py_buffer,
PyObject_CheckBuffer,
PyObject_GetBuffer,
PyBuffer_Release,
PyBuffer_IsContiguous,
PyBUF_READ,
PyBUF_SIMPLE,
PyBUF_FULL_RO,
)
from cpython.mem cimport PyMem_Malloc, PyMem_Free
from cpython.object cimport PyCallable_Check
from cpython.ref cimport Py_DECREF
from cpython.exc cimport PyErr_WarnEx
from cpython cimport *
cdef extern from "Python.h":
ctypedef struct PyObject