1
- """Fallback pure Python implementation of msgpack"""
1
+ """Pure Python implementation of msgpack"""
2
2
3
3
import sys
4
4
import array
@@ -46,10 +46,11 @@ def getvalue(self):
46
46
PackValueError ,
47
47
ExtraData )
48
48
49
- EX_SKIP = 0
49
+ EX_SKIP = 0
50
50
EX_CONSTRUCT = 1
51
51
EX_READ_ARRAY_HEADER = 2
52
52
EX_READ_MAP_HEADER = 3
53
+ EX_CONSTRUCT_SIMPLE = 4
53
54
54
55
TYPE_IMMEDIATE = 0
55
56
TYPE_ARRAY = 1
@@ -275,7 +276,10 @@ def _fb_unpack(self, execute=EX_CONSTRUCT, write_bytes=None):
275
276
obj = struct .unpack ("b" , c )[0 ]
276
277
elif b & 0b11100000 == 0b10100000 :
277
278
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 )
279
283
typ = TYPE_RAW
280
284
elif b & 0b11110000 == 0b10010000 :
281
285
n = b & 0b00001111
@@ -311,11 +315,17 @@ def _fb_unpack(self, execute=EX_CONSTRUCT, write_bytes=None):
311
315
obj = struct .unpack (">q" , self ._fb_read (8 , write_bytes ))[0 ]
312
316
elif b == 0xda :
313
317
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 )
315
322
typ = TYPE_RAW
316
323
elif b == 0xdb :
317
324
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 )
319
329
typ = TYPE_RAW
320
330
elif b == 0xdc :
321
331
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):
331
341
typ = TYPE_MAP
332
342
else :
333
343
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 )
334
347
if execute == EX_READ_ARRAY_HEADER :
335
348
if typ != TYPE_ARRAY :
336
349
raise UnpackValueError ("Expected array" )
@@ -377,10 +390,14 @@ def _fb_unpack(self, execute=EX_CONSTRUCT, write_bytes=None):
377
390
if execute == EX_SKIP :
378
391
return
379
392
if typ == TYPE_RAW :
393
+ if execute == EX_CONSTRUCT_SIMPLE :
394
+ return (TYPE_RAW , obj )
380
395
if self ._encoding is not None :
381
396
obj = obj .decode (self ._encoding , self ._unicode_errors )
382
397
return obj
383
398
assert typ == TYPE_IMMEDIATE
399
+ if execute == EX_CONSTRUCT_SIMPLE :
400
+ return (TYPE_IMMEDIATE , obj )
384
401
return obj
385
402
386
403
def next (self ):
@@ -392,6 +409,11 @@ def next(self):
392
409
raise StopIteration
393
410
__next__ = next
394
411
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
+
395
417
def skip (self , write_bytes = None ):
396
418
self ._fb_unpack (EX_SKIP , write_bytes )
397
419
self ._fb_consume ()
0 commit comments