8000 Merge branch 'skip' of git://github.com/jnothman/msgpack-python into … · twigtek/msgpack-python@65f5823 · GitHub
[go: up one dir, main page]

Skip to content

Commit 65f5823

Browse files
committed
Merge branch 'skip' of git://github.com/jnothman/msgpack-python into skip
2 parents e8842ef + 032df6f commit 65f5823

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
@@ -209,7 +209,7 @@ cdef extern from "unpack.h":
209209
PyObject* key
210210

211211
int template_execute(template_context* ctx, const_char_ptr data,
212-
size_t len, size_t* off) except -1
212+
size_t len, size_t* off, bool construct) except -1
213213
void template_init(template_context* ctx)
214214
object template_data(template_context* ctx)
215215

@@ -255,7 +255,7 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
255255
if not PyCallable_Check(list_hook):
256256
raise TypeError("list_hook must be a callable.")
257257
ctx.user.list_hook = <PyObject*>list_hook
258-
ret = template_execute(&ctx, buf, buf_len, &off)
258+
ret = template_execute(&ctx, buf, buf_len, &off, True)
259259
if ret == 1:
260260
obj = template_data(&ctx)
261261
if off < buf_len:
@@ -451,15 +451,12 @@ cdef class Unpacker(object):
451451
else:
452452
self.file_like = None
453453

454-
cpdef unpack(self):
455-
"""unpack one object"""
454+
cpdef _unpack(self, bool construct):
456455
cdef int ret
457456
while 1:
458-
ret = template_execute(&self.ctx, self.buf, self.buf_tail, &self.buf_head)
457+
ret = template_execute(&self.ctx, self.buf, self.buf_tail, &self.buf_head, construct)
459458
if ret == 1:
460-
o = template_data(&self.ctx)
461-
template_init(&self.ctx)
462-
return o
459+
return
463460
elif ret == 0:
464461
if self.file_like is not None:
465462
self.read_from_file()
@@ -468,6 +465,18 @@ cdef class Unpacker(object):
468465
else:
469466
raise ValueError("Unpack failed: error = %d" % (ret,))
470467

468+
cpdef unpack(self):
469+
"""unpack one object"""
470+
self._unpack(True)
471+
o = template_data(&self.ctx)
472+
template_init(&self.ctx)
473+
474+
475+
cpdef skip(self):
476+
"""read and ignore one object, returning None"""
477+
self._unpack(False)
478+
template_init(&self.ctx)
479+
471480
def __iter__(self):
472481
return self
473482

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, a 8000 rg) \
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)
0