Skip to content

Commit bd33c17

Browse files
author
rocky
committed
Start handling most recent RustPython..
It uses CPython 3.12's magic number!
1 parent 2492b40 commit bd33c17

File tree

7 files changed

+515
-20
lines changed

7 files changed

+515
-20
lines changed

xdis/load.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,24 @@ def load_module_from_file_object(
228228
magic = fp.read(4)
229229
magic_int = magic2int(magic)
230230

231+
if magic_int == 3531:
232+
# this magic int is used for both 3.12 and 3.13Rust!
233+
# Disambiguate using the fact that CPython 3.13 stores 0xe3
234+
# "c" | 0x80 at offoset 0x10 while RustPython uses "c" (no 0x80).
235+
fp.seek(0x10)
236+
code_type = fp.read(1)
237+
if code_type == b'c':
238+
# Is RustPython 3.13 using CPython's 3.12 magic number.
239+
magic_int = 35310
240+
else:
241+
assert code_type == b'\xe3', "Expecting magic int 3531 to have a code type b'0x63 or b'0x33' at offset 0x10"
242+
fp.seek(0x04)
243+
231244
# For reasons I don't understand, PyPy 3.2 stores a magic
232245
# of '0'... The two values below are for Python 2.x and 3.x respectively
233246
if magic[0:1] in ["0", b"0"]:
234247
magic = int2magic(3180 + 7)
248+
magic_int = magic2int(magic)
235249

236250
try:
237251
# FIXME: use the internal routine below
@@ -245,10 +259,7 @@ def load_module_from_file_object(
245259
else:
246260
raise ImportError(f"Bad magic number: '{magic}'")
247261

248-
if magic_int in [2657, 22138] + list(
249-
RUSTPYTHON_MAGICS
250-
) + list(JYTHON_MAGICS):
251-
version = magicint2version.get(magic_int, "")
262+
version = magic_int2tuple(magic_int)
252263

253264
if magic_int in INTERIM_MAGIC_INTS:
254265
raise ImportError(
@@ -268,8 +279,6 @@ def load_module_from_file_object(
268279

269280
try:
270281
my_magic_int = PYTHON_MAGIC_INT
271-
magic_int = magic2int(magic)
272-
version = magic_int2tuple(magic_int)
273282

274283
timestamp = None
275284
source_size = None

xdis/magics.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
12897, # RustPython 3.12.0 0.4.0
5454
13413, # RustPython 3.13
5555
24881, # RustPython 3.13 0.4.0
56-
3531, # RustPython 3.13 0.4.0 later version!
56+
35310, # RustPython 3.13 0.4.0 later version! Actually 3531 is stored in file
5757
)
5858

5959
# A list of interim Python version magic numbers used, but were not
@@ -701,6 +701,9 @@ def __by_version(magic_versions: Dict[bytes, str]) -> dict:
701701
add_magic_from_int(13413, "3.13.0a.rust") # RustPython 3.13.0
702702
add_magic_from_int(24881, "3.13.0b.rust") # RustPython 3.13.0 0.4.0
703703

704+
# Actually we should add 3531, but that already means CPython 3.12!
705+
add_magic_from_int(35310, "3.13.1.rust") # RustPython 3.13.0 0.4.0
706+
704707
# Graal Python. Graal uses its own JVM-ish CPython bytecode, not
705708
# true CPython or PyPy bytecode.
706709
#

xdis/op_imports.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
from typing import Tuple
2020

2121
from xdis.magics import canonic_python_version
22-
from xdis.opcodes import opcode_12897rust # 3.13.0 0.4.0
23-
from xdis.opcodes import opcode_24481rust # 3.13.0 0.4.0
2422
from xdis.opcodes import (
2523
opcode_10,
2624
opcode_11,
@@ -63,6 +61,9 @@
6361
opcode_312,
6462
opcode_313,
6563
opcode_314,
64+
opcode_3531rust,
65+
opcode_12897rust,
66+
opcode_24481rust,
6667
)
6768
from xdis.opcodes.opcode_graal import (
6869
opcode_38graal,
@@ -193,6 +194,7 @@
193194
"3.12.0": opcode_312,
194195
"3.13.0rc3": opcode_313,
195196
"3.13.0Rust": opcode_24481rust,
197+
"3.13.1Rust": opcode_3531rust,
196198
"3.14b3": opcode_314,
197199
"3.14.0": opcode_314,
198200
"3.14": opcode_314,

xdis/opcodes/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
opcode_310pypy,
6969
opcode_311pypy,
7070
)
71-
from xdis.opcodes.opcode_rust import opcode_12897rust, opcode_24481rust
71+
from xdis.opcodes.opcode_rust import opcode_3531rust, opcode_12897rust, opcode_24481rust
7272

7373
# from xdis.opcodes.opcode_graal import (
7474
# opcode_310graal,
@@ -122,8 +122,8 @@
122122
"opcode_39pypy",
123123
# "opcode_310graal",
124124
# "opcode_311graal",
125-
"opcode_38graal",
126125
# "opcode_312rust",
126+
"opcode_3531rust",
127127
"opcode_12897rust",
128128
"opcode_24481rust",
129129
]

xdis/opcodes/opcode_rust/base.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def make_opcodes(loc: dict, magic_int: int):
239239
name_op_graal(loc, "LOAD_ATTR")
240240
compare_op_graal(loc, "TEST_OP", 2, 1) # test operator
241241
if magic_int in (24881,):
242-
def_op_graal(loc, "CopyItem")
242+
def_op_graal(loc, "COPY")
243243
binary_op_graal(loc, "BINARY_SUBCR")
244244
compare_op_graal(loc, "COMPARE_OP", 2, 1) # Comparison operator
245245
if magic_int in (24881,):
@@ -256,10 +256,10 @@ def make_opcodes(loc: dict, magic_int: int):
256256

257257
if magic_int in (24881,):
258258
def_op_graal(loc, "GetLen")
259-
def_op_graal(loc, "CallIntrinsic1")
260-
def_op_graal(loc, "CallIntrinsic2")
261-
def_op_graal(loc, "Continue")
262-
def_op_graal(loc, "Break")
259+
call_op_graal(loc, "CallIntrinsic1")
260+
call_op_graal(loc, "CallIntrinsic2")
261+
def_op_graal(loc, "CONTINUE_LOOP")
262+
def_op_graal(loc, "BREAK_LOOP")
263263

264264
# In contrast to CPython and others Rust does not seem to use relative jumps,
265265
# but only absolute jumps.
@@ -283,7 +283,7 @@ def make_opcodes(loc: dict, magic_int: int):
283283
call_op_graal(loc, "CALL_METHOD_KW")
284284
call_op_graal(loc, "CALL_METHOD_EX")
285285

286-
jrel_op_graal(loc, "FOR_ITER")
286+
jabs_op_graal(loc, "FOR_ITER")
287287
def_op_graal(loc, "RETURN_VALUE")
288288
const_op_graal(loc, "RETURN_CONST")
289289
def_op_graal(loc, "YIELD")
@@ -292,7 +292,7 @@ def make_opcodes(loc: dict, magic_int: int):
292292
def_op_graal(loc, "RESUME", 0, 0)
293293

294294
def_op_graal(loc, "SETUP_ANNOTATIONS")
295-
jrel_op_graal(loc, "SETUP_LOOP", 0, 0, conditional=True)
295+
jabs_op_graal(loc, "SETUP_LOOP", 0, 0, conditional=True)
296296
def_op_graal(loc, "SETUP_FINALLY", 0, 1)
297297

298298
if magic_int in (12897,):

0 commit comments

Comments
 (0)