From 3478406537fc36badb2501d2110a2bceb48a45b6 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 11 Dec 2012 02:46:13 +0900 Subject: Fix tests. --- msgpack/_msgpack.pyx | 62 +++++++++++++++++++++++++++++--------------------- setup.py | 2 +- test/test_obj.py | 2 +- test/test_sequnpack.py | 2 +- 4 files changed, 39 insertions(+), 29 deletions(-) diff --git a/msgpack/_msgpack.pyx b/msgpack/_msgpack.pyx index dca6237..5feba11 100644 --- a/msgpack/_msgpack.pyx +++ b/msgpack/_msgpack.pyx @@ -1,21 +1,17 @@ # coding: utf-8 #cython: embedsignature=True -import warnings - from cpython cimport * cdef extern from "Python.h": ctypedef char* const_char_ptr "const char*" ctypedef char* const_void_ptr "const void*" ctypedef struct PyObject cdef int PyObject_AsReadBuffer(object o, const_void_ptr* buff, Py_ssize_t* buf_len) except -1 - char* __FILE__ - int __LINE__ from libc.stdlib cimport * from libc.string cimport * from libc.limits cimport * - +import warnings cdef extern from "pack.h": struct msgpack_packer: @@ -218,7 +214,9 @@ cdef extern from "unpack.h": void template_init(template_context* ctx) object template_data(template_context* ctx) -cdef inline init_ctx(template_context *ctx, object object_hook, object object_pairs_hook, object list_hook, bint use_list, encoding, unicode_errors): +cdef inline init_ctx(template_context *ctx, + object object_hook, object object_pairs_hook, object list_hook, + bint use_list, char* encoding, char* unicode_errors): template_init(ctx) ctx.user.use_list = use_list ctx.user.object_hook = ctx.user.list_hook = NULL @@ -244,20 +242,8 @@ cdef inline init_ctx(template_context *ctx, object object_hook, object object_pa raise TypeError("list_hook must be a callable.") ctx.user.list_hook = list_hook - if encoding is None: - ctx.user.encoding = NULL - ctx.user.unicode_errors = NULL - else: - if isinstance(encoding, unicode): - _bencoding = encoding.encode('ascii') - else: - _bencoding = encoding - ctx.user.encoding = PyBytes_AsString(_bencoding) - if isinstance(unicode_errors, unicode): - _berrors = unicode_errors.encode('ascii') - else: - _berrors = unicode_errors - ctx.user.unicode_errors = PyBytes_AsString(_berrors) + ctx.user.encoding = encoding + ctx.user.unicode_errors = unicode_errors def unpackb(object packed, object object_hook=None, object list_hook=None, use_list=None, encoding=None, unicode_errors="strict", @@ -273,13 +259,26 @@ def unpackb(object packed, object object_hook=None, object list_hook=None, cdef char* buf cdef Py_ssize_t buf_len + cdef char* cenc = NULL + cdef char* cerr = NULL PyObject_AsReadBuffer(packed, &buf, &buf_len) if use_list is None: warnings.warn("Set use_list explicitly.", category=DeprecationWarning, stacklevel=1) use_list = 0 - init_ctx(&ctx, object_hook, object_pairs_hook, list_hook, use_list, encoding, unicode_errors) + + if encoding is not None: + if isinstance(encoding, unicode): + encoding = encoding.encode('ascii') + cenc = PyBytes_AsString(encoding) + + if unicode_errors is not None: + if isinstance(unicode_errors, unicode): + unicode_errors = unicode_errors.encode('ascii') + cerr = PyBytes_AsString(unicode_errors) + + init_ctx(&ctx, object_hook, object_pairs_hook, list_hook, use_list, cenc, cerr) ret = template_execute(&ctx, buf, buf_len, &off, 1) if ret == 1: obj = template_data(&ctx) @@ -361,10 +360,7 @@ cdef class Unpacker(object): cdef object file_like_read cdef Py_ssize_t read_size cdef object object_hook - cdef object _bencoding - cdef object _berrors - cdef char *encoding - cdef char *unicode_errors + cdef object encoding, unicode_errors cdef size_t max_buffer_size def __cinit__(self): @@ -378,6 +374,7 @@ cdef class Unpacker(object): object object_hook=None, object object_pairs_hook=None, object list_hook=None, encoding=None, unicode_errors='strict', int max_buffer_size=0, ): + cdef char *cenc=NULL, *cerr=NULL if use_list is None: warnings.warn("Set use_list explicitly.", category=DeprecationWarning, stacklevel=1) use_list = 0 @@ -401,7 +398,20 @@ cdef class Unpacker(object): self.buf_size = read_size self.buf_head = 0 self.buf_tail = 0 - init_ctx(&self.ctx, object_hook, object_pairs_hook, list_hook, use_list, encoding, unicode_errors) + + if encoding is not None: + if isinstance(encoding, unicode): + encoding = encoding.encode('ascii') + self.encoding = encoding + cenc = PyBytes_AsString(encoding) + + if unicode_errors is not None: + if isinstance(unicode_errors, unicode): + unicode_errors = unicode_errors.encode('ascii') + self.unicode_errors = unicode_errors + cerr = PyBytes_AsString(unicode_errors) + + init_ctx(&self.ctx, object_hook, object_pairs_hook, list_hook, use_list, cenc, cerr) def feed(self, object next_bytes): cdef char* buf diff --git a/setup.py b/setup.py index 9f0ce5d..86b0b34 100644 --- a/setup.py +++ b/setup.py @@ -18,7 +18,7 @@ except ImportError: def cythonize(src): sys.stderr.write("cythonize: %r\n" % (src,)) - cython_compiler.compile([src], emit_linenums=True) + cython_compiler.compile([src]) def ensure_source(src): pyx = os.path.splitext(src)[0] + '.pyx' diff --git a/test/test_obj.py b/test/test_obj.py index 881e627..c38d6dc 100644 --- a/test/test_obj.py +++ b/test/test_obj.py @@ -34,7 +34,7 @@ def test_decode_pairs_hook(): @raises(ValueError) def test_only_one_obj_hook(): - unpackb(b'', object_hook=lambda x: x, object_pairs_hook=lambda x: x) + unpackb(b'', object_hook=lambda x: x, object_pairs_hook=lambda x: x, use_list=1) @raises(ValueError) def test_bad_hook(): diff --git a/test/test_sequnpack.py b/test/test_sequnpack.py index dac36a8..eac0828 100644 --- a/test/test_sequnpack.py +++ b/test/test_sequnpack.py @@ -44,7 +44,7 @@ def test_foobar_skip(): assert 1, "ok" def test_maxbuffersize(): - nose.tools.assert_raises(ValueError, Unpacker, read_size=5, max_buffer_size=3) + nose.tools.assert_raises(ValueError, Unpacker, read_size=5, max_buffer_size=3, use_list=1) unpacker = Unpacker(read_size=3, max_buffer_size=3, use_list=1) unpacker.feed(b'fo') nose.tools.assert_raises(BufferFull, unpacker.feed, b'ob') -- cgit v1.2.1 From 6ae363ea27e409c63e951eb32d7c16df0309bb52 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 11 Dec 2012 02:52:24 +0900 Subject: Update changelog for 0.2.3 --- ChangeLog.rst | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/ChangeLog.rst b/ChangeLog.rst index 6a4d27b..833c97c 100644 --- a/ChangeLog.rst +++ b/ChangeLog.rst @@ -1,12 +1,3 @@ -0.3.0 -===== -:release date: in development - -Changes -------- -* Add ``.skip()`` method to ``Unpacker`` (thanks to jnothman) - - 0.2.3 ======= :release date: in development @@ -14,6 +5,7 @@ Changes Changes ------- * Warn when use_list is not specified. It's default value will be changed in 0.3. +* Add ``.skip()`` method to ``Unpacker`` (thanks to jnothman) Bugs fixed ----------- -- cgit v1.2.1 From 4a20700e2000d4baa952e8db48801672e53bab99 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 11 Dec 2012 02:56:20 +0900 Subject: prepare 0.2.3 --- README.rst | 4 ++-- msgpack/_version.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 1da6ece..5c0829a 100644 --- a/README.rst +++ b/README.rst @@ -3,8 +3,8 @@ MessagePack Python Binding =========================== :author: INADA Naoki -:version: 0.2.0 -:date: 2012-06-27 +:version: 0.2.3 +:date: 2012-12-11 HOW TO USE ----------- diff --git a/msgpack/_version.py b/msgpack/_version.py index 9bffd02..1bbdf2a 100644 --- a/msgpack/_version.py +++ b/msgpack/_version.py @@ -1 +1 @@ -version = (0, 2, 3, 'dev1') +version = (0, 2, 3) -- cgit v1.2.1 From cb7dff331911f836f9bfac2e5f0450760e362e89 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 11 Dec 2012 02:59:45 +0900 Subject: Add .travis.yml --- .travis.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..11835cb --- /dev/null +++ b/.travis.yml @@ -0,0 +1,14 @@ +language: python +python: + - 2.6 + - 2.7 + - 3.2 + +install: + - sudo apt-get update -qq + - sudo apt-get install -q cython + - cython --cplus msgpack/_msgpack.pyx + - pip install six --use-mirrors + - python setup.py install + +script: "nosetests -w test" -- cgit v1.2.1 From e9f9e9e83ff5b69e7aab5384d9cb507363dd88e3 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 11 Dec 2012 09:26:15 +0900 Subject: Don't use c++ on travis. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 11835cb..e8c2cec 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,7 @@ python: install: - sudo apt-get update -qq - sudo apt-get install -q cython - - cython --cplus msgpack/_msgpack.pyx + - cython msgpack/_msgpack.pyx - pip install six --use-mirrors - python setup.py install -- cgit v1.2.1 From 0fa8c102d7f8ed618284e1b039fa867cfd9baa2f Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Sat, 22 Dec 2012 12:12:32 +0900 Subject: Add test reproducing SEGV --- test/test_except.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/test_except.py b/test/test_except.py index ad02cb6..e142dd6 100644 --- a/test/test_except.py +++ b/test/test_except.py @@ -6,9 +6,28 @@ from msgpack import packb, unpackb import datetime +class DummyException(Exception): + pass + + def test_raise_on_find_unsupported_value(): assert_raises(TypeError, packb, datetime.datetime.now()) + +def test_raise_from_object_hook(): + def hook(obj): + raise DummyException + assert_raises(DummyException, unpackb, packb({}), object_hook=hook) + assert_raises(DummyException, unpackb, packb({'fizz': 'buzz'}), + object_hook=hook) + assert_raises(DummyException, unpackb, packb({'fizz': 'buzz'}), + object_pairs_hook=hook) + assert_raises(DummyException, unpackb, packb({'fizz': {'buzz': 'spam'}}), + object_hook=hook) + assert_raises(DummyException, unpackb, packb({'fizz': {'buzz': 'spam'}}), + object_pairs_hook=hook) + + if __name__ == '__main__': from nose import main main() -- cgit v1.2.1 From 79e44f86c9ec554f56b9807f909e21887c36d166 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Sat, 22 Dec 2012 12:14:05 +0900 Subject: Add NULL check. --- msgpack/unpack.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/msgpack/unpack.h b/msgpack/unpack.h index 7064a1b..0851ec2 100644 --- a/msgpack/unpack.h +++ b/msgpack/unpack.h @@ -206,6 +206,9 @@ static inline int template_callback_map_end(unpack_user* u, msgpack_unpack_objec { if (u->object_hook) { PyObject *new_c = PyEval_CallFunction(u->object_hook, "(O)", *c); + if (new_c == NULL) { + return -1; + } Py_DECREF(*c); *c = new_c; } -- cgit v1.2.1 From ce2c5b22efcbbbc5598c7a631b3558d757b8b9dc Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Sat, 22 Dec 2012 12:42:36 +0900 Subject: Check return value of _end functions. --- msgpack/unpack_template.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/msgpack/unpack_template.h b/msgpack/unpack_template.h index 6080a51..9450943 100644 --- a/msgpack/unpack_template.h +++ b/msgpack/unpack_template.h @@ -145,7 +145,7 @@ msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const c if(top >= MSGPACK_EMBED_STACK_SIZE) { goto _failed; } /* FIXME */ \ if(construct_cb(func)(user, count_, &stack[top].obj) < 0) { goto _failed; } \ if((count_) == 0) { obj = stack[top].obj; \ - construct_cb(func##_end)(user, &obj); \ + if (construct_cb(func##_end)(user, &obj) < 0) { goto _failed; } \ goto _push; } \ stack[top].ct = ct_; \ stack[top].size = count_; \ @@ -346,7 +346,7 @@ _push: if(construct_cb(_array_item)(user, c->count, &c->obj, obj) < 0) { goto _failed; } if(++c->count == c->size) { obj = c->obj; - construct_cb(_array_end)(user, &obj); + if (construct_cb(_array_end)(user, &obj) < 0) { goto _failed; } --top; /*printf("stack pop %d\n", top);*/ goto _push; @@ -360,7 +360,7 @@ _push: if(construct_cb(_map_item)(user, c->count, &c->obj, c->map_key, obj) < 0) { goto _failed; } if(++c->count == c->size) { obj = c->obj; - construct_cb(_map_end)(user, &obj); + if (construct_cb(_map_end)(user, &obj) < 0) { goto _failed; } --top; /*printf("stack pop %d\n", top);*/ goto _push; -- cgit v1.2.1