Skip to content

Commit 0431a76

Browse files
committed
read_array/map_header functionality
1 parent d56e2b2 commit 0431a76

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed

msgpack/_msgpack.pyx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ cdef extern from "unpack.h":
212212
size_t len, size_t* off) except -1
213213
execute_fn template_construct
214214
execute_fn template_skip
215+
execute_fn read_array_header
216+
execute_fn read_map_header
215217
void template_init(template_context* ctx)
216218
object template_data(template_context* ctx)
217219

@@ -482,6 +484,14 @@ cdef class Unpacker(object):
482484
"""read and ignore one object, returning None"""
483485
return self._unpack(template_skip)
484486

487+
def read_array_header(self):
488+
"""assuming the next object is an array, return its size n, such that the next n unpack() calls will iterate over its contents."""
489+
return self._unpack(read_array_header)
490+
491+
def read_map_header(self):
492+
"""assuming the next object is a map, return its size n, such that the next n * 2 unpack() calls will iterate over its key-value pairs."""
493+
return self._unpack(read_map_header)
494+
485495
def __iter__(self):
486496
return self
487497

msgpack/unpack_template.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,15 +408,78 @@ msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const c
408408
#undef construct_cb
409409
}
410410

411+
#undef SWITCH_RANGE_BEGIN
412+
#undef SWITCH_RANGE
413+
#undef SWITCH_RANGE_DEFAULT
414+
#undef SWITCH_RANGE_END
411415
#undef push_simple_value
412416
#undef push_fixed_value
413417
#undef push_variable_value
414418
#undef again_fixed_trail
415419
#undef again_fixed_trail_if_zero
416420
#undef start_container
417421

422+
template <unsigned int fixed_offset, unsigned int var_offset>
423+
msgpack_unpack_func(int, _container_header)(msgpack_unpack_struct(_context)* ctx, const char* data, size_t len, size_t* off)
424+
{
425+
assert(len >= *off);
426+
uint32_t size;
427+
const unsigned char *const p = (unsigned char*)data + *off;
428+
429+
#define inc_offset(inc) \
430+
if (len - *off < inc) \
431+
return 0; \
432+
*off += inc;
433+
434+
switch (*p) {
435+
case var_offset:
436+
inc_offset(3);
437+
size = _msgpack_load16(uint16_t, p + 1);
438+
break;
439+
case var_offset + 1:
440+
inc_offset(5);
441+
size = _msgpack_load32(uint32_t, p + 1);
442+
break;
443+
#ifdef USE_CASE_RANGE
444+
case fixed_offset + 0x0 ... fixed_offset + 0xf:
445+
#else
446+
case fixed_offset + 0x0:
447+
case fixed_offset + 0x1:
448+
case fixed_offset + 0x2:
449+
case fixed_offset + 0x3:
450+
case fixed_offset + 0x4:
451+
case fixed_offset + 0x5:
452+
case fixed_offset + 0x6:
453+
case fixed_offset + 0x7:
454+
case fixed_offset + 0x8:
455+
case fixed_offset + 0x9:
456+
case fixed_offset + 0xa:
457+
case fixed_offset + 0xb:
458+
case fixed_offset + 0xc:
459+
case fixed_offset + 0xd:
460+
case fixed_offset + 0xe:
461+
case fixed_offset + 0xf:
462+
#endif
463+
++*off;
464+
size = ((unsigned int)*p) & 0x0f;
465+
break;
466+
default:
467+
PyErr_SetString(PyExc_ValueError, "Unexpected type header on stream");
468+
return -1;
469+
}
470+
msgpack_unpack_callback(_uint32)(&ctx->user, size, &ctx->stack[0].obj);
471+
return 1;
472+
}
473+
474+
#undef SWITCH_RANGE_BEGIN
475+
#undef SWITCH_RANGE
476+
#undef SWITCH_RANGE_DEFAULT
477+
#undef SWITCH_RANGE_END
478+
418479
static const execute_fn template_construct = &template_execute<true>;
419480
static const execute_fn template_skip = &template_execute<false>;
481+
static const execute_fn read_array_header = &template_container_header<0x90, 0xdc>;
482+
static const execute_fn read_map_header = &template_container_header<0x80, 0xde>;
420483

421484
#undef msgpack_unpack_func
422485
#undef msgpack_unpack_callback

test/test_read_size.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""Test Unpacker's read_array_header and read_map_header methods"""
2+
from msgpack import packb, Unpacker
3+
UnexpectedTypeException = ValueError
4+
5+
def test_read_array_header():
6+
unpacker = Unpacker()
7+
unpacker.feed(packb(['a', 'b', 'c']))
8+
assert unpacker.read_array_header() == 3
9+
assert unpacker.unpack() == 'a'
10+
assert unpacker.unpack() == 'b'
11+
assert unpacker.unpack() == 'c'
12+
try:
13+
unpacker.unpack()
14+
assert 0, 'should raise exception'
15+
except StopIteration:
16+
assert 1, 'okay'
17+
18+
19+
def test_read_map_header():
20+
unpacker = Unpacker()
21+
unpacker.feed(packb({'a': 'A'}))
22+
assert unpacker.read_map_header() == 1
23+
assert unpacker.unpack() == 'a'
24+
assert unpacker.unpack() == 'A'
25+
try:
26+
unpacker.unpack()
27+
assert 0, 'should raise exception'
28+
except StopIteration:
29+
assert 1, 'okay'
30+
31+
def test_incorrect_type_array():
32+
unpacker = Unpacker()
33+
unpacker.feed(packb(1))
34+
try:
35+
unpacker.read_array_header()
36+
assert 0, 'should raise exception'
37+
except UnexpectedTypeException:
38+
assert 1, 'okay'
39+
40+
def test_incorrect_type_map():
41+
unpacker = Unpacker()
42+
unpacker.feed(packb(1))
43+
try:
44+
unpacker.read_map_header()
45+
assert 0, 'should raise exception'
46+
except UnexpectedTypeException:
47+
assert 1, 'okay'
48+
49+
def test_correct_type_nested_array():
50+
unpacker = Unpacker()
51+
unpacker.feed(packb({'a': ['b', 'c', 'd']}))
52+
try:
53+
unpacker.read_array_header()
54+
assert 0, 'should raise exception'
55+
except UnexpectedTypeException:
56+
assert 1, 'okay'
57+
58+
def test_incorrect_type_nested_map():
59+
unpacker = Unpacker()
60+
unpacker.feed(packb([{'a': 'b'}]))
61+
try:
62+
unpacker.read_map_header()
63+
assert 0, 'should raise exception'
64+
except UnexpectedTypeException:
65+
assert 1, 'okay'
66+

0 commit comments

Comments
 (0)