Skip to content

Commit 227f494

Browse files
committed
Double -a option show asm before tokenization
1 parent 99f054e commit 227f494

File tree

5 files changed

+50
-23
lines changed

5 files changed

+50
-23
lines changed

uncompyle6/bin/uncompile.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
import sys
1212
import time
1313

14-
from xdis.version_info import version_tuple_to_str
14+
from uncompyle6 import verify
15+
from uncompyle6.main import main, status_msg
16+
from uncompyle6.version import __version__
1517

1618
program = "uncompyle6"
1719

@@ -69,10 +71,6 @@
6971

7072
program = "uncompyle6"
7173

72-
from uncompyle6 import verify
73-
from uncompyle6.main import main, status_msg
74-
from uncompyle6.version import __version__
75-
7674

7775
def usage():
7876
print(__doc__)
@@ -102,7 +100,9 @@ def main_bin():
102100
print("%s: %s" % (os.path.basename(sys.argv[0]), e), file=sys.stderr)
103101
sys.exit(-1)
104102

105-
options = {}
103+
options = {
104+
"showasm": None
105+
}
106106
for opt, val in opts:
107107
if opt in ("-h", "--help"):
108108
print(__doc__)
@@ -121,7 +121,10 @@ def main_bin():
121121
elif opt == "--linemaps":
122122
options["do_linemaps"] = True
123123
elif opt in ("--asm", "-a"):
124-
options["showasm"] = "after"
124+
if options["showasm"] == None:
125+
options["showasm"] = "after"
126+
else:
127+
options["showasm"] = "both"
125128
options["do_verify"] = None
126129
elif opt in ("--tree", "-t"):
127130
if "showast" not in options:
@@ -227,6 +230,8 @@ def main_bin():
227230

228231
rqueue = Queue(numproc)
229232

233+
tot_files = okay_files = failed_files = verify_failed_files = 0
234+
230235
def process_func():
231236
try:
232237
(tot_files, okay_files, failed_files, verify_failed_files) = (

uncompyle6/main.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import os
1818
import py_compile
1919
import sys
20-
from typing import Any, Tuple
20+
from typing import Any, Optional, Tuple
2121

2222
from xdis import iscode
2323
from xdis.load import load_module
@@ -50,7 +50,7 @@ def decompile(
5050
co,
5151
bytecode_version: Tuple[int] = PYTHON_VERSION_TRIPLE,
5252
out=sys.stdout,
53-
showasm=None,
53+
showasm: Optional[str]=None,
5454
showast={},
5555
timestamp=None,
5656
showgrammar=False,
@@ -107,14 +107,11 @@ def write(s):
107107
if source_size:
108108
write("# Size of source mod 2**32: %d bytes" % source_size)
109109

110-
# maybe a second -a will do before as well
111-
asm = "after" if showasm else None
112-
113110
grammar = dict(PARSER_DEFAULT_DEBUG)
114111
if showgrammar:
115112
grammar["reduce"] = True
116113

117-
debug_opts = {"asm": asm, "tree": showast, "grammar": grammar}
114+
debug_opts = {"asm": showasm, "tree": showast, "grammar": grammar}
118115

119116
try:
120117
if mapstream:
@@ -244,7 +241,7 @@ def main(
244241
compiled_files: list,
245242
source_files: list,
246243
outfile=None,
247-
showasm=None,
244+
showasm: Optional[str] = None,
248245
showast={},
249246
do_verify=False,
250247
showgrammar=False,

uncompyle6/scanners/scanner2.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,17 @@ def ingest(self, co, classname=None, code_objects={}, show_asm=None):
205205

206206
bytecode = self.build_instructions(co)
207207

208-
# show_asm = 'after'
209208
if show_asm in ("both", "before"):
210-
for instr in bytecode.get_instructions(co):
211-
print(instr.disassemble())
209+
print("\n# ---- before tokenization:")
210+
bytecode.disassemble_bytes(
211+
co.co_code,
212+
varnames=co.co_varnames,
213+
names=co.co_names,
214+
constants=co.co_consts,
215+
cells=bytecode._cell_names,
216+
linestarts=bytecode._linestarts,
217+
asm_format="extended",
218+
)
212219

213220
# list of tokens/instructions
214221
new_tokens = []
@@ -483,6 +490,7 @@ def ingest(self, co, classname=None, code_objects={}, show_asm=None):
483490
pass
484491

485492
if show_asm in ("both", "after"):
493+
print("\n# ---- after tokenization:")
486494
for t in new_tokens:
487495
print(t.format(line_prefix=""))
488496
print()

uncompyle6/scanners/scanner3.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,16 @@ def ingest(
414414

415415
# show_asm = 'both'
416416
if show_asm in ("both", "before"):
417-
for instr in bytecode.get_instructions(co):
418-
print(instr.disassemble())
417+
print("\n# ---- before tokenization:")
418+
bytecode.disassemble_bytes(
419+
co.co_code,
420+
varnames=co.co_varnames,
421+
names=co.co_names,
422+
constants=co.co_consts,
423+
cells=bytecode._cell_names,
424+
linestarts=bytecode._linestarts,
425+
asm_format="extended",
426+
)
419427

420428
# "customize" is in the process of going away here
421429
customize = {}
@@ -777,6 +785,7 @@ def ingest(
777785
pass
778786

779787
if show_asm in ("both", "after"):
788+
print("\n# ---- after tokenization:")
780789
for t in new_tokens:
781790
print(t.format(line_prefix=""))
782791
print()

uncompyle6/scanners/scanner37base.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2015-2020, 2022 by Rocky Bernstein
1+
# Copyright (c) 2015-2020, 2022-2023 by Rocky Bernstein
22
# Copyright (c) 2005 by Dan Pascu <dan@windowmaker.org>
33
# Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com>
44
#
@@ -219,10 +219,17 @@ def tokens_append(j, token):
219219

220220
bytecode = self.build_instructions(co)
221221

222-
# show_asm = 'both'
223222
if show_asm in ("both", "before"):
224-
for instr in bytecode.get_instructions(co):
225-
print(instr.disassemble(self.opc))
223+
print("\n# ---- before tokenization:")
224+
bytecode.disassemble_bytes(
225+
co.co_code,
226+
varnames=co.co_varnames,
227+
names=co.co_names,
228+
constants=co.co_consts,
229+
cells=bytecode._cell_names,
230+
linestarts=bytecode._linestarts,
231+
asm_format="extended",
232+
)
226233

227234
# "customize" is in the process of going away here
228235
customize = {}
@@ -525,6 +532,7 @@ def tokens_append(j, token):
525532
pass
526533

527534
if show_asm in ("both", "after"):
535+
print("\n# ---- after tokenization:")
528536
for t in tokens:
529537
print(t.format(line_prefix=""))
530538
print()

0 commit comments

Comments
 (0)