Skip to content

Commit d782464

Browse files
authored
Refactor Cython code (#328)
_msgpack -> _cmsgpack
1 parent 2b5f591 commit d782464

File tree

10 files changed

+17
-40
lines changed

10 files changed

+17
-40
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ install:
4545

4646
script:
4747
- python -c 'import sys; print(hex(sys.maxsize))'
48-
- python -c 'from msgpack import _msgpack'
48+
- python -c 'from msgpack import _cmsgpack'
4949
- pytest -v test
5050
- MSGPACK_PUREPYTHON=x pytest -v test
5151

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ all: cython
44

55
.PHONY: cython
66
cython:
7-
cython --cplus msgpack/_msgpack.pyx
7+
cython --cplus msgpack/_cmsgpack.pyx
88

99
.PHONY: test
1010
test:

appveyor.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ environment:
66

77
install:
88
# We need wheel installed to build wheels
9+
- "%PYTHON%\\python.exe -m pip install -U pip"
910
- "%PYTHON%\\python.exe -m pip install -U cython"
10-
- "%PYTHON%\\Scripts\\cython --cplus msgpack/_packer.pyx msgpack/_unpacker.pyx"
11+
- "%PYTHON%\\Scripts\\cython --cplus msgpack/_cmsgpack.pyx"
1112

1213
build: off
1314

docker/runtests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ for V in cp36-cp36m cp35-cp35m cp27-cp27m cp27-cp27mu; do
88
$PYBIN/pip install pytest
99
pushd test # prevent importing msgpack package in current directory.
1010
$PYBIN/python -c 'import sys; print(hex(sys.maxsize))'
11-
$PYBIN/python -c 'from msgpack import _msgpack' # Ensure extension is available
11+
$PYBIN/python -c 'from msgpack import _cmsgpack' # Ensure extension is available
1212
$PYBIN/pytest -v .
1313
popd
1414
done

msgpack/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __new__(cls, code, data):
2222
from msgpack.fallback import Packer, unpackb, Unpacker
2323
else:
2424
try:
25-
from msgpack._msgpack import Packer, unpackb, Unpacker
25+
from msgpack._cmsgpack import Packer, unpackb, Unpacker
2626
except ImportError:
2727
from msgpack.fallback import Packer, unpackb, Unpacker
2828

msgpack/_cmsgpack.pyx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# coding: utf-8
2+
#cython: embedsignature=True, c_string_encoding=ascii, language_level=2
3+
include "_packer.pyx"
4+
include "_unpacker.pyx"

msgpack/_msgpack.pyx

Lines changed: 0 additions & 4 deletions
This file was deleted.

msgpack/_packer.pyx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
# coding: utf-8
2-
#cython: embedsignature=True, c_string_encoding=ascii
32

43
from cpython cimport *
5-
from cpython.version cimport PY_MAJOR_VERSION
6-
from cpython.exc cimport PyErr_WarnEx
4+
from cpython.bytearray cimport PyByteArray_Check, PyByteArray_CheckExact
75

86
from msgpack import ExtType
97

108

119
cdef extern from "Python.h":
1210

1311
int PyMemoryView_Check(object obj)
14-
int PyByteArray_Check(object obj)
15-
int PyByteArray_CheckExact(object obj)
1612
char* PyUnicode_AsUTF8AndSize(object obj, Py_ssize_t *l) except NULL
1713

1814

@@ -204,7 +200,7 @@ cdef class Packer(object):
204200
elif PyBytesLike_CheckExact(o) if strict_types else PyBytesLike_Check(o):
205201
L = len(o)
206202
if L > ITEM_LIMIT:
207-
raise ValueError("%s is too large" % type(o).__name__)
203+
PyErr_Format(ValueError, b"%.200s object is too large", Py_TYPE(o).tp_name)
208204
rawval = o
209205
ret = msgpack_pack_bin(&self.pk, L)
210206
if ret == 0:
@@ -280,7 +276,7 @@ cdef class Packer(object):
280276
default_used = 1
281277
continue
282278
else:
283-
raise TypeError("can't serialize %r" % (o,))
279+
PyErr_Format(TypeError, b"can not serialize '%.200s' object", Py_TYPE(o).tp_name)
284280
return ret
285281

286282
cpdef pack(self, object obj):

msgpack/_unpacker.pyx

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,6 @@
11
# coding: utf-8
2-
#cython: embedsignature=True, c_string_encoding=ascii
32

4-
from cpython.version cimport PY_MAJOR_VERSION
5-
from cpython.bytes cimport (
6-
PyBytes_AsString,
7-
PyBytes_FromStringAndSize,
8-
PyBytes_Size,
9-
)
10-
from cpython.buffer cimport (
11-
Py_buffer,
12-
PyObject_CheckBuffer,
13-
PyObject_GetBuffer,
14-
PyBuffer_Release,
15-
PyBuffer_IsContiguous,
16-
PyBUF_READ,
17-
PyBUF_SIMPLE,
18-
PyBUF_FULL_RO,
19-
)
20-
from cpython.mem cimport PyMem_Malloc, PyMem_Free
21-
from cpython.object cimport PyCallable_Check
22-
from cpython.ref cimport Py_DECREF
23-
from cpython.exc cimport PyErr_WarnEx
3+
from cpython cimport *
244

255
cdef extern from "Python.h":
266
ctypedef struct PyObject

setup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def build_extension(self, ext):
6868
if have_cython:
6969
class Sdist(sdist):
7070
def __init__(self, *args, **kwargs):
71-
cythonize('msgpack/_msgpack.pyx')
71+
cythonize('msgpack/_cmsgpack.pyx')
7272
sdist.__init__(self, *args, **kwargs)
7373
else:
7474
Sdist = sdist
@@ -84,8 +84,8 @@ def __init__(self, *args, **kwargs):
8484

8585
ext_modules = []
8686
if not hasattr(sys, 'pypy_version_info'):
87-
ext_modules.append(Extension('msgpack._msgpack',
88-
sources=['msgpack/_msgpack.cpp'],
87+
ext_modules.append(Extension('msgpack._cmsgpack',
88+
sources=['msgpack/_cmsgpack.cpp'],
8989
libraries=libraries,
9090
include_dirs=['.'],
9191
define_macros=macros,

0 commit comments

Comments
 (0)