Skip to content

Commit 2d289d0

Browse files
committed
Merge branch 'master' into python-3.7-to-3.10
2 parents e97794c + 606b4d2 commit 2d289d0

File tree

16 files changed

+169
-95
lines changed

16 files changed

+169
-95
lines changed

decompyle3/code_fns.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2015-2016, 2818-2024 by Rocky Bernstein
1+
# Copyright (c) 2015-2016, 2818-2025 by Rocky Bernstein
22
# Copyright (c) 2005 by Dan Pascu <[email protected]>
33
# Copyright (c) 2000-2002 by hartmut Goebel <[email protected]>
44
# Copyright (c) 1999 John Aycock
@@ -49,7 +49,7 @@ def disco_deparse(
4949
co,
5050
codename_map: dict,
5151
out,
52-
is_pypy,
52+
python_implementation,
5353
debug_opts,
5454
start_offset: int = 0,
5555
stop_offset: int = -1,
@@ -66,7 +66,7 @@ def disco_deparse(
6666
if co.co_filename:
6767
print(f"# Embedded file name: {co.co_filename}", file=real_out)
6868

69-
scanner = get_scanner(version, is_pypy=is_pypy)
69+
scanner = get_scanner(version, python_implementation=python_implementation)
7070

7171
queue = deque([co])
7272
disco_deparse_loop(
@@ -75,7 +75,7 @@ def disco_deparse(
7575
codename_map,
7676
queue,
7777
real_out,
78-
is_pypy,
78+
python_implementation,
7979
debug_opts,
8080
start_offset=start_offset,
8181
stop_offset=stop_offset,
@@ -88,7 +88,7 @@ def disco_deparse_loop(
8888
codename_map: dict,
8989
queue,
9090
real_out,
91-
is_pypy,
91+
python_implementation,
9292
debug_opts,
9393
start_offset: int = 0,
9494
stop_offset: int = -1,
@@ -108,7 +108,7 @@ def disco_deparse_loop(
108108
real_out,
109109
version=version,
110110
debug_opts=debug_opts,
111-
is_pypy=is_pypy,
111+
python_implementation=python_implementation,
112112
compile_mode=codename_map[co.co_name],
113113
start_offset=start_offset,
114114
stop_offset=stop_offset,
@@ -149,7 +149,7 @@ def decompile_code_type(
149149
print(f"Skipping {filename}:\n{e}")
150150
return False
151151

152-
(version, _, _, co, is_pypy, _, _) = load_module(filename)
152+
(version, _, _, co, python_implementation, _, _, _) = load_module(filename)
153153

154154
# maybe a second -a will do before as well
155155
# asm = "after" if showasm else None
@@ -162,7 +162,7 @@ def decompile_code_type(
162162
bytecode,
163163
codename_map,
164164
outstream,
165-
is_pypy,
165+
python_implementation,
166166
debug_opts,
167167
start_offset=start_offset,
168168
stop_offset=stop_offset,
@@ -173,7 +173,7 @@ def decompile_code_type(
173173
co,
174174
codename_map,
175175
outstream,
176-
is_pypy,
176+
python_implementation,
177177
debug_opts,
178178
start_offset=start_offset,
179179
stop_offset=stop_offset,

decompyle3/disas.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2015-2016, 2818-2020, 2024 by Rocky Bernstein
1+
# Copyright (c) 2015-2016, 2818-2020, 2024-2025 by Rocky Bernstein
22
# Copyright (c) 2005 by Dan Pascu <[email protected]>
33
# Copyright (c) 2000-2002 by hartmut Goebel <[email protected]>
44
# Copyright (c) 1999 John Aycock
@@ -98,14 +98,21 @@ def disassemble_file(filename: str, outstream=None) -> None:
9898
try to find the corresponding compiled object.
9999
"""
100100
filename = check_object_path(filename)
101-
(version, timestamp, magic_int, co, is_pypy, source_size, sip_hash) = load_module(
102-
filename
103-
)
101+
(
102+
version,
103+
timestamp,
104+
magic_int,
105+
co,
106+
python_implementation,
107+
source_size,
108+
sip_hash,
109+
_,
110+
) = load_module(filename)
104111
if isinstance(co, list):
105112
for con in co:
106113
disco(version, con, outstream)
107114
else:
108-
disco(version, co, outstream, is_pypy=is_pypy)
115+
disco(version, co, outstream, python_implementation)
109116
co = None
110117

111118

decompyle3/linenumbers.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2015-2016, 2018-2020 by Rocky Bernstein
1+
# Copyright (c) 2015-2016, 2018-2020, 2025 by Rocky Bernstein
22
#
33
# This program is free software: you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License as published by
@@ -17,12 +17,12 @@
1717

1818
from xdis import (
1919
Bytecode,
20-
iscode,
2120
findlinestarts,
2221
get_opcode,
23-
offset2line,
22+
iscode,
2423
load_file,
2524
load_module,
25+
offset2line,
2626
)
2727

2828

@@ -32,9 +32,10 @@ def line_number_mapping(pyc_filename, src_filename):
3232
timestamp,
3333
magic_int,
3434
code1,
35-
is_pypy,
35+
python_implementation,
3636
source_size,
3737
sip_hash,
38+
_,
3839
) = load_module(pyc_filename)
3940
try:
4041
code2 = load_file(src_filename)
@@ -45,7 +46,7 @@ def line_number_mapping(pyc_filename, src_filename):
4546

4647
mappings = []
4748

48-
opc = get_opcode(version, is_pypy)
49+
opc = get_opcode(version, python_implementation)
4950
number_loop(queue, mappings, opc)
5051
return sorted(mappings, key=lambda x: x[1])
5152

decompyle3/main.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@
2424
from typing import Any, Optional, TextIO, Tuple
2525

2626
from xdis import iscode, load_module
27-
from xdis.version_info import IS_PYPY, PYTHON_VERSION_TRIPLE, version_tuple_to_str
27+
from xdis.version_info import (
28+
IS_PYPY,
29+
PYTHON_VERSION_TRIPLE,
30+
PythonImplementation,
31+
version_tuple_to_str,
32+
)
2833

2934
from decompyle3.disas import check_object_path
3035
from decompyle3.parsers.parse_heads import ParserError
@@ -71,7 +76,7 @@ def decompile(
7176
source_encoding=None,
7277
code_objects={},
7378
source_size=None,
74-
is_pypy: bool = False,
79+
python_implementation=PythonImplementation.CPython,
7580
magic_int=None,
7681
mapstream=None,
7782
do_fragments=False,
@@ -99,7 +104,7 @@ def write(s):
99104

100105
assert iscode(co), f"""{co} does not smell like code"""
101106

102-
co_pypy_str = "PyPy " if is_pypy else ""
107+
co_pypy_str = "PyPy " if python_implementation is PythonImplementation.PyPy else ""
103108
run_pypy_str = "PyPy " if IS_PYPY else ""
104109
sys_version_lines = sys.version.split("\n")
105110
if source_encoding:
@@ -141,7 +146,7 @@ def write(s):
141146
out=out,
142147
version=bytecode_version,
143148
code_objects=code_objects,
144-
is_pypy=is_pypy,
149+
python_implementation=python_implementation,
145150
debug_opts=debug_opts,
146151
)
147152
header_count = 3 + len(sys_version_lines)
@@ -160,7 +165,7 @@ def write(s):
160165
co,
161166
out,
162167
bytecode_version,
163-
is_pypy=is_pypy,
168+
python_implementation=python_implementation,
164169
debug_opts=debug_opts,
165170
compile_mode=compile_mode,
166171
start_offset=start_offset,
@@ -209,9 +214,16 @@ def decompile_file(
209214

210215
filename = check_object_path(filename)
211216
code_objects = {}
212-
version, timestamp, magic_int, co, is_pypy, source_size, _ = load_module(
213-
filename, code_objects
214-
)
217+
(
218+
version,
219+
timestamp,
220+
magic_int,
221+
co,
222+
python_implementation,
223+
source_size,
224+
_,
225+
_,
226+
) = load_module(filename, code_objects)
215227

216228
if isinstance(co, list):
217229
deparsed = []
@@ -227,7 +239,7 @@ def decompile_file(
227239
showgrammar,
228240
source_encoding,
229241
code_objects=code_objects,
230-
is_pypy=is_pypy,
242+
python_implementation=python_implementation,
231243
magic_int=magic_int,
232244
mapstream=mapstream,
233245
start_offset=start_offset,
@@ -247,7 +259,7 @@ def decompile_file(
247259
source_encoding,
248260
code_objects=code_objects,
249261
source_size=source_size,
250-
is_pypy=is_pypy,
262+
python_implementation=python_implementation,
251263
magic_int=magic_int,
252264
mapstream=mapstream,
253265
do_fragments=do_fragments,

decompyle3/parsers/main.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2019-2024 Rocky Bernstein
1+
# Copyright (c) 2019-2025 Rocky Bernstein
22
#
33
# This program is free software: you can redistribute it and/or modify
44
# it under the terms of the GNU General Public License as published by
@@ -25,7 +25,12 @@
2525

2626
from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG
2727
from xdis import iscode
28-
from xdis.version_info import IS_PYPY, PYTHON_VERSION_TRIPLE, version_tuple_to_str
28+
from xdis.version_info import (
29+
PYTHON_IMPLEMENTATION,
30+
PYTHON_VERSION_TRIPLE,
31+
PythonImplementation,
32+
version_tuple_to_str,
33+
)
2934

3035
from decompyle3.parsers.p37.heads import (
3136
Python37ParserEval,
@@ -63,7 +68,10 @@ def parse(p, tokens, customize, is_lambda: bool) -> SyntaxTree:
6368

6469

6570
def get_python_parser(
66-
version, debug_parser=PARSER_DEFAULT_DEBUG, compile_mode="exec", is_pypy=False
71+
version,
72+
debug_parser=PARSER_DEFAULT_DEBUG,
73+
compile_mode="exec",
74+
python_implementation=PythonImplementation.CPython,
6775
):
6876
"""
6977
Returns parser object for Python version 3.7, 3.8, etc. depending on the parameters
@@ -105,32 +113,32 @@ def get_python_parser(
105113
p = Python37ParserSingle(debug_parser)
106114
elif version == (3, 8):
107115
if compile_mode == "exec":
108-
if is_pypy:
116+
if python_implementation is PythonImplementation.PyPy:
109117
p = Python38PyPyParserExec(debug_parser=debug_parser)
110118
else:
111119
p = Python38ParserExec(debug_parser=debug_parser)
112120

113121
elif compile_mode == "single":
114-
if is_pypy:
122+
if python_implementation is PythonImplementation.PyPy:
115123
p = Python38PyPyParserSingle(debug_parser=debug_parser)
116124
else:
117125
p = Python38ParserSingle(debug_parser=debug_parser)
118126
elif compile_mode == "lambda":
119-
if is_pypy:
127+
if python_implementation is PythonImplementation.PyPy:
120128
p = Python38PyPyParserLambda(debug_parser=debug_parser)
121129
else:
122130
p = Python38ParserLambda(debug_parser=debug_parser)
123131
elif compile_mode == "eval":
124-
if is_pypy:
132+
if python_implementation is PythonImplementation.PyPy:
125133
p = Python38PyPyParserEval(debug_parser=debug_parser)
126134
else:
127135
p = Python38ParserEval(debug_parser=debug_parser)
128136
elif compile_mode == "expr":
129-
if is_pypy:
137+
if python_implementation is PythonImplementation.PyPy:
130138
p = Python38PyPyParserExpr(debug_parser=debug_parser)
131139
else:
132140
p = Python38ParserExpr(debug_parser=debug_parser)
133-
elif is_pypy:
141+
elif python_implementation is PythonImplementation.PyPy:
134142
p = Python38PyPyParserSingle(debug_parser)
135143
else:
136144
p = Python38ParserSingle(debug_parser)
@@ -152,7 +160,7 @@ def python_parser(
152160
showasm: bool = False,
153161
parser_debug=PARSER_DEFAULT_DEBUG,
154162
compile_mode: str = "exec",
155-
is_pypy: bool = False,
163+
python_implementation: PythonImplementation = PythonImplementation.CPython,
156164
is_lambda: bool = False,
157165
) -> SyntaxTree:
158166
"""
@@ -176,15 +184,18 @@ def python_parser(
176184
assert iscode(co)
177185
from decompyle3.scanner import get_scanner
178186

179-
scanner = get_scanner(version, is_pypy)
187+
scanner = get_scanner(version, python_implementation)
180188
tokens, customize = scanner.ingest(co)
181189
maybe_show_asm(showasm, tokens)
182190

183191
# For heavy grammar debugging
184192
# parser_debug = {'rules': True, 'transition': True, 'reduce' : True,
185193
# 'showstack': 'full'}
186194
p = get_python_parser(
187-
version, parser_debug, compile_mode=compile_mode, is_pypy=IS_PYPY
195+
version,
196+
parser_debug,
197+
compile_mode=compile_mode,
198+
python_implementation=PYTHON_IMPLEMENTATION,
188199
)
189200

190201
# FIXME: have p.insts update in a better way
@@ -199,8 +210,12 @@ def python_parser(
199210
if __name__ == "__main__":
200211

201212
def parse_test(co) -> None:
202-
203-
tree = python_parser(co, (3, 8, 2), showasm=True, is_pypy=IS_PYPY)
213+
tree = python_parser(
214+
co,
215+
(3, 8, 2),
216+
showasm=True,
217+
python_implementation=PythonImplementation.CPython,
218+
)
204219
print(tree)
205220
print("+" * 30)
206221
return

0 commit comments

Comments
 (0)