2929import io
3030import marshal
3131import sys
32- from struct import unpack
32+ from struct import unpack , unpack_from
3333from types import EllipsisType
3434from typing import Any , Dict , Tuple , Union
3535
@@ -147,6 +147,9 @@ def long(n: int) -> LongTypeForPython3:
147147 TYPE_UNKNOWN : "unknown" ,
148148}
149149
150+ # Used by graal
151+ JAVA_MARSHAL_SHIFT = 15
152+ JAVA_MARSHAL_BASE = 1 << JAVA_MARSHAL_SHIFT
150153
151154def compat_str (s : Union [str , bytes ]) -> Union [str , bytes ]:
152155 """
@@ -244,8 +247,8 @@ def t_graal_readArray(self, save_ref: bool, bytes_for_s: bool) -> tuple:
244247 # print(marshal_type) # debug
245248
246249 match marshal_type :
247- # case "B":
248- # ret = tuple ()
250+ case "B" :
251+ ret = self . graal_readBigInteger ()
249252 # case "b":
250253 # return "OK"
251254 case "d" :
@@ -269,6 +272,48 @@ def t_graal_readArray(self, save_ref: bool, bytes_for_s: bool) -> tuple:
269272 self .intern_objects .append (ret )
270273 return ret
271274
275+ def graal_readBigInteger (self ):
276+ """
277+ Reads a marshaled big integer from the input stream.
278+ """
279+ negative = False
280+ sz = self .graal_readInt () # Get the size in shorts
281+ if sz < 0 :
282+ negative = True
283+ sz = - sz
284+
285+ # size is in shorts, convert to size in bytes
286+ sz *= 2
287+
288+ data = bytes ([self .graal_readByte () for _ in range (sz )])
289+
290+ i = 0
291+
292+ # Read the first 2 bytes as a 16-bit signed integer (short)
293+ # '>h' specifies big-endian signed short
294+ digit = unpack_from ('>h' , data , i )[0 ]
295+ i += 2
296+
297+ # Python int handles arbitrarily large numbers
298+ result = digit
299+
300+ while i < sz :
301+ # Calculate the power based on the number of shorts processed so far
302+ power = i // 2
303+ # Read the next 2 bytes as a 16-bit signed integer
304+ digit = unpack_from ('>h' , data , i )[0 ]
305+ i += 2
306+
307+ # In Python, int supports all these operations directly
308+ # The Java code effectively reconstructs the number using base 2^16 (MARSHAL_BASE)
309+ term = digit * (JAVA_MARSHAL_BASE ** power )
310+ result += term
311+
312+ if negative :
313+ return - result
314+ else :
315+ return result
316+
272317 def graal_readByte (self ) -> int :
273318 """
274319 Python equivalent of Python Graal's readBytes() from
0 commit comments