Skip to content

Commit 3478406

Browse files
committed
Fix tests.
1 parent 477d3b1 commit 3478406

File tree

4 files changed

+39
-29
lines changed

4 files changed

+39
-29
lines changed

msgpack/_msgpack.pyx

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
# coding: utf-8
22
#cython: embedsignature=True
33

4-
import warnings
5-
64
from cpython cimport *
75
cdef extern from "Python.h":
86
ctypedef char* const_char_ptr "const char*"
97
ctypedef char* const_void_ptr "const void*"
108
ctypedef struct PyObject
119
cdef int PyObject_AsReadBuffer(object o, const_void_ptr* buff, Py_ssize_t* buf_len) except -1
12-
char* __FILE__
13-
int __LINE__
1410

1511
from libc.stdlib cimport *
1612
from libc.string cimport *
1713
from libc.limits cimport *
18-
14+
import warnings
1915

2016
cdef extern from "pack.h":
2117
struct msgpack_packer:
@@ -218,7 +214,9 @@ cdef extern from "unpack.h":
218214
void template_init(template_context* ctx)
219215
object template_data(template_context* ctx)
220216

221-
cdef inline init_ctx(template_context *ctx, object object_hook, object object_pairs_hook, object list_hook, bint use_list, encoding, unicode_errors):
217+
cdef inline init_ctx(template_context *ctx,
218+
object object_hook, object object_pairs_hook, object list_hook,
219+
bint use_list, char* encoding, char* unicode_errors):
222220
template_init(ctx)
223221
ctx.user.use_list = use_list
224222
ctx.user.object_hook = ctx.user.list_hook = <PyObject*>NULL
@@ -244,20 +242,8 @@ cdef inline init_ctx(template_context *ctx, object object_hook, object object_pa
244242
raise TypeError("list_hook must be a callable.")
245243
ctx.user.list_hook = <PyObject*>list_hook
246244

247-
if encoding is None:
248-
ctx.user.encoding = NULL
249-
ctx.user.unicode_errors = NULL
250-
else:
251-
if isinstance(encoding, unicode):
252-
_bencoding = encoding.encode('ascii')
253-
else:
254-
_bencoding = encoding
255-
ctx.user.encoding = PyBytes_AsString(_bencoding)
256-
if isinstance(unicode_errors, unicode):
257-
_berrors = unicode_errors.encode('ascii')
258-
else:
259-
_berrors = unicode_errors
260-
ctx.user.unicode_errors = PyBytes_AsString(_berrors)
245+
ctx.user.encoding = encoding
246+
ctx.user.unicode_errors = unicode_errors
261247

262248
def unpackb(object packed, object object_hook=None, object list_hook=None,
263249
use_list=None, encoding=None, unicode_errors="strict",
@@ -273,13 +259,26 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
273259

274260
cdef char* buf
275261
cdef Py_ssize_t buf_len
262+
cdef char* cenc = NULL
263+
cdef char* cerr = NULL
276264

277265
PyObject_AsReadBuffer(packed, <const_void_ptr*>&buf, &buf_len)
278266

279267
if use_list is None:
280268
warnings.warn("Set use_list explicitly.", category=DeprecationWarning, stacklevel=1)
281269
use_list = 0
282-
init_ctx(&ctx, object_hook, object_pairs_hook, list_hook, use_list, encoding, unicode_errors)
270+
271+
if encoding is not None:
272+
if isinstance(encoding, unicode):
273+
encoding = encoding.encode('ascii')
274+
cenc = PyBytes_AsString(encoding)
275+
276+
if unicode_errors is not None:
277+
if isinstance(unicode_errors, unicode):
278+
unicode_errors = unicode_errors.encode('ascii')
279+
cerr = PyBytes_AsString(unicode_errors)
280+
281+
init_ctx(&ctx, object_hook, object_pairs_hook, list_hook, use_list, cenc, cerr)
283282
ret = template_execute(&ctx, buf, buf_len, &off, 1)
284283
if ret == 1:
285284
obj = template_data(&ctx)
@@ -361,10 +360,7 @@ cdef class Unpacker(object):
361360
cdef object file_like_read
362361
cdef Py_ssize_t read_size
363362
cdef object object_hook
364-
cdef object _bencoding
365-
cdef object _berrors
366-
cdef char *encoding
367-
cdef char *unicode_errors
363+
cdef object encoding, unicode_errors
368364
cdef size_t max_buffer_size
369365

370366
def __cinit__(self):
@@ -378,6 +374,7 @@ cdef class Unpacker(object):
378374
object object_hook=None, object object_pairs_hook=None, object list_hook=None,
379375
encoding=None, unicode_errors='strict', int max_buffer_size=0,
380376
):
377+
cdef char *cenc=NULL, *cerr=NULL
381378
if use_list is None:
382379
warnings.warn("Set use_list explicitly.", category=DeprecationWarning, stacklevel=1)
383380
use_list = 0
@@ -401,7 +398,20 @@ cdef class Unpacker(object):
401398
self.buf_size = read_size
402399
self.buf_head = 0
403400
self.buf_tail = 0
404-
init_ctx(&self.ctx, object_hook, object_pairs_hook, list_hook, use_list, encoding, unicode_errors)
401+
402+
if encoding is not None:
403+
if isinstance(encoding, unicode):
404+
encoding = encoding.encode('ascii')
405+
self.encoding = encoding
406+
cenc = PyBytes_AsString(encoding)
407+
408+
if unicode_errors is not None:
409+
if isinstance(unicode_errors, unicode):
410+
unicode_errors = unicode_errors.encode('ascii')
411+
self.unicode_errors = unicode_errors
412+
cerr = PyBytes_AsString(unicode_errors)
413+
414+
init_ctx(&self.ctx, object_hook, object_pairs_hook, list_hook, use_list, cenc, cerr)
405415

406416
def feed(self, object next_bytes):
407417
cdef char* buf

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
def cythonize(src):
2020
sys.stderr.write("cythonize: %r\n" % (src,))
21-
cython_compiler.compile([src], emit_linenums=True)
21+
cython_compiler.compile([src])
2222

2323
def ensure_source(src):
2424
pyx = os.path.splitext(src)[0] + '.pyx'

test/test_obj.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def test_decode_pairs_hook():
3434

3535
@raises(ValueError)
3636
def test_only_one_obj_hook():
37-
unpackb(b'', object_hook=lambda x: x, object_pairs_hook=lambda x: x)
37+
unpackb(b'', object_hook=lambda x: x, object_pairs_hook=lambda x: x, use_list=1)
3838

3939
@raises(ValueError)
4040
def test_bad_hook():

test/test_sequnpack.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def test_foobar_skip():
4444
assert 1, "ok"
4545

4646
def test_maxbuffersize():
47-
nose.tools.assert_raises(ValueError, Unpacker, read_size=5, max_buffer_size=3)
47+
nose.tools.assert_raises(ValueError, Unpacker, read_size=5, max_buffer_size=3, use_list=1)
4848
unpacker = Unpacker(read_size=3, max_buffer_size=3, use_list=1)
4949
unpacker.feed(b'fo')
5050
nose.tools.assert_raises(BufferFull, unpacker.feed, b'ob')

0 commit comments

Comments
 (0)