Merge branch 'master' into python-3.3-to-3.5

This commit is contained in:
rocky
2024-02-04 12:25:41 -05:00
51 changed files with 403 additions and 337 deletions

View File

@@ -1,4 +1,4 @@
# Copyright (C) 2018-2023 Rocky Bernstein <rocky@gnu.org>
# Copyright (C) 2018-2024 Rocky Bernstein <rocky@gnu.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -34,12 +34,15 @@ from uncompyle6.version import __version__
def _get_outstream(outfile):
dir = os.path.dirname(outfile)
"""
Return an opened output file descriptor for ``outfile``.
"""
dir_name = os.path.dirname(outfile)
failed_file = outfile + "_failed"
if os.path.exists(failed_file):
os.remove(failed_file)
try:
os.makedirs(dir)
os.makedirs(dir_name)
except OSError:
pass
return open(outfile, mode="w", encoding="utf-8")
@@ -65,7 +68,7 @@ def decompile(
"""
ingests and deparses a given code block 'co'
if `bytecode_version` is None, use the current Python intepreter
if `bytecode_version` is None, use the current Python interpreter
version.
Caller is responsible for closing `out` and `mapstream`
@@ -86,7 +89,7 @@ def decompile(
run_pypy_str = "PyPy " if IS_PYPY else ""
sys_version_lines = sys.version.split("\n")
if source_encoding:
write("# -*- coding: %s -*-" % source_encoding)
write(f"# -*- coding: {source_encoding} -*-")
write(
"# uncompyle6 version %s\n"
"# %sPython bytecode version base %s%s\n# Decompiled from: %sPython %s"
@@ -100,9 +103,9 @@ def decompile(
)
)
if co.co_filename:
write("# Embedded file name: %s" % co.co_filename)
write(f"# Embedded file name: {co.co_filename}")
if timestamp:
write("# Compiled at: %s" % datetime.datetime.fromtimestamp(timestamp))
write(f"# Compiled at: {datetime.datetime.fromtimestamp(timestamp)}")
if source_size:
write("# Size of source mod 2**32: %d bytes" % source_size)
@@ -117,22 +120,22 @@ def decompile(
if isinstance(mapstream, str):
mapstream = _get_outstream(mapstream)
debug_opts = {"asm": showasm, "tree": showast, "grammar": grammar}
deparsed = deparse_code_with_map(
bytecode_version,
co,
out,
showasm,
showast,
showgrammar,
co=co,
out=out,
version=bytecode_version,
code_objects=code_objects,
is_pypy=is_pypy,
debug_opts=debug_opts,
)
header_count = 3 + len(sys_version_lines)
linemap = [
(line_no, deparsed.source_linemap[line_no] + header_count)
for line_no in sorted(deparsed.source_linemap.keys())
]
mapstream.write("\n\n# %s\n" % linemap)
mapstream.write(f"\n\n# {linemap}\n")
else:
if do_fragments:
deparse_fn = code_deparse_fragments
@@ -142,9 +145,9 @@ def decompile(
co,
out,
bytecode_version,
is_pypy=is_pypy,
debug_opts=debug_opts,
compile_mode=compile_mode,
is_pypy=is_pypy,
)
pass
return deparsed
@@ -160,11 +163,11 @@ def compile_file(source_path):
basename = source_path
if hasattr(sys, "pypy_version_info"):
bytecode_path = "%s-pypy%s.pyc" % (basename, version_tuple_to_str())
bytecode_path = f"{basename}-pypy{version_tuple_to_str()}.pyc"
else:
bytecode_path = "%s-%s.pyc" % (basename, version_tuple_to_str())
bytecode_path = f"{basename}-{version_tuple_to_str()}.pyc"
print("compiling %s to %s" % (source_path, bytecode_path))
print(f"compiling {source_path} to {bytecode_path}")
py_compile.compile(source_path, bytecode_path, "exec")
return bytecode_path
@@ -229,23 +232,20 @@ def decompile_file(
compile_mode="exec",
)
]
co = None
return deparsed
# FIXME: combine into an options parameter
def main(
in_base,
in_base: str,
out_base,
compiled_files,
source_files,
compiled_files: list,
source_files: list,
outfile=None,
showasm=None,
showast={},
do_verify=False,
showgrammar=False,
source_encoding=None,
raise_on_error=False,
do_linemaps=False,
do_fragments=False,
):
@@ -271,7 +271,7 @@ def main(
infile = os.path.join(in_base, filename)
# print("XXX", infile)
if not os.path.exists(infile):
sys.stderr.write("File '%s' doesn't exist. Skipped\n" % infile)
sys.stderr.write(f"File '{infile}' doesn't exist. Skipped\n")
continue
if do_linemaps:
@@ -319,13 +319,13 @@ def main(
):
if e[0] != last_mod:
line = "=" * len(e[0])
outstream.write("%s\n%s\n%s\n" % (line, e[0], line))
outstream.write(f"{line}\n{e[0]}\n{line}\n")
last_mod = e[0]
info = offsets[e]
extractInfo = d.extract_node_info(info)
outstream.write("%s" % info.node.format().strip() + "\n")
outstream.write(extractInfo.selectedLine + "\n")
outstream.write(extractInfo.markerLine + "\n\n")
extract_info = d.extract_node_info(info)
outstream.write(f"{info.node.format().strip()}" + "\n")
outstream.write(extract_info.selectedLine + "\n")
outstream.write(extract_info.markerLine + "\n\n")
pass
pass
tot_files += 1
@@ -345,15 +345,13 @@ def main(
sys.stdout.write("\n%s\n" % str(e))
if str(e).startswith("Unsupported Python"):
sys.stdout.write("\n")
sys.stderr.write(
"\n# Unsupported bytecode in file %s\n# %s\n" % (infile, e)
)
sys.stderr.write(f"\n# Unsupported bytecode in file {infile}\n# {e}\n")
else:
if outfile:
outstream.close()
os.remove(outfile)
sys.stdout.write("\n")
sys.stderr.write("\nLast file: %s " % (infile))
sys.stderr.write(f"\nLast file: {infile} ")
raise
# except:
@@ -373,7 +371,7 @@ def main(
okay_files += 1
if not current_outfile:
mess = "\n# okay decompiling"
# mem_usage = __memUsage()
# mem_usage = __mem_usage()
print(mess, infile)
if current_outfile:
sys.stdout.write(
@@ -381,7 +379,6 @@ def main(
% (
infile,
status_msg(
do_verify,
tot_files,
okay_files,
failed_files,
@@ -402,14 +399,14 @@ def main(
except Exception:
pass
pass
return (tot_files, okay_files, failed_files, verify_failed_files)
return tot_files, okay_files, failed_files, verify_failed_files
# ---- main ----
if sys.platform.startswith("linux") and os.uname()[2][:2] in ["2.", "3.", "4."]:
def __memUsage():
def __mem_sage():
mi = open("/proc/self/stat", "r")
mu = mi.readline().split()[22]
mi.close()
@@ -417,11 +414,11 @@ if sys.platform.startswith("linux") and os.uname()[2][:2] in ["2.", "3.", "4."]:
else:
def __memUsage():
def __mem_usage():
return ""
def status_msg(do_verify, tot_files, okay_files, failed_files, verify_failed_files):
def status_msg(tot_files, okay_files, failed_files, verify_failed_files):
if tot_files == 1:
if failed_files:
return "\n# decompile failed"