Skip to content

Commit 6d2b082

Browse files
author
rocky
committed
Add second Rust bytecode (for 3.12)
1 parent b5cafe1 commit 6d2b082

File tree

11 files changed

+683
-733
lines changed

11 files changed

+683
-733
lines changed

xdis/codetype/code312rust.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# (C) Copyright 2025 by Rocky Bernstein
2+
#
3+
# This program is free software; you can redistribute it and/or
4+
# modify it under the terms of the GNU General Public License
5+
# as published by the Free Software Foundation; either version 2
6+
# of the License, or (at your option) any later version.
7+
#
8+
# This program is distributed in the hope that it will be useful,
9+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
# GNU General Public License for more details.
12+
#
13+
# You should have received a copy of the GNU General Public License
14+
# along with this program; if not, write to the Free Software
15+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16+
17+
from dataclasses import dataclass
18+
from typing import Any, Dict, Tuple, Union
19+
20+
from xdis.codetype.code311 import Code311
21+
22+
23+
@dataclass
24+
class SourceLocation:
25+
# 1-based integer line number
26+
line_number: int
27+
# column offset in line: 1-based int (constructed from a zero-indexed stored value)
28+
column_offset: int
29+
30+
31+
class Code312Rust(Code311):
32+
"""Class for a RustPython 3.12 code object used when a Python
33+
interpreter is not RustPython 3.12 but working on RustPython 3.12 bytecode. It
34+
also functions as an object that can be used to build or write a
35+
Python3 code object, since we allow mutable structures.
36+
37+
When done mutating, call method to_native().
38+
39+
For convenience in generating code objects, fields like
40+
`co_consts`, co_names which are (immutable) tuples in the end-result can be stored
41+
instead as (mutable) lists. Likewise, the line number table `co_lnotab`
42+
can be stored as a simple list of offset, line_number tuples.
43+
44+
"""
45+
def __init__(
46+
self,
47+
co_argcount: int,
48+
co_posonlyargcount: int,
49+
co_kwonlyargcount: int,
50+
co_stacksize: int,
51+
co_flags: int,
52+
co_code: bytes,
53+
co_consts: tuple,
54+
co_names: tuple[str],
55+
co_varnames: tuple[str],
56+
co_filename: str,
57+
co_name: str,
58+
co_firstlineno: int,
59+
co_freevars: tuple,
60+
co_cellvars: tuple,
61+
version_triple: Tuple[int, int, int],
62+
locations: tuple,
63+
collection_order: Dict[Union[set, frozenset, dict], Tuple[Any]] = {},
64+
) -> None:
65+
self.co_argcount = co_argcount
66+
self.co_posonlyargcount = co_posonlyargcount
67+
self.co_kwonlyargcount = co_kwonlyargcount
68+
self.co_lnotab={} # FIXME: compute this from locations.
69+
self.co_stacksize = co_stacksize
70+
self.co_flags = co_flags
71+
self.co_code = co_code
72+
self.co_consts = co_consts
73+
self.co_names = co_names
74+
self.co_varnames = co_varnames
75+
self.co_filename = co_filename
76+
self.co_name = co_name
77+
self.co_firstlineno = co_firstlineno # None if 0 in serialized form
78+
self.co_cellvars = co_cellvars
79+
self.co_freevars= co_freevars
80+
self.co_collection_order = collection_order
81+
self.version_triple = version_triple
82+
self.locations = locations

