From 88eb0143f7a1ebc2bc958ff689a98550370685de Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 13 Nov 2018 22:19:52 +0900 Subject: [PATCH 1/7] Refactor Cython code --- msgpack/_packer.pyx | 9 +++------ msgpack/_unpacker.pyx | 21 +-------------------- 2 files changed, 4 insertions(+), 26 deletions(-) diff --git a/msgpack/_packer.pyx b/msgpack/_packer.pyx index 2643f858..730f7823 100644 --- a/msgpack/_packer.pyx +++ b/msgpack/_packer.pyx @@ -2,8 +2,7 @@ #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 +10,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 +201,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 +277,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): diff --git a/msgpack/_unpacker.pyx b/msgpack/_unpacker.pyx index e168587b..5e0eac95 100644 --- a/msgpack/_unpacker.pyx +++ b/msgpack/_unpacker.pyx @@ -1,26 +1,7 @@ # 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 import * cdef extern from "Python.h": ctypedef struct PyObject From 80016ea72a901172842918ac477682eca1af16de Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 13 Nov 2018 22:22:48 +0900 Subject: [PATCH 2/7] Specify language level --- msgpack/_msgpack.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/msgpack/_msgpack.pyx b/msgpack/_msgpack.pyx index 4381394f..a48d5b51 100644 --- a/msgpack/_msgpack.pyx +++ b/msgpack/_msgpack.pyx @@ -1,4 +1,4 @@ # coding: utf-8 -#cython: embedsignature=True, c_string_encoding=ascii +#cython: embedsignature=True, c_string_encoding=ascii, language_level=2 include "_packer.pyx" include "_unpacker.pyx" From b6535b3e974f4cbda1d4ece2440a2f9b2252c06e Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 13 Nov 2018 22:25:43 +0900 Subject: [PATCH 3/7] _msgpack -> _cmsgpack --- Makefile | 2 +- msgpack/__init__.py | 2 +- msgpack/{_msgpack.pyx => _cmsgpack.pyx} | 0 setup.py | 6 +++--- 4 files changed, 5 insertions(+), 5 deletions(-) rename msgpack/{_msgpack.pyx => _cmsgpack.pyx} (100%) diff --git a/Makefile b/Makefile index ff9a482b..b65aa859 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ all: cython .PHONY: cython cython: - cython --cplus msgpack/_msgpack.pyx + cython --cplus msgpack/_cmsgpack.pyx .PHONY: test test: diff --git a/msgpack/__init__.py b/msgpack/__init__.py index 7c5d4c0c..7493c4c6 100644 --- a/msgpack/__init__.py +++ b/msgpack/__init__.py @@ -22,7 +22,7 @@ def __new__(cls, code, data): 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 diff --git a/msgpack/_msgpack.pyx b/msgpack/_cmsgpack.pyx similarity index 100% rename from msgpack/_msgpack.pyx rename to msgpack/_cmsgpack.pyx diff --git a/setup.py b/setup.py index 8b8f7bdf..eb9403f1 100755 --- a/setup.py +++ b/setup.py @@ -68,7 +68,7 @@ def build_extension(self, ext): if have_cython: class Sdist(sdist): def __init__(self, *args, **kwargs): - cythonize('msgpack/_msgpack.pyx') + cythonize('msgpack/_cmsgpack.pyx') sdist.__init__(self, *args, **kwargs) else: Sdist = sdist @@ -84,8 +84,8 @@ def __init__(self, *args, **kwargs): ext_modules = [] if not hasattr(sys, 'pypy_version_info'): - ext_modules.append(Extension('msgpack._msgpack', - sources=['msgpack/_msgpack.cpp'], + ext_modules.append(Extension('msgpack._cmsgpack', + sources=['msgpack/_cmsgpack.cpp'], libraries=libraries, include_dirs=['.'], define_macros=macros, From 74f21608c827ed33b8e2972f871e57251dd38f47 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 13 Nov 2018 22:29:03 +0900 Subject: [PATCH 4/7] Fix appveyor --- appveyor.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/appveyor.yml b/appveyor.yml index 72b334a0..f0e21fc7 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -6,8 +6,9 @@ environment: install: # We need wheel installed to build wheels + - "%PYTHON%\\python.exe -m pip install -U pip" - "%PYTHON%\\python.exe -m pip install -U cython" - - "%PYTHON%\\Scripts\\cython --cplus msgpack/_packer.pyx msgpack/_unpacker.pyx" + - "%PYTHON%\\Scripts\\cython --cplus msgpack/_cmsgpack.pyx" build: off From 5661f8497dc963b6bdb4a715ae8f9916234a2ce6 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 13 Nov 2018 22:30:06 +0900 Subject: [PATCH 5/7] Fix travis --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 822ca9ad..1adbdc25 100644 --- a/.travis.yml +++ b/.travis.yml @@ -45,7 +45,7 @@ install: script: - python -c 'import sys; print(hex(sys.maxsize))' - - python -c 'from msgpack import _msgpack' + - python -c 'from msgpack import _cmsgpack' - pytest -v test - MSGPACK_PUREPYTHON=x pytest -v test From 9053e7c3d7b95cd172d67e3019e6bd91a434c68d Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Wed, 14 Nov 2018 14:31:00 +0900 Subject: [PATCH 6/7] import -> cimport --- msgpack/_packer.pyx | 1 - msgpack/_unpacker.pyx | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/msgpack/_packer.pyx b/msgpack/_packer.pyx index 730f7823..3be593fb 100644 --- a/msgpack/_packer.pyx +++ b/msgpack/_packer.pyx @@ -1,5 +1,4 @@ # coding: utf-8 -#cython: embedsignature=True, c_string_encoding=ascii from cpython cimport * from cpython.bytearray cimport PyByteArray_Check, PyByteArray_CheckExact diff --git a/msgpack/_unpacker.pyx b/msgpack/_unpacker.pyx index 5e0eac95..aeebe2ac 100644 --- a/msgpack/_unpacker.pyx +++ b/msgpack/_unpacker.pyx @@ -1,7 +1,6 @@ # coding: utf-8 -#cython: embedsignature=True, c_string_encoding=ascii -from cpython import * +from cpython cimport * cdef extern from "Python.h": ctypedef struct PyObject From 4bfbd008a580c6a49fe66f02b3e575fe35f87b73 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Wed, 14 Nov 2018 14:55:50 +0900 Subject: [PATCH 7/7] Fix docker runtests.sh --- docker/runtests.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/runtests.sh b/docker/runtests.sh index f6c2c68e..c6bbf60f 100755 --- a/docker/runtests.sh +++ b/docker/runtests.sh @@ -8,7 +8,7 @@ for V in cp36-cp36m cp35-cp35m cp27-cp27m cp27-cp27mu; do $PYBIN/pip install pytest pushd test # prevent importing msgpack package in current directory. $PYBIN/python -c 'import sys; print(hex(sys.maxsize))' - $PYBIN/python -c 'from msgpack import _msgpack' # Ensure extension is available + $PYBIN/python -c 'from msgpack import _cmsgpack' # Ensure extension is available $PYBIN/pytest -v . popd done