8000 merge next_marker patch from @mitsuhiko · loude/msgpack-python@62d209c · GitHub
[go: up one dir, main page]

Skip to content 10000

Commit 62d209c

Browse files
committed
merge next_marker patch from @mitsuhiko
1 parent 626ae51 commit 62d209c

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

msgpack/fallback.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Fallback pure Python implementation of msgpack"""
1+
"""Pure Python implementation of msgpack"""
22

33
import sys
44
import array
@@ -46,10 +46,11 @@ def getvalue(self):
4646
PackValueError,
4747
ExtraData)
4848

49-
EX_SKIP = 0
49+
EX_SKIP = 0
5050
EX_CONSTRUCT = 1
5151
EX_READ_ARRAY_HEADER = 2
5252
EX_READ_MAP_HEADER = 3
53+
EX_CONSTRUCT_SIMPLE = 4
5354

5455
TYPE_IMMEDIATE = 0
5556
TYPE_ARRAY = 1
@@ -275,7 +276,10 @@ def _fb_unpack(self, execute=EX_CONSTRUCT, write_bytes=None):
275276
obj = struct.unpack("b", c)[0]
276277
elif b & 0b11100000 == 0b10100000:
277278
n = b & 0b00011111
278-
obj = self._fb_read(n, write_bytes)
279+
if execute == EX_CONSTRUCT_SIMPLE:
280+
obj = n
281+
else:
282+
obj = self._fb_read(n, write_bytes)
279283
typ = TYPE_RAW
280284
elif b & 0b11110000 == 0b10010000:
281285
n = b & 0b00001111
@@ -311,11 +315,17 @@ def _fb_unpack(self, execute=EX_CONSTRUCT, write_bytes=None):
311315
obj = struct.unpack(">q", self._fb_read(8, write_bytes))[0]
312316
elif b == 0xda:
313317
n = struct.unpack(">H", self._fb_read(2, write_bytes))[0]
314-
obj = self._fb_read(n, write_bytes)
318+
if execute == EX_CONSTRUCT_SIMPLE:
319+
obj = n
320+
else:
321+
obj = self._fb_read(n, write_bytes)
315322
typ = TYPE_RAW
316323
elif b == 0xdb:
317324
n = struct.unpack(">I", self._fb_read(4, write_bytes))[0]
318-
obj = self._fb_read(n, write_bytes)
325+
if execute == EX_CONSTRUCT_SIMPLE:
326+
obj = n
327+
else:
328+
obj = self._fb_read(n, write_bytes)
319329
typ = TYPE_RAW
320330
elif b == 0xdc:
321331
n = struct.unpack(">H", self._fb_read(2, write_bytes))[0]
@@ -331,6 +341,9 @@ def _fb_unpack(self, execute=EX_CONSTRUCT, write_bytes=None):
331341
typ = TYPE_MAP
332342
else:
333343
raise UnpackValueError("Unknown header: 0x%x" % b)
344+
if execute == EX_CONSTRUCT_SIMPLE:
345+
if typ in (TYPE_ARRAY, TYPE_MAP):
346+
return (typ, n)
334347
if execute == EX_READ_ARRAY_HEADER:
335348
if typ != TYPE_ARRAY:
336349
raise UnpackValueError("Expected array")
@@ -377,10 +390,14 @@ def _fb_unpack(self, execute=EX_CONSTRUCT, write_bytes=None):
377390
if execute == EX_SKIP:
378391
return
379392
if typ == TYPE_RAW:
393+
if execute == EX_CONSTRUCT_SIMPLE:
394+
return (TYPE_RAW, obj)
380395
if self._encoding is not None:
381396
obj = obj.decode(self._encoding, self._unicode_errors)
382397
return obj
383398
assert typ == TYPE_IMMEDIATE
399+
if execute == EX_CONSTRUCT_SIMPLE:
400+
return (TYPE_IMMEDIATE, obj)
384401
return obj
385402

386403
def next(self):
@@ -392,6 +409,11 @@ def next(self):
392409
raise StopIteration
393410
__next__ = next
394411

412+
def next_marker(self, write_bytes=None):
413+
ret = self._fb_unpack(EX_CONSTRUCT_SIMPLE, write_bytes)
414+
self._fb_consume()
415+
return ret
416+
395417
def skip(self, write_bytes=None):
396418
self._fb_unpack(EX_SKIP, write_bytes)
397419
self._fb_consume()

0 commit comments

Comments
 (0)
0