Sync with master

This commit is contained in:
rocky
2024-02-11 12:19:27 -05:00
parent ab1b81a8da
commit 70690f2ede
8 changed files with 90 additions and 88 deletions

View File

@@ -55,16 +55,16 @@ Options:
-V | --version show version and stop -V | --version show version and stop
-h | --help show this message -h | --help show this message
""" % ((program,) * 5) """ % (
(program,) * 5
)
PATTERNS = ("*.pyc", "*.pyo") PATTERNS = ("*.pyc", "*.pyo")
def main(): def main():
usage_short = ( usage_short = f"""usage: {program} FILE...
f"""usage: {program} FILE...
Type -h for for full help.""" Type -h for for full help."""
)
if len(sys.argv) == 1: if len(sys.argv) == 1:
sys.stderr.write("No file(s) given\n") sys.stderr.write("No file(s) given\n")
@@ -76,7 +76,7 @@ Type -h for for full help."""
sys.argv[1:], "hVU", ["help", "version", "uncompyle6"] sys.argv[1:], "hVU", ["help", "version", "uncompyle6"]
) )
except getopt.GetoptError(e): except getopt.GetoptError(e):
sys.stderr.write('%s: %s' % (os.path.basename(sys.argv[0]), e)) sys.stderr.write("%s: %s" % (os.path.basename(sys.argv[0]), e))
sys.exit(-1) sys.exit(-1)
for opt, val in opts: for opt, val in opts:

View File

@@ -6,22 +6,17 @@
# Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com> # Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com>
# #
import getopt
import os import os
import sys import sys
import time import time
from uncompyle6.verify import VerifyCmpError
from uncompyle6.main import main, status_msg from uncompyle6.main import main, status_msg
from uncompyle6.verify import VerifyCmpError
from uncompyle6.version import __version__ from uncompyle6.version import __version__
program = "uncompyle6" program = "uncompyle6"
def usage():
print(__doc__)
sys.exit(1)
__doc__ = """ __doc__ = """
Usage: Usage:
%s [OPTIONS]... [ FILE | DIR]... %s [OPTIONS]... [ FILE | DIR]...
@@ -74,18 +69,44 @@ Extensions of generated files:
(program,) * 5 (program,) * 5
) )
program = "uncompyle6"
def usage():
print(__doc__)
sys.exit(1)
def main_bin():
if not (
sys.version_info[0:2]
in (
(2, 4),
(2, 5),
(2, 6),
(2, 7),
(3, 0),
(3, 1),
(3, 2),
(3, 3),
(3, 4),
(3, 5),
(3, 6),
(3, 7),
(3, 8),
(3, 9),
(3, 10),
(3, 11),
)
): ):
print('Error: %s requires Python 2.4-3.10' % program) print('Error: %s requires Python 2.4-3.10' % program)
sys.exit(-1) sys.exit(-1)
recurse_dirs = False
numproc = 0 numproc = 0
out_base = None outfile = "-"
out_base = None out_base = None
source_paths = [] source_paths = []
timestamp = False timestamp = False
timestampfmt = "# %Y.%m.%d %H:%M:%S %Z" timestampfmt = "# %Y.%m.%d %H:%M:%S %Z"
pyc_paths = files
try: try:
opts, pyc_paths = getopt.getopt( opts, pyc_paths = getopt.getopt(
@@ -196,17 +217,10 @@ Extensions of generated files:
out_base = outfile out_base = outfile
outfile = None outfile = None
# A second -a turns show_asm="after" into show_asm="before"
if asm_plus or asm:
asm_opt = "both" if asm_plus else "after"
else:
asm_opt = None
if timestamp: if timestamp:
print(time.strftime(timestampfmt)) print(time.strftime(timestampfmt))
if numproc <= 1: if numproc <= 1:
show_ast = {"before": tree or tree_plus, "after": tree_plus}
try: try:
result = main( result = main(
src_base, src_base,
@@ -214,14 +228,15 @@ Extensions of generated files:
pyc_paths, pyc_paths,
source_paths, source_paths,
outfile, outfile,
showasm=asm_opt, showasm=options["showasm"],
showgrammar=show_grammar, showgrammar=options["showgrammar"],
showast=show_ast, showast=options["showast"],
do_verify=verify, do_verify=options["do_verify"],
do_linemaps=linemaps, do_linemaps=options["do_linemaps"],
start_offset=start_offset, start_offset=0,
stop_offset=stop_offset, stop_offset=-1,
) )
result = [options.get("do_verify", None)] + list(result)
if len(pyc_paths) > 1: if len(pyc_paths) > 1:
mess = status_msg(*result) mess = status_msg(*result)
print("# " + mess) print("# " + mess)

View File

@@ -13,7 +13,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import ast # import ast
import datetime import datetime
import os import os
import os.path as osp import os.path as osp
@@ -52,15 +52,19 @@ def _get_outstream(outfile):
pass pass
return open(outfile, 'wb') return open(outfile, 'wb')
def syntax_check(filename: str) -> bool: # def syntax_check(filename):
with open(filename) as f: # f = open(filename, "r")
source = f.read() # try:
valid = True # source = f.read()
try: # finally:
ast.parse(source) # f.close()
except SyntaxError: # valid = True
valid = False # try:
return valid # ast.parse(source)
# except SyntaxError:
# valid = False
# return valid
def decompile( def decompile(
@@ -74,13 +78,13 @@ def decompile(
source_encoding=None, source_encoding=None,
code_objects={}, code_objects={},
source_size=None, source_size=None,
is_pypy: bool = False, is_pypy=False,
magic_int=None, magic_int=None,
mapstream=None, mapstream=None,
do_fragments=False, do_fragments=False,
compile_mode="exec", compile_mode="exec",
start_offset: int = 0, start_offset=0,
stop_offset: int = -1, stop_offset=-1,
): ):
""" """
ingests and deparses a given code block 'co' ingests and deparses a given code block 'co'
@@ -215,7 +219,7 @@ def compile_file(source_path):
def decompile_file( def decompile_file(
filename: str, filename,
outstream=None, outstream=None,
showasm=None, showasm=None,
showast={}, showast={},
@@ -293,12 +297,12 @@ def main(
showasm=None, showasm=None,
showast={}, showast={},
do_verify=None, do_verify=None,
showgrammar: bool = False, showgrammar = False,
source_encoding=None, source_encoding=None,
do_linemaps=False, do_linemaps=False,
do_fragments=False, do_fragments=False,
start_offset: int = 0, start_offset=0,
stop_offset: int = -1, stop_offset=-1,
): ):
""" """
in_base base directory for input files in_base base directory for input files
@@ -312,7 +316,9 @@ def main(
- stdout out_base=None, outfile=None - stdout out_base=None, outfile=None
""" """
tot_files = okay_files = failed_files = 0 tot_files = okay_files = failed_files = 0
verify_failed_files = 0 if do_verify else 0
verify_failed_files = 0
current_outfile = outfile current_outfile = outfile
linemap_stream = None linemap_stream = None
@@ -423,7 +429,9 @@ def main(
print(result.stderr.decode()) print(result.stderr.decode())
else: else:
valid = syntax_check(deparsed_object.f.name) print("Syntax checking not supported before Python 3.0")
# valid = syntax_check(deparsed_object.f.name)
valid = True
if not valid: if not valid:
verify_failed_files += 1 verify_failed_files += 1

View File

@@ -869,29 +869,6 @@ class Python3Parser(PythonParser):
rule = "starred ::= %s %s" % ("expr " * v, opname) rule = "starred ::= %s %s" % ("expr " * v, opname)
self.addRule(rule, nop_func) self.addRule(rule, nop_func)
elif opname in ("BUILD_CONST_LIST", "BUILD_CONST_DICT", "BUILD_CONST_SET"):
if opname == "BUILD_CONST_DICT":
rule = (
"""
add_consts ::= ADD_VALUE*
const_list ::= COLLECTION_START add_consts %s
dict ::= const_list
expr ::= dict
"""
% opname
)
else:
rule = (
"""
add_consts ::= ADD_VALUE*
const_list ::= COLLECTION_START add_consts %s
expr ::= const_list
"""
% opname
)
self.addRule(rule, nop_func)
>>>>>>> python-3.0-to-3.2
elif opname_base in ( elif opname_base in (
"BUILD_LIST", "BUILD_LIST",
"BUILD_SET", "BUILD_SET",

View File

@@ -47,4 +47,6 @@ if __name__ == "__main__":
print(t.format()) print(t.format())
pass pass
else: else:
print("Need to be Python 3.3 to demo; I am version %s." % version_tuple_to_str()) print(
"Need to be Python 3.3 to demo; I am version %s." % version_tuple_to_str()
)

View File

@@ -2054,8 +2054,8 @@ def code_deparse(
code_objects={}, code_objects={},
compile_mode="exec", compile_mode="exec",
walker=FragmentsWalker, walker=FragmentsWalker,
start_offset: int = 0, start_offset=0,
stop_offset: int = -1, stop_offset=-1,
): ):
""" """
Convert the code object co into a python source fragment. Convert the code object co into a python source fragment.
@@ -2189,7 +2189,7 @@ def code_deparse_around_offset(
co, co,
out=StringIO(), out=StringIO(),
version=None, version=None,
is_pypy = False, is_pypy=False,
debug_opts=DEFAULT_DEBUG_OPTS, debug_opts=DEFAULT_DEBUG_OPTS,
): ):
""" """

View File

@@ -130,9 +130,9 @@ Python.
# evaluating the escape code. # evaluating the escape code.
import sys import sys
from io import StringIO
from spark_parser import GenericASTTraversal from spark_parser import GenericASTTraversal
from StringIO import StringIO
from xdis import COMPILER_FLAG_BIT, iscode from xdis import COMPILER_FLAG_BIT, iscode
from xdis.version_info import PYTHON_VERSION_TRIPLE from xdis.version_info import PYTHON_VERSION_TRIPLE
@@ -221,7 +221,7 @@ class SourceWalker(GenericASTTraversal, NonterminalActions, ComprehensionMixin):
def __init__( def __init__(
self, self,
version: tuple, version,
out, out,
scanner, scanner,
showast=TREE_DEFAULT_DEBUG, showast=TREE_DEFAULT_DEBUG,
@@ -397,7 +397,7 @@ class SourceWalker(GenericASTTraversal, NonterminalActions, ComprehensionMixin):
i += 1 i += 1
return rv return rv
def indent_if_source_nl(self, line_number, indent): def indent_if_source_nl(self, line_number, indent_spaces):
if line_number != self.line_number: if line_number != self.line_number:
self.write("\n" + indent_spaces + INDENT_PER_LEVEL[:-1]) self.write("\n" + indent_spaces + INDENT_PER_LEVEL[:-1])
return self.line_number return self.line_number
@@ -1206,7 +1206,7 @@ class SourceWalker(GenericASTTraversal, NonterminalActions, ComprehensionMixin):
is_lambda=False, is_lambda=False,
noneInNames=False, noneInNames=False,
is_top_level_module=False, is_top_level_module=False,
) -> GenericASTTraversal: ):
# FIXME: DRY with fragments.py # FIXME: DRY with fragments.py
# assert isinstance(tokens[0], Token) # assert isinstance(tokens[0], Token)
@@ -1294,8 +1294,8 @@ def code_deparse(
compile_mode="exec", compile_mode="exec",
is_pypy=IS_PYPY, is_pypy=IS_PYPY,
walker=SourceWalker, walker=SourceWalker,
start_offset: int = 0, start_offset = 0,
stop_offset: int = -1, stop_offset = -1,
): ):
""" """
ingests and deparses a given code block 'co'. If version is None, ingests and deparses a given code block 'co'. If version is None,
@@ -1452,9 +1452,9 @@ def deparse_code2str(
compile_mode="exec", compile_mode="exec",
is_pypy=IS_PYPY, is_pypy=IS_PYPY,
walker=SourceWalker, walker=SourceWalker,
start_offset: int = 0, start_offset=0,
stop_offset: int = -1, stop_offset=-1,
) -> str: ):
""" """
Return the deparsed text for a Python code object. `out` is where Return the deparsed text for a Python code object. `out` is where
any intermediate output for assembly or tree output will be sent. any intermediate output for assembly or tree output will be sent.

View File

@@ -56,7 +56,7 @@ def is_docstring(node, version, co_consts):
return node == ASSIGN_DOC_STRING(co_consts[0], doc_load) return node == ASSIGN_DOC_STRING(co_consts[0], doc_load)
def is_not_docstring(call_stmt_node) -> bool: def is_not_docstring(call_stmt_node):
try: try:
return ( return (
call_stmt_node == "call_stmt" call_stmt_node == "call_stmt"
@@ -70,7 +70,7 @@ def is_not_docstring(call_stmt_node) -> bool:
class TreeTransform(GenericASTTraversal, object): class TreeTransform(GenericASTTraversal, object):
def __init__( def __init__(
self, self,
version: tuple, version,
is_pypy=False, is_pypy=False,
show_ast=None, show_ast=None,
): ):
@@ -462,7 +462,7 @@ class TreeTransform(GenericASTTraversal, object):
node = self.preorder(node) node = self.preorder(node)
return node return node
def transform(self, parse_tree: GenericASTTraversal, code) -> GenericASTTraversal: def transform(self, parse_tree, code):
self.maybe_show_tree(parse_tree) self.maybe_show_tree(parse_tree)
self.ast = copy(parse_tree) self.ast = copy(parse_tree)
del parse_tree del parse_tree