Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky committed Feb 3, 2024
1 parent 9839cfe commit db6c715
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 92 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.pyo
*_dis
*~
.mypy_cache
/.cache
/.eggs
/.hypothesis
Expand All @@ -10,7 +11,6 @@
/.pytest_cache
/.python-version
/.tox
.mypy_cache
/.venv*
/README
/__pkginfo__.pyc
Expand All @@ -20,6 +20,7 @@
/tmp
/uncompyle6.egg-info
/unpyc
/venv
ChangeLog
__pycache__
build
Expand Down
2 changes: 1 addition & 1 deletion test/test_pythonlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def file_matches(files, root, basenames, patterns):
print("Output directory: ", target_dir)
try:
_, _, failed_files, failed_verify = main(
src_dir, target_dir, files, [], do_verify=opts["do_verify"]
src_dir, target_dir, files, []
)
if failed_files != 0:
sys.exit(2)
Expand Down
44 changes: 21 additions & 23 deletions uncompyle6/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def write(s):
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"
Expand All @@ -104,9 +104,9 @@ def write(s):
)
)
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)

Expand All @@ -129,13 +129,14 @@ def write(s):
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
Expand Down Expand Up @@ -163,11 +164,11 @@ def compile_file(source_path: str) -> str:
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

Expand Down Expand Up @@ -232,7 +233,6 @@ def decompile_file(
compile_mode="exec",
)
]
co = None
return deparsed


Expand All @@ -245,7 +245,6 @@ def main(
outfile=None,
showasm: Optional[str] = None,
showast={},
do_verify=False,
showgrammar=False,
source_encoding=None,
raise_on_error=False,
Expand Down Expand Up @@ -274,7 +273,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:
Expand Down Expand Up @@ -322,13 +321,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
Expand All @@ -349,14 +348,14 @@ def main(
if str(e).startswith("Unsupported Python"):
sys.stdout.write("\n")
sys.stderr.write(
"\n# Unsupported bytecode in file %s\n# %s\n" % (infile, e)
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:
Expand All @@ -376,15 +375,14 @@ 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(
"%s -- %s\r"
% (
infile,
status_msg(
do_verify,
tot_files,
okay_files,
failed_files,
Expand All @@ -405,26 +403,26 @@ 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()
return int(mu) / 1000000

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"
Expand Down
14 changes: 9 additions & 5 deletions uncompyle6/semantics/linemap.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright (c) 2018, 2024 by Rocky Bernstein
#

# 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
# the Free Software Foundation, either version 3 of the License, or
Expand All @@ -13,7 +14,10 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from uncompyle6.semantics.fragments import FragmentsWalker, code_deparse as fragments_code_deparse
from uncompyle6.semantics.fragments import (
FragmentsWalker,
code_deparse as fragments_code_deparse,
)
from uncompyle6.semantics.pysource import SourceWalker, code_deparse


Expand All @@ -25,9 +29,9 @@ def __init__(self, *args, **kwargs):
self.current_line_number = 1

def write(self, *data):
"""Augment write routine to keep track of current line"""
"""Augment write routine to keep track of current line."""
for line in data:
## print("XXX write: '%s'" % l)
# print(f"XXX write: '{line}'")
for i in str(line):
if i == "\n":
self.current_line_number += 1
Expand All @@ -39,7 +43,7 @@ def write(self, *data):
# Note n_expr needs treatment too

def default(self, node):
"""Augment write default routine to record line number changes"""
"""Augment default-write routine to record line number changes."""
if hasattr(node, "linestart"):
if node.linestart:
self.source_linemap[self.current_line_number] = node.linestart
Expand Down Expand Up @@ -85,7 +89,7 @@ def code_deparse_with_fragments_and_map(*args, **kwargs):
if __name__ == "__main__":

def deparse_test(co):
"This is a docstring"
"""This is a docstring"""
deparsed = code_deparse_with_map(co)
a = 1
b = 2
Expand Down
Loading

0 comments on commit db6c715

Please sign in to comment.