@@ -195,6 +195,21 @@ def __init__(self, fp, magic_int, bytes_for_s, code_objects={}) -> None:
195195
196196 self .UNMARSHAL_DISPATCH_TABLE = UNMARSHAL_DISPATCH_TABLE
197197
198+ def read_int16 (self ) -> int :
199+ return int (unpack ("<h" , self .fp .read (2 ))[0 ])
200+
201+ def read_int32 (self ) -> int :
202+ return int (unpack ("<i" , self .fp .read (4 ))[0 ])
203+
204+ def read_int64 (self ) -> int :
205+ return int (unpack ("<q" , self .fp .read (8 ))[0 ])
206+
207+ def read_slice (self , n : int ) -> bytes :
208+ return self .fp .read (n )
209+
210+ def read_uint32 (self ) -> int :
211+ return int (unpack ("<I" , self .fp .read (4 ))[0 ])
212+
198213 def load (self ):
199214 """
200215 ``marshal.load()`` written in Python. When the Python bytecode magic loaded is the
@@ -302,16 +317,16 @@ def t_True(self, save_ref, bytes_for_s: bool = False) -> bool:
302317 return True
303318
304319 def t_int32 (self , save_ref , bytes_for_s : bool = False ):
305- return self .r_ref (int ( unpack ( "<i" , self .fp . read ( 4 ))[ 0 ] ), save_ref )
320+ return self .r_ref (self .read_int32 ( ), save_ref )
306321
307322 def t_long (self , save_ref , bytes_for_s : bool = False ):
308- n = unpack ( "<i" , self .fp . read ( 4 ))[ 0 ]
323+ n = self .read_uint32 ()
309324 if n == 0 :
310325 return long (0 )
311326 size = abs (n )
312327 d = long (0 )
313328 for j in range (0 , size ):
314- md = int ( unpack ( "<h" , self .fp . read ( 2 ))[ 0 ] )
329+ md = self .read_int16 ( )
315330 # This operation and turn "d" from a long back
316331 # into an int.
317332 d += md << j * 15
@@ -323,7 +338,7 @@ def t_long(self, save_ref, bytes_for_s: bool = False):
323338
324339 # Python 3.4 removed this.
325340 def t_int64 (self , save_ref , bytes_for_s : bool = False ):
326- obj = unpack ( "<q" , self .fp . read ( 8 ))[ 0 ]
341+ obj = self .read_int64 ()
327342 if save_ref :
328343 self .intern_objects .append (obj )
329344 return obj
@@ -342,7 +357,7 @@ def unpack_pre_24() -> float:
342357 return float (self .fp .read (unpack ("B" , self .fp .read (1 ))[0 ]))
343358
344359 def unpack_newer () -> float :
345- return float (self .fp .read (unpack ( "<i" , self .fp . read ( 4 ))[ 0 ] ))
360+ return float (self .fp .read (self .read_int32 () ))
346361
347362 get_float = unpack_pre_24 if self .magic_int <= 62061 else unpack_newer
348363
@@ -363,7 +378,7 @@ def t_string(self, save_ref, bytes_for_s: bool):
363378 In Python3, this is a ``bytes`` type. In Python2, it is a string type;
364379 ``bytes_for_s`` is True when a Python 3 interpreter is reading Python 2 bytecode.
365380 """
366- strsize = unpack ( "<i" , self .fp . read ( 4 ))[ 0 ]
381+ strsize = self .read_uint32 ()
367382 s = self .fp .read (strsize )
368383 if not bytes_for_s :
369384 s = compat_str (s )
@@ -377,7 +392,7 @@ def t_ASCII_interned(self, save_ref, bytes_for_s: bool = False):
377392 the string.
378393 """
379394 # FIXME: check
380- strsize = unpack ( "<i" , self .fp . read ( 4 ))[ 0 ]
395+ strsize = self .read_uint32 ()
381396 interned = compat_str (self .fp .read (strsize ))
382397 self .intern_strings .append (interned )
383398 return self .r_ref (interned , save_ref )
@@ -388,7 +403,7 @@ def t_ASCII(self, save_ref, bytes_for_s: bool = False):
388403 There are true strings in Python3 as opposed to
389404 bytes.
390405 """
391- strsize = unpack ( "<i" , self .fp . read ( 4 ))[ 0 ]
406+ strsize = self .read_uint32 ()
392407 s = self .fp .read (strsize )
393408 s = compat_str (s )
394409 return self .r_ref (s , save_ref )
@@ -407,13 +422,13 @@ def t_short_ASCII_interned(self, save_ref, bytes_for_s: bool = False):
407422 return self .r_ref (interned , save_ref )
408423
409424 def t_interned (self , save_ref , bytes_for_s : bool = False ):
410- strsize = unpack ( "<i" , self .fp . read ( 4 ))[ 0 ]
425+ strsize = self .read_uint32 ()
411426 interned = compat_str (self .fp .read (strsize ))
412427 self .intern_strings .append (interned )
413428 return self .r_ref (interned , save_ref )
414429
415430 def t_unicode (self , save_ref , bytes_for_s : bool = False ):
416- strsize = unpack ( "<i" , self .fp . read ( 4 ))[ 0 ]
431+ strsize = self .read_uint32 ()
417432 unicodestring = self .fp .read (strsize )
418433 if self .version_triple < (3 , 0 ):
419434 string = UnicodeForPython3 (unicodestring )
@@ -434,7 +449,7 @@ def t_small_tuple(self, save_ref, bytes_for_s: bool = False):
434449 return self .r_ref_insert (ret , i )
435450
436451 def t_tuple (self , save_ref , bytes_for_s : bool = False ):
437- tuplesize = unpack ( "<i" , self .fp . read ( 4 ))[ 0 ]
452+ tuplesize = self .read_uint32 ()
438453 ret = self .r_ref (tuple (), save_ref )
439454 while tuplesize > 0 :
440455 ret += (self .r_object (bytes_for_s = bytes_for_s ),)
@@ -443,15 +458,15 @@ def t_tuple(self, save_ref, bytes_for_s: bool = False):
443458
444459 def t_list (self , save_ref , bytes_for_s : bool = False ):
445460 # FIXME: check me
446- n = unpack ( "<i" , self .fp . read ( 4 ))[ 0 ]
461+ n = self .read_uint32 ()
447462 ret = self .r_ref (list (), save_ref )
448463 while n > 0 :
449464 ret += (self .r_object (bytes_for_s = bytes_for_s ),)
450465 n -= 1
451466 return ret
452467
453468 def t_frozenset (self , save_ref , bytes_for_s : bool = False ):
454- setsize = unpack ( "<i" , self .fp . read ( 4 ))[ 0 ]
469+ setsize = self .read_uint32 ()
455470 collection , i = self .r_ref_reserve ([], save_ref )
456471 while setsize > 0 :
457472 collection .append (self .r_object (bytes_for_s = bytes_for_s ))
@@ -462,7 +477,7 @@ def t_frozenset(self, save_ref, bytes_for_s: bool = False):
462477 return self .r_ref_insert (final_frozenset , i )
463478
464479 def t_set (self , save_ref , bytes_for_s : bool = False ):
465- setsize = unpack ( "<i" , self .fp . read ( 4 ))[ 0 ]
480+ setsize = self .read_uint32 ()
466481 ret , i = self .r_ref_reserve (tuple (), save_ref )
467482 while setsize > 0 :
468483 ret += (self .r_object (bytes_for_s = bytes_for_s ),)
@@ -484,7 +499,7 @@ def t_dict(self, save_ref, bytes_for_s: bool = False):
484499 return ret
485500
486501 def t_python2_string_reference (self , save_ref , bytes_for_s : bool = False ):
487- refnum = unpack ( "<i" , self .fp . read ( 4 ))[ 0 ]
502+ refnum = self .read_uint32 ()
488503 return self .intern_strings [refnum ]
489504
490505 def t_slice (self , save_ref , bytes_for_s : bool = False ):
@@ -522,9 +537,9 @@ def t_code(self, save_ref, bytes_for_s: bool = False):
522537 self .version_triple = magic_int2tuple (self .magic_int )
523538
524539 if self .version_triple >= (2 , 3 ):
525- co_argcount = unpack ( "<i" , self .fp . read ( 4 ))[ 0 ]
540+ co_argcount = self .read_uint32 ()
526541 elif self .version_triple >= (1 , 3 ):
527- co_argcount = unpack ( "<h" , self .fp . read ( 2 ))[ 0 ]
542+ co_argcount = self .read_int16 ()
528543 else :
529544 co_argcount = 0
530545
@@ -538,7 +553,7 @@ def t_code(self, save_ref, bytes_for_s: bool = False):
538553 co_posonlyargcount = None
539554
540555 if self .version_triple >= (3 , 0 ):
541- kwonlyargcount = unpack ( "<i" , self .fp . read ( 4 ))[ 0 ]
556+ kwonlyargcount = self .read_uint32 ()
542557 else :
543558 kwonlyargcount = 0
544559
@@ -547,21 +562,21 @@ def t_code(self, save_ref, bytes_for_s: bool = False):
547562 self .version_triple [:2 ] == (3 , 11 ) and self .is_pypy
548563 ):
549564 if self .version_triple >= (2 , 3 ):
550- co_nlocals = unpack ( "<i" , self .fp . read ( 4 ))[ 0 ]
565+ co_nlocals = self .read_uint32 ()
551566 elif self .version_triple >= (1 , 3 ):
552- co_nlocals = unpack ( "<h" , self .fp . read ( 2 ))[ 0 ]
567+ co_nlocals = self .read_int16 ()
553568
554569 if self .version_triple >= (2 , 3 ):
555- co_stacksize = unpack ( "<i" , self .fp . read ( 4 ))[ 0 ]
570+ co_stacksize = self .read_uint32 ()
556571 elif self .version_triple >= (1 , 5 ):
557- co_stacksize = unpack ( "<h" , self .fp . read ( 2 ))[ 0 ]
572+ co_stacksize = self .read_int16 ()
558573 else :
559574 co_stacksize = 0
560575
561576 if self .version_triple >= (2 , 3 ):
562- co_flags = unpack ( "<i" , self .fp . read ( 4 ))[ 0 ]
577+ co_flags = self .read_uint32 ()
563578 elif self .version_triple >= (1 , 3 ):
564- co_flags = unpack ( "<h" , self .fp . read ( 2 ))[ 0 ]
579+ co_flags = self .read_int16 ()
565580 else :
566581 co_flags = 0
567582
@@ -628,9 +643,9 @@ def t_code(self, save_ref, bytes_for_s: bool = False):
628643
629644 if self .version_triple >= (1 , 5 ):
630645 if self .version_triple >= (2 , 3 ):
631- co_firstlineno = unpack ( "<i" , self .fp . read ( 4 ))[ 0 ]
646+ co_firstlineno = self .read_int32 ()
632647 else :
633- co_firstlineno = unpack ( "<h" , self .fp . read ( 2 ))[ 0 ]
648+ co_firstlineno = self .read_int16 ()
634649
635650 if self .version_triple >= (3 , 11 ) and not self .is_pypy :
636651 co_linetable = self .r_object (bytes_for_s = bytes_for_s )
@@ -775,7 +790,7 @@ def t_code_old(self, _, bytes_for_s: bool = False):
775790
776791 # Since Python 3.4
777792 def t_object_reference (self , save_ref = None , bytes_for_s : bool = False ):
778- refnum = unpack ( "<i" , self .fp . read ( 4 ))[ 0 ]
793+ refnum = self .read_uint32 ()
779794 return self .intern_objects [refnum ]
780795
781796 def t_unknown (self , save_ref = None , bytes_for_s : bool = False ):
0 commit comments