Skip to content

Commit 8348d86

Browse files
committed
Better annotation parsing for < 3.6
1 parent 454fac4 commit 8348d86

File tree

5 files changed

+63
-61
lines changed

5 files changed

+63
-61
lines changed

uncompyle6/bin/uncompile.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
import click
1616
from xdis.version_info import version_tuple_to_str
1717

18-
from uncompyle6 import verify
1918
from uncompyle6.main import main, status_msg
19+
from uncompyle6.verify import VerifyCmpError
2020
from uncompyle6.version import __version__
2121

2222
program = "uncompyle6"
@@ -245,7 +245,7 @@ def main_bin(
245245
sys.exit(2)
246246
except KeyboardInterrupt:
247247
pass
248-
except verify.VerifyCmpError:
248+
except VerifyCmpError:
249249
raise
250250
else:
251251
from multiprocessing import Process, Queue
@@ -266,18 +266,18 @@ def main_bin(
266266
tot_files = okay_files = failed_files = verify_failed_files = 0
267267

268268
def process_func():
269+
(tot_files, okay_files, failed_files, verify_failed_files) = (
270+
0,
271+
0,
272+
0,
273+
0,
274+
)
269275
try:
270-
(tot_files, okay_files, failed_files, verify_failed_files) = (
271-
0,
272-
0,
273-
0,
274-
0,
275-
)
276276
while 1:
277277
f = fqueue.get()
278278
if f is None:
279279
break
280-
(t, o, f, v) = main(src_base, out_base, [f], [], outfile, **options)
280+
(t, o, f, v) = main(src_base, out_base, [f], [], outfile)
281281
tot_files += t
282282
okay_files += o
283283
failed_files += f

uncompyle6/parsers/parse3.py

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2015-2023 Rocky Bernstein
1+
# Copyright (c) 2015-2024 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
@@ -1221,9 +1221,24 @@ def customize_grammar_rules(self, tokens, customize):
12211221
)
12221222
)
12231223
else:
1224-
rule = "mkfunc ::= %s%sload_closure LOAD_CODE LOAD_STR %s" % (
1225-
kwargs_str,
1226-
"pos_arg " * pos_args_count,
1224+
if self.version == (3, 3):
1225+
# 3.3 puts kwargs before pos_arg
1226+
pos_kw_tuple = (
1227+
("kwargs " * kw_args_count),
1228+
("pos_arg " * pos_args_count),
1229+
)
1230+
else:
1231+
# 3.4 and 3.5 puts pos_arg before kwargs
1232+
pos_kw_tuple = (
1233+
"pos_arg " * (pos_args_count),
1234+
("kwargs " * kw_args_count),
1235+
)
1236+
rule = (
1237+
"mkfunc ::= %s%s%s " "load_closure LOAD_CODE LOAD_STR %s"
1238+
) % (
1239+
pos_kw_tuple[0],
1240+
pos_kw_tuple[1],
1241+
"annotate_pair " * (annotate_args),
12271242
opname,
12281243
)
12291244
self.add_unique_rule(rule, opname, token.attr, customize)
@@ -1465,12 +1480,12 @@ def customize_grammar_rules(self, tokens, customize):
14651480
)
14661481
self.add_unique_rule(rule, opname, token.attr, customize)
14671482
rule = (
1468-
"mkfunc_annotate ::= %s%sannotate_tuple LOAD_CODE LOAD_STR %s"
1469-
% (
1470-
("pos_arg " * pos_args_count),
1471-
("annotate_arg " * annotate_args),
1472-
opname,
1473-
)
1483+
"mkfunc_annotate ::= %s%sannotate_tuple LOAD_CODE "
1484+
"LOAD_STR %s"
1485+
) % (
1486+
("pos_arg " * pos_args_count),
1487+
("annotate_arg " * annotate_args),
1488+
opname,
14741489
)
14751490
if self.version >= (3, 3):
14761491
if self.version == (3, 3):
@@ -1480,29 +1495,19 @@ def customize_grammar_rules(self, tokens, customize):
14801495
("pos_arg " * pos_args_count),
14811496
)
14821497
else:
1483-
# 3.4 and 3.5puts pos_arg before kwargs
1498+
# 3.4 and 3.5 puts pos_arg before kwargs
14841499
pos_kw_tuple = (
14851500
"pos_arg " * (pos_args_count),
14861501
("kwargs " * kw_args_count),
14871502
)
14881503
rule = (
1489-
"mkfunc_annotate ::= %s%s%sannotate_tuple LOAD_CODE LOAD_STR %s"
1490-
% (
1491-
pos_kw_tuple[0],
1492-
pos_kw_tuple[1],
1493-
("annotate_arg " * annotate_args),
1494-
opname,
1495-
)
1496-
)
1497-
self.add_unique_rule(rule, opname, token.attr, customize)
1498-
rule = (
1499-
"mkfunc_annotate ::= %s%s%sannotate_tuple LOAD_CODE LOAD_STR %s"
1500-
% (
1501-
pos_kw_tuple[0],
1502-
pos_kw_tuple[1],
1503-
("annotate_arg " * annotate_args),
1504-
opname,
1505-
)
1504+
"mkfunc_annotate ::= %s%s%sannotate_tuple LOAD_CODE "
1505+
"LOAD_STR %s"
1506+
) % (
1507+
pos_kw_tuple[0],
1508+
pos_kw_tuple[1],
1509+
("annotate_arg " * annotate_args),
1510+
opname,
15061511
)
15071512
else:
15081513
rule = (

uncompyle6/parsers/parse33.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ def p_33on(self, args):
1616
stmt ::= genexpr_func
1717
"""
1818

19+
def p_33_function_def(self, args):
20+
"""
21+
annotate_pair ::= LOAD_NAME LOAD_CONST
22+
23+
"""
24+
1925
def customize_grammar_rules(self, tokens, customize):
2026
self.remove_rules(
2127
"""

uncompyle6/scanners/scanner35.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2017, 2021-2022 by Rocky Bernstein
1+
# Copyright (c) 2017, 2021-2022, 2024 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
@@ -22,21 +22,22 @@
2222
scanner routine for Python 3.
2323
"""
2424

25-
from __future__ import print_function
25+
# bytecode verification, verify(), uses JUMP_OPs from here
26+
from xdis.opcodes import opcode_35 as opc
2627

2728
from uncompyle6.scanners.scanner3 import Scanner3
2829

29-
# bytecode verification, verify(), uses JUMP_OPs from here
30-
from xdis.opcodes import opcode_35 as opc
3130
JUMP_OPS = opc.JUMP_OPS
3231

33-
class Scanner35(Scanner3):
3432

33+
class Scanner35(Scanner3):
3534
def __init__(self, show_asm=None, is_pypy=False):
3635
Scanner3.__init__(self, (3, 5), show_asm, is_pypy)
3736
return
37+
3838
pass
3939

40+
4041
if __name__ == "__main__":
4142
from xdis.version_info import PYTHON_VERSION_TRIPLE, version_tuple_to_str
4243

uncompyle6/scanners/scanner37base.py

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2015-2020, 2022-2023 by Rocky Bernstein
1+
# Copyright (c) 2015-2020, 2022-2024 by Rocky Bernstein
22
# Copyright (c) 2005 by Dan Pascu <[email protected]>
33
# Copyright (c) 2000-2002 by hartmut Goebel <[email protected]>
44
#
@@ -15,7 +15,7 @@
1515
# You should have received a copy of the GNU General Public License
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
"""
18-
Python 37 bytecode scanner/deparser base.
18+
Python 3.7 bytecode scanner/deparser base.
1919
2020
Also we *modify* the instruction sequence to assist deparsing code.
2121
For example:
@@ -29,20 +29,17 @@
2929
Finally we save token information.
3030
"""
3131

32+
import sys
3233
from typing import Any, Dict, List, Set, Tuple
3334

34-
from xdis import iscode, instruction_size, Instruction
35-
from xdis.bytecode import _get_const_info
36-
37-
from uncompyle6.scanner import Token
3835
import xdis
3936

4037
# Get all the opcodes into globals
4138
import xdis.opcodes.opcode_37 as op3
39+
from xdis import Instruction, instruction_size, iscode
40+
from xdis.bytecode import _get_const_info
4241

43-
from uncompyle6.scanner import Scanner
44-
45-
import sys
42+
from uncompyle6.scanner import Scanner, Token
4643

4744
globals().update(op3.opmap)
4845

@@ -51,7 +48,9 @@
5148

5249

5350
class Scanner37Base(Scanner):
54-
def __init__(self, version: Tuple[int, int], show_asm=None, debug="", is_pypy=False):
51+
def __init__(
52+
self, version: Tuple[int, int], show_asm=None, debug="", is_pypy=False
53+
):
5554
super(Scanner37Base, self).__init__(version, show_asm, is_pypy)
5655
self.offset2tok_index = None
5756
self.debug = debug
@@ -254,7 +253,6 @@ def tokens_append(j, token):
254253

255254
n = len(self.insts)
256255
for i, inst in enumerate(self.insts):
257-
258256
# We need to detect the difference between:
259257
# raise AssertionError
260258
# and
@@ -284,7 +282,6 @@ def tokens_append(j, token):
284282
# To simplify things we want to untangle this. We also
285283
# do this loop before we compute jump targets.
286284
for i, inst in enumerate(self.insts):
287-
288285
# One artifact of the "too-small" operand problem, is that
289286
# some backward jumps, are turned into forward jumps to another
290287
# "extended arg" backward jump to the same location.
@@ -321,16 +318,9 @@ def tokens_append(j, token):
321318

322319
j = 0
323320
for i, inst in enumerate(self.insts):
324-
325321
argval = inst.argval
326322
op = inst.opcode
327323

328-
if inst.opname == "EXTENDED_ARG":
329-
# FIXME: The EXTENDED_ARG is used to signal annotation
330-
# parameters
331-
if i + 1 < n and self.insts[i + 1].opcode != self.opc.MAKE_FUNCTION:
332-
continue
333-
334324
if inst.offset in jump_targets:
335325
jump_idx = 0
336326
# We want to process COME_FROMs to the same offset to be in *descending*

0 commit comments

Comments
 (0)