Skip to content

Commit 28058fb

Browse files
committed
A first implementation of Unpacker.skip()
1 parent f14d926 commit 28058fb

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

msgpack/_msgpack.pyx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ cdef extern from "unpack.h":
200200
PyObject* key
201201

202202
int template_execute(template_context* ctx, const_char_ptr data,
203-
size_t len, size_t* off) except -1
203+
size_t len, size_t* off, bool construct) except -1
204204
void template_init(template_context* ctx)
205205
object template_data(template_context* ctx)
206206

@@ -246,7 +246,7 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
246246
if not PyCallable_Check(list_hook):
247247
raise TypeError("list_hook must be a callable.")
248248
ctx.user.list_hook = <PyObject*>list_hook
249-
ret = template_execute(&ctx, buf, buf_len, &off)
249+
ret = template_execute(&ctx, buf, buf_len, &off, True)
250250
if ret == 1:
251251
return template_data(&ctx)
252252
else:
@@ -440,15 +440,12 @@ cdef class Unpacker(object):
440440
else:
441441
self.file_like = None
442442

443-
cpdef unpack(self):
444-
"""unpack one object"""
443+
cpdef _unpack(self, bool construct):
445444
cdef int ret
446445
while 1:
447-
ret = template_execute(&self.ctx, self.buf, self.buf_tail, &self.buf_head)
446+
ret = template_execute(&self.ctx, self.buf, self.buf_tail, &self.buf_head, construct)
448447
if ret == 1:
449-
o = template_data(&self.ctx)
450-
template_init(&self.ctx)
451-
return o
448+
return
452449
elif ret == 0:
453450
if self.file_like is not None:
454451
self.fill_buffer()
@@ -457,6 +454,18 @@ cdef class Unpacker(object):
457454
else:
458455
raise ValueError("Unpack failed: error = %d" % (ret,))
459456

457+
cpdef unpack(self):
458+
"""unpack one object"""
459+
self._unpack(True)
460+
o = template_data(&self.ctx)
461+
template_init(&self.ctx)
462+
463+
464+
cpdef skip(self):
465+
"""read and ignore one object, returning None"""
466+
self._unpack(False)
467+
template_init(&self.ctx)
468+
460469
def __iter__(self):
461470
return self
462471

msgpack/unpack_template.h

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ msgpack_unpack_func(msgpack_unpack_object, _data)(msgpack_unpack_struct(_context
9595
}
9696

9797

98-
msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const char* data, size_t len, size_t* off)
98+
msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const char* data, size_t len, size_t* off, bool construct)
9999
{
100100
assert(len >= *off);
101101

@@ -117,14 +117,17 @@ msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const c
117117

118118
int ret;
119119

120+
#define construct_cb(name) \
121+
construct && msgpack_unpack_callback(name)
122+
120123
#define push_simple_value(func) \
121-
if(msgpack_unpack_callback(func)(user, &obj) < 0) { goto _failed; } \
124+
if(construct_cb(func)(user, &obj) < 0) { goto _failed; } \
122125
goto _push
123126
#define push_fixed_value(func, arg) \
124-
if(msgpack_unpack_callback(func)(user, arg, &obj) < 0) { goto _failed; } \
127+
if(construct_cb(func)(user, arg, &obj) < 0) { goto _failed; } \
125128
goto _push
126129
#define push_variable_value(func, base, pos, len) \
127-
if(msgpack_unpack_callback(func)(user, \
130+
if(construct_cb(func)(user, \
128131
(const char*)base, (const char*)pos, len, &obj) < 0) { goto _failed; } \
129132
goto _push
130133

@@ -140,9 +143,9 @@ msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const c
140143

141144
#define start_container(func, count_, ct_) \
142145
if(top >= MSGPACK_EMBED_STACK_SIZE) { goto _failed; } /* FIXME */ \
143-
if(msgpack_unpack_callback(func)(user, count_, &stack[top].obj) < 0) { goto _failed; } \
146+
if(construct_cb(func)(user, count_, &stack[top].obj) < 0) { goto _failed; } \
144147
if((count_) == 0) { obj = stack[top].obj; \
145-
msgpack_unpack_callback(func##_end)(user, &obj); \
148+
construct_cb(func##_end)(user, &obj); \
146149
goto _push; } \
147150
stack[top].ct = ct_; \
148151
stack[top].size = count_; \
@@ -340,10 +343,10 @@ msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const c
340343
c = &stack[top-1];
341344
switch(c->ct) {
342345
case CT_ARRAY_ITEM:
343-
if(msgpack_unpack_callback(_array_item)(user, c->count, &c->obj, obj) < 0) { goto _failed; }
346+
if(construct_cb(_array_item)(user, c->count, &c->obj, obj) < 0) { goto _failed; }
344347
if(++c->count == c->size) {
345348
obj = c->obj;
346-
msgpack_unpack_callback(_array_end)(user, &obj);
349+
construct_cb(_array_end)(user, &obj);
347350
--top;
348351
/*printf("stack pop %d\n", top);*/
349352
goto _push;
@@ -354,10 +357,10 @@ msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const c
354357
c->ct = CT_MAP_VALUE;
355358
goto _header_again;
356359
case CT_MAP_VALUE:
357-
if(msgpack_unpack_callback(_map_item)(user, &c->obj, c->map_key, obj) < 0) { goto _failed; }
360+
if(construct_cb(_map_item)(user, &c->obj, c->map_key, obj) < 0) { goto _failed; }
358361
if(++c->count == c->size) {
359362
obj = c->obj;
360-
msgpack_unpack_callback(_map_end)(user, &obj);
363+
construct_cb(_map_end)(user, &obj);
361364
--top;
362365
/*printf("stack pop %d\n", top);*/
363366
goto _push;
@@ -399,6 +402,7 @@ msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const c
399402
*off = p - (const unsigned char*)data;
400403

401404
return ret;
405+
#undef construct_cb
402406
}
403407

404408

0 commit comments

Comments
 (0)