diff options
author | INADA Naoki <inada-n@klab.com> | 2012-09-23 10:04:41 +0900 |
---|---|---|
committer | INADA Naoki <inada-n@klab.com> | 2012-09-23 10:04:41 +0900 |
commit | 9963522d46cefe97594204e1a0229571c7e91b89 (patch) | |
tree | efc2661695f7a17a016cb892d12367e293ce7e57 /msgpack/_msgpack.pyx | |
parent | e8842efdedb1917a28147aa8ad1bf6f7b729a751 (diff) | |
parent | 7d142d2bef0805a528f1cd84e173579209298380 (diff) | |
download | msgpack-python-9963522d46cefe97594204e1a0229571c7e91b89.tar.gz |
Add `.skip()` method to `Unpacker`. (Merge branch 'skip')
Diffstat (limited to 'msgpack/_msgpack.pyx')
-rw-r--r-- | msgpack/_msgpack.pyx | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/msgpack/_msgpack.pyx b/msgpack/_msgpack.pyx index 12ee2ea..c8ee7bb 100644 --- a/msgpack/_msgpack.pyx +++ b/msgpack/_msgpack.pyx @@ -209,7 +209,7 @@ cdef extern from "unpack.h": PyObject* key int template_execute(template_context* ctx, const_char_ptr data, - size_t len, size_t* off) except -1 + size_t len, size_t* off, bint construct) except -1 void template_init(template_context* ctx) object template_data(template_context* ctx) @@ -255,7 +255,7 @@ def unpackb(object packed, object object_hook=None, object list_hook=None, if not PyCallable_Check(list_hook): raise TypeError("list_hook must be a callable.") ctx.user.list_hook = <PyObject*>list_hook - ret = template_execute(&ctx, buf, buf_len, &off) + ret = template_execute(&ctx, buf, buf_len, &off, 1) if ret == 1: obj = template_data(&ctx) if off < buf_len: @@ -451,15 +451,18 @@ cdef class Unpacker(object): else: self.file_like = None - cpdef unpack(self): - """unpack one object""" + cdef _unpack(self, bint construct): cdef int ret + cdef object obj while 1: - ret = template_execute(&self.ctx, self.buf, self.buf_tail, &self.buf_head) + ret = template_execute(&self.ctx, self.buf, self.buf_tail, &self.buf_head, construct) if ret == 1: - o = template_data(&self.ctx) + if construct: + obj = template_data(&self.ctx) + else: + obj = None template_init(&self.ctx) - return o + return obj elif ret == 0: if self.file_like is not None: self.read_from_file() @@ -468,11 +471,19 @@ cdef class Unpacker(object): else: raise ValueError("Unpack failed: error = %d" % (ret,)) + def unpack(self): + """unpack one object""" + return self._unpack(1) + + def skip(self): + """read and ignore one object, returning None""" + return self._unpack(0) + def __iter__(self): return self def __next__(self): - return self.unpack() + return self._unpack(1) # for debug. #def _buf(self): |