Skip to content

Commit ca04ae9

Browse files
authored
Merge pull request #483 from rocky/xdis-fix-woes
Xdis fix woes
2 parents 454fac4 + 2886d2b commit ca04ae9

File tree

6 files changed

+74
-84
lines changed

6 files changed

+74
-84
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/scanners/scanner3.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2015-2019, 2021-2023 by Rocky Bernstein
1+
# Copyright (c) 2015-2019, 2021-2024 by Rocky Bernstein
22
# Copyright (c) 2005 by Dan Pascu <[email protected]>
33
# Copyright (c) 2000-2002 by hartmut Goebel <[email protected]>
44
#
@@ -482,7 +482,6 @@ def ingest(
482482

483483
last_op_was_break = False
484484
new_tokens = []
485-
operand_value = 0
486485

487486
for i, inst in enumerate(self.insts):
488487
opname = inst.opname
@@ -534,11 +533,9 @@ def ingest(
534533
op = inst.opcode
535534

536535
if opname == "EXTENDED_ARG":
537-
if i + 1 < n:
538-
operand_value = argval << 16
539-
continue
540-
else:
541-
operand_value = 0
536+
# EXTEND_ARG adjustments to the operand value should have
537+
# already been accounted for in xdis instruction creation.
538+
continue
542539

543540
if inst.offset in jump_targets:
544541
jump_idx = 0
@@ -645,7 +642,7 @@ def ingest(
645642
attr = attr[:4] # remove last value: attr[5] == False
646643
else:
647644
pos_args, name_pair_args, annotate_args = parse_fn_counts_30_35(
648-
inst.argval + operand_value
645+
inst.argval
649646
)
650647

651648
pattr = f"{pos_args} positional, {name_pair_args} keyword only, {annotate_args} annotated"

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*

uncompyle6/semantics/make_function3.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2015-2021 by Rocky Bernstein
1+
# Copyright (c) 2015-2021, 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
@@ -16,18 +16,18 @@
1616
All the crazy things we have to do to handle Python functions in 3.0-3.5 or so.
1717
The saga of changes before and after is in other files.
1818
"""
19-
from xdis import iscode, code_has_star_arg, code_has_star_star_arg, CO_GENERATOR
20-
from uncompyle6.scanner import Code
21-
from uncompyle6.parsers.treenode import SyntaxTree
22-
from uncompyle6.semantics.parser_error import ParserError
19+
from xdis import CO_GENERATOR, code_has_star_arg, code_has_star_star_arg, iscode
20+
2321
from uncompyle6.parser import ParserError as ParserError2
22+
from uncompyle6.parsers.treenode import SyntaxTree
23+
from uncompyle6.scanner import Code
2424
from uncompyle6.semantics.helper import (
25-
print_docstring,
2625
find_all_globals,
2726
find_globals_and_nonlocals,
2827
find_none,
28+
print_docstring,
2929
)
30-
30+
from uncompyle6.semantics.parser_error import ParserError
3131
from uncompyle6.show import maybe_show_tree_param_default
3232

3333
# FIXME: DRY the below code...
@@ -42,8 +42,8 @@ def make_function3_annotate(
4242

4343
def build_param(ast, name, default):
4444
"""build parameters:
45-
- handle defaults
46-
- handle format tuple parameters
45+
- handle defaults
46+
- handle format tuple parameters
4747
"""
4848
if default:
4949
value = self.traverse(default, indent="")
@@ -300,7 +300,7 @@ def build_param(ast, name, default):
300300

301301
def make_function3(self, node, is_lambda, nested=1, code_node=None):
302302
"""Dump function definition, doc string, and function body in
303-
Python version 3.0 and above
303+
Python version 3.0 and above
304304
"""
305305

306306
# For Python 3.3, the evaluation stack in MAKE_FUNCTION is:
@@ -333,8 +333,8 @@ def make_function3(self, node, is_lambda, nested=1, code_node=None):
333333

334334
def build_param(ast, name, default, annotation=None):
335335
"""build parameters:
336-
- handle defaults
337-
- handle format tuple parameters
336+
- handle defaults
337+
- handle format tuple parameters
338338
"""
339339
value = self.traverse(default, indent="")
340340
maybe_show_tree_param_default(self.showast, name, value)
@@ -419,7 +419,6 @@ def build_param(ast, name, default, annotation=None):
419419
pass
420420

421421
if len(node) > 2 and (have_kwargs or node[lc_index].kind != "load_closure"):
422-
423422
# Find the index in "node" where the first default
424423
# parameter value is located. Note this is in contrast to
425424
# key-word arguments, pairs of (name, value), which appear after "*".
@@ -492,8 +491,6 @@ def build_param(ast, name, default, annotation=None):
492491
self.ERROR = p
493492
return
494493

495-
kw_pairs = 0
496-
497494
i = len(paramnames) - len(defparams)
498495

499496
# build parameters

0 commit comments

Comments
 (0)