xdis/codetype/code313rust.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ def __init__(
8282
self.co_linetable = co_linetable
8383
self.co_qualname = co_qualname
8484
self.co_cellvars = co_cellvars
85-
self.co_linetable = co_linetable
8685
self.co_freevars= co_freevars
8786
self.co_exceptiontable = co_exceptiontable
8887
self.co_collection_order = collection_order

xdis/cross_dis.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def findlinestarts(code, dup_lines: bool = False):
170170
Generate pairs (offset, lineno) as described in Python/compile.c.
171171
"""
172172

173-
if hasattr(code, "co_lines"):
173+
if hasattr(code, "co_lines") and hasattr(code, "co_linetable"):
174174
# Taken from 3.10 findlinestarts
175175
lastline = None
176176
for start, _, line in code.co_lines():
@@ -325,7 +325,7 @@ def format_code_info(
325325
lines.append("# Keyword-only arguments: %s" % co.co_kwonlyargcount)
326326

327327
pos_argc = co.co_argcount
328-
if version_tuple >= (1, 3):
328+
if hasattr(co, "co_nlocals"):
329329
lines.append("# Number of locals: %s" % co.co_nlocals)
330330
if version_tuple >= (1, 5):
331331
lines.append("# Stack size: %s" % co.co_stacksize)

xdis/magics.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@
4747
# See below for mapping to version numbers.
4848
PYPY3_MAGICS = (48, 64, 112, 160, 192, 240, 244, 256, 320, 336, 384, 416)
4949

50+
# Rust Magics is a git commit number!
5051
RUSTPYTHON_MAGICS = (
5152
12641, # RustPython 3.12
52-
12897, # RustPython 3.12
53+
12897, # RustPython 3.12.0 0.4.0
5354
13413, # RustPython 3.13
54-
24881, # RustPython 3.13
55+
24881, # RustPython 3.13 0.4.0
5556
)
5657

5758
# A list of interim Python version magic numbers used, but were not
@@ -695,9 +696,9 @@ def __by_version(magic_versions: Dict[bytes, str]) -> dict:
695696
add_magic_from_int(416, "3.11.13PyPy") # PyPy 3.11.13 or pypy3.11-7.3.20
696697

697698
add_magic_from_int(12641, "3.12.0a.rust") # RustPython 3.12.0
698-
add_magic_from_int(12897, "3.13.0b.rust") # RustPython 3.12.0
699+
add_magic_from_int(12897, "3.12.0.rust") # RustPython 3.12.0 0.4.0
699700
add_magic_from_int(13413, "3.13.0a.rust") # RustPython 3.13.0
700-
add_magic_from_int(24881, "3.13.0b.rust") # RustPython 3.13.0
701+
add_magic_from_int(24881, "3.13.0b.rust") # RustPython 3.13.0 0.4.0
701702

702703
# Graal Python. Graal uses its own JVM-ish CPython bytecode, not
703704
# true CPython or PyPy bytecode.

xdis/op_imports.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
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
2224
from xdis.opcodes import (
2325
opcode_10,
2426
opcode_11,
@@ -60,7 +62,6 @@
6062
opcode_311pypy,
6163
opcode_312,
6264
opcode_313,
63-
opcode_313rust,
6465
opcode_314,
6566
)
6667
from xdis.opcodes.opcode_graal import (
@@ -187,10 +188,11 @@
187188
3.11: opcode_311,
188189
"3.12.7Graal": opcode_312graal, # this right?
189190
"3.12.8Graal": opcode_312graal, # this right?
191+
"3.12.0Rust": opcode_12897rust,
190192
"3.12.0rc2": opcode_312,
191193
"3.12.0": opcode_312,
192194
"3.13.0rc3": opcode_313,
193-
"3.13.0Rust": opcode_313rust,
195+
"3.13.0Rust": opcode_24481rust,
194196
"3.14b3": opcode_314,
195197
"3.14.0": opcode_314,
196198
"3.14": opcode_314,
@@ -211,7 +213,7 @@ def get_opcode_module(version_info: Tuple[int, ...], implementation: PythonImple
211213
if vers_str not in canonic_python_version:
212214
vers_str = version_tuple_to_str(version_info[:2])
213215

214-
if implementation != PythonImplementation.CPython:
216+
if str(implementation) != str(PythonImplementation.CPython):
215217
vers_str += str(implementation)
216218

217219
return op_imports[canonic_python_version[vers_str]]
@@ -330,4 +332,4 @@ def remap_opcodes(op_obj, alternate_opmap):
330332

331333
if __name__ == "__main__":
332334
from version_info import PYTHON_IMPLEMENTATION, PYTHON_VERSION_TRIPLE
333-
print(get_opcode_module(PYTHON_VERSION_TRIPLE, PYTHON_IMPLEMENTATION))
335+
print(get_opcode_module(PYTHON_VERSION_TRIPLE[:2], PYTHON_IMPLEMENTATION))

xdis/opcodes/__init__.py

Lines changed: 3 additions & 5 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_313rust # opcode_312rust,
71+
from xdis.opcodes.opcode_rust import opcode_12897rust, opcode_24481rust
7272

7373
# from xdis.opcodes.opcode_graal import (
7474
# opcode_310graal,
@@ -78,7 +78,6 @@
7878
# )
7979

8080

81-
8281
__all__ = [
8382
"opcode_10",
8483
"opcode_11",
@@ -123,9 +122,8 @@
123122
"opcode_39pypy",
124123
# "opcode_310graal",
125124
# "opcode_311graal",
126-
# "opcode_312graal",
127125
"opcode_38graal",
128126
# "opcode_312rust",
129-
"opcode_313rust",
130-
127+
"opcode_12897rust",
128+
"opcode_24481rust",
131129
]

0 commit comments

Comments
 (0)