From e1b590e1d4fe3f8d31089a103b03efcd25ffc1e4 Mon Sep 17 00:00:00 2001 From: rocky Date: Wed, 24 Dec 2025 21:29:38 -0500 Subject: [PATCH] DRY unmarshal integer reading routines --- xdis/unmarsh_graal.py | 43 +++++++++++---------------- xdis/unmarsh_rust.py | 13 -------- xdis/unmarshal.py | 69 ++++++++++++++++++++++++++----------------- 3 files changed, 60 insertions(+), 65 deletions(-) diff --git a/xdis/unmarsh_graal.py b/xdis/unmarsh_graal.py index 2f860969..54eb1a34 100644 --- a/xdis/unmarsh_graal.py +++ b/xdis/unmarsh_graal.py @@ -235,7 +235,7 @@ def graal_readBigInteger(self): Reads a marshaled big integer from the input stream. """ negative = False - sz = self.graal_readInt() # Get the size in shorts + sz = self.read_uint32() # Get the size in shorts if sz < 0: negative = True sz = -sz @@ -277,7 +277,7 @@ def graal_readBooleanArray(self) -> tuple[bool, ...]: Python equivalent of Python Graal's readBooleanArray() from MarshalModuleBuiltins.java """ - length: int = int(unpack(" int: @@ -292,7 +292,7 @@ def graal_readBytes(self) -> bytes: Python equivalent of Python Graal's readBytes() from MarshalModuleBuiltins.java """ - length: int = unpack(" float: @@ -307,23 +307,16 @@ def graal_readDoubleArray(self) -> tuple[float, ...]: Python equivalent of Python Graal's readDoubleArray() from MarshalModuleBuiltins.java """ - length: int = int(unpack(" int: - """ - Python equivalent of Python Graal's readInt() from - MarshalModuleBuiltins.java - """ - return int(unpack(" tuple[int, ...]: """ Python equivalent of Python Graal's readIntArray() from MarshalModuleBuiltins.java """ length: int = int(unpack(" int: """ @@ -370,7 +363,7 @@ def graal_readStringArray(self) -> tuple[str, ...]: Python equvalent of Python Graal's readObjectArray() from MarshalModuleBuiltins.java """ - length: int = self.graal_readInt() + length: int = self.read_uint32() return tuple([self.graal_readString() for _ in range(length)]) def graal_readSparseTable(self) -> Dict[int, tuple]: @@ -378,10 +371,10 @@ def graal_readSparseTable(self) -> Dict[int, tuple]: Python equvalent of Python Graal's readObjectArray() from MarshalModuleBuiltins.java """ - self.graal_readInt() # the length return value isn't used. + self.read_uint32() # the length return value isn't used. table = {} # new int[length][]; while True: - i = self.graal_readInt() + i = self.read_int32() if i == -1: return table table[i] = self.graal_readIntArray() @@ -494,15 +487,15 @@ def t_graal_CodeUnit(self, save_ref, bytes_for_s: bool = False): co_name = self.graal_readString() co_qualname = self.graal_readString() - co_argcount = self.graal_readInt() - co_kwonlyargcount = self.graal_readInt() - co_posonlyargcount = self.graal_readInt() + co_argcount = self.read_uint32() + co_kwonlyargcount = self.read_uint32() + co_posonlyargcount = self.read_uint32() - co_stacksize = self.graal_readInt() + co_stacksize = self.read_uint32() co_code_offset_in_file = self.fp.tell() co_code = self.graal_readBytes() other_fields["srcOffsetTable"] = self.graal_readBytes() - co_flags = self.graal_readInt() + co_flags = self.read_uint32() # writeStringArray(code.names); # writeStringArray(code.varnames); @@ -542,11 +535,11 @@ def t_graal_CodeUnit(self, save_ref, bytes_for_s: bool = False): other_fields["primitiveConstants"] = self.graal_readLongArray() other_fields["exception_handler_ranges"] = self.graal_readIntArray() - other_fields["condition_profileCount"] = self.graal_readInt() - other_fields["startLine"] = self.graal_readInt() - other_fields["startColumn"] = self.graal_readInt() - other_fields["endLine"] = self.graal_readInt() - other_fields["endColumn"] = self.graal_readInt() + other_fields["condition_profileCount"] = self.read_uint32() + other_fields["startLine"] = self.read_uint32() + other_fields["startColumn"] = self.read_uint32() + other_fields["endLine"] = self.read_uint32() + other_fields["endColumn"] = self.read_uint32() other_fields["outputCanQuicken"] = self.graal_readBytes() other_fields["variableShouldUnbox"] = self.graal_readBytes() other_fields["generalizeInputsMap"] = self.graal_readSparseTable() diff --git a/xdis/unmarsh_rust.py b/xdis/unmarsh_rust.py index 7add819f..52babac2 100644 --- a/xdis/unmarsh_rust.py +++ b/xdis/unmarsh_rust.py @@ -24,7 +24,6 @@ object. """ -from struct import unpack from typing import Any, Dict, List, Tuple, Union from xdis.codetype.code313rust import Code313Rust, SourceLocation @@ -277,18 +276,6 @@ def t_bigint(self, save_ref: bool=False, bytes_for_s: bool=False): value = int.from_bytes(byte_data, byteorder='little') return value if is_positive else -value - def read_int16(self): - return int(unpack(" bytes: - return self.fp.read(n) - - def read_uint32(self): - return int(unpack(" Union[bytes, str]: s = self.read_slice(n) if not bytes_for_s: diff --git a/xdis/unmarshal.py b/xdis/unmarshal.py index fc347218..2f02e579 100644 --- a/xdis/unmarshal.py +++ b/xdis/unmarshal.py @@ -195,6 +195,21 @@ def __init__(self, fp, magic_int, bytes_for_s, code_objects={}) -> None: self.UNMARSHAL_DISPATCH_TABLE = UNMARSHAL_DISPATCH_TABLE + def read_int16(self) -> int: + return int(unpack(" int: + return int(unpack(" int: + return int(unpack(" bytes: + return self.fp.read(n) + + def read_uint32(self) -> int: + return int(unpack(" bool: return True def t_int32(self, save_ref, bytes_for_s: bool = False): - return self.r_ref(int(unpack(" float: return float(self.fp.read(unpack("B", self.fp.read(1))[0])) def unpack_newer() -> float: - return float(self.fp.read(unpack(" 0: ret += (self.r_object(bytes_for_s=bytes_for_s),) @@ -443,7 +458,7 @@ def t_tuple(self, save_ref, bytes_for_s: bool = False): def t_list(self, save_ref, bytes_for_s: bool = False): # FIXME: check me - n = unpack(" 0: ret += (self.r_object(bytes_for_s=bytes_for_s),) @@ -451,7 +466,7 @@ def t_list(self, save_ref, bytes_for_s: bool = False): return ret def t_frozenset(self, save_ref, bytes_for_s: bool = False): - setsize = unpack(" 0: 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): return self.r_ref_insert(final_frozenset, i) def t_set(self, save_ref, bytes_for_s: bool = False): - setsize = unpack(" 0: 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): return ret def t_python2_string_reference(self, save_ref, bytes_for_s: bool = False): - refnum = unpack("= (2, 3): - co_argcount = unpack("= (1, 3): - co_argcount = unpack("= (3, 0): - kwonlyargcount = unpack("= (2, 3): - co_nlocals = unpack("= (1, 3): - co_nlocals = unpack("= (2, 3): - co_stacksize = unpack("= (1, 5): - co_stacksize = unpack("= (2, 3): - co_flags = unpack("= (1, 3): - co_flags = unpack("= (1, 5): if self.version_triple >= (2, 3): - co_firstlineno = unpack("= (3, 11) and not self.is_pypy: 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): # Since Python 3.4 def t_object_reference(self, save_ref=None, bytes_for_s: bool = False): - refnum = unpack("