You've already forked python-uncompyle6
mirror of
https://github.com/rocky/python-uncompyle6.git
synced 2025-08-03 00:45:53 +08:00
Use black to reformat some files
This commit is contained in:
@@ -28,64 +28,77 @@ from fnmatch import fnmatch
|
||||
from uncompyle6 import main, PYTHON3
|
||||
import xdis.magics as magics
|
||||
|
||||
#----- configure this for your needs
|
||||
# ----- configure this for your needs
|
||||
|
||||
python_versions = [v for v in magics.python_versions if
|
||||
re.match('^[0-9.]+$', v)]
|
||||
python_versions = [v for v in magics.python_versions if re.match("^[0-9.]+$", v)]
|
||||
|
||||
# FIXME: we should remove Python versions that we don't support.
|
||||
# These include Jython, and Python bytecode changes pre release.
|
||||
|
||||
TEST_VERSIONS = (
|
||||
'pypy3-2.4.0', 'pypy-2.6.1',
|
||||
'pypy-5.0.1', 'pypy-5.3.1', 'pypy3.5-5.7.1-beta',
|
||||
'pypy3.5-5.9.0', 'pypy3.5-6.0.0',
|
||||
'native') + tuple(python_versions)
|
||||
"pypy3-2.4.0",
|
||||
"pypy-2.6.1",
|
||||
"pypy-5.0.1",
|
||||
"pypy-5.3.1",
|
||||
"pypy3.5-5.7.1-beta",
|
||||
"pypy3.5-5.9.0",
|
||||
"pypy3.5-6.0.0",
|
||||
"native",
|
||||
) + tuple(python_versions)
|
||||
|
||||
|
||||
target_base = '/tmp/py-dis/'
|
||||
lib_prefix = os.path.join(os.environ['HOME'], '.pyenv/versions')
|
||||
target_base = "/tmp/py-dis/"
|
||||
lib_prefix = os.path.join(os.environ["HOME"], ".pyenv/versions")
|
||||
|
||||
PYC = ('*.pyc', )
|
||||
PYO = ('*.pyo', )
|
||||
PYOC = ('*.pyc', '*.pyo')
|
||||
PYC = ("*.pyc",)
|
||||
PYO = ("*.pyo",)
|
||||
PYOC = ("*.pyc", "*.pyo")
|
||||
|
||||
#-----
|
||||
# -----
|
||||
|
||||
test_options = {
|
||||
# name: (src_basedir, pattern, output_base_suffix)
|
||||
'test': ('./test', PYOC, 'test'),
|
||||
'max=': 200,
|
||||
}
|
||||
"test": ("./test", PYOC, "test"),
|
||||
"max=": 200,
|
||||
}
|
||||
|
||||
for vers in TEST_VERSIONS:
|
||||
if vers.startswith('pypy'):
|
||||
if vers.startswith('pypy3.'):
|
||||
if vers.startswith("pypy"):
|
||||
if vers.startswith("pypy3."):
|
||||
short_vers = vers[4:6]
|
||||
else:
|
||||
short_vers = vers[0:-2]
|
||||
|
||||
test_options[vers] = (os.path.join(lib_prefix, vers, 'lib_pypy'),
|
||||
PYC, 'python-lib'+short_vers)
|
||||
test_options[vers] = (
|
||||
os.path.join(lib_prefix, vers, "lib_pypy"),
|
||||
PYC,
|
||||
"python-lib" + short_vers,
|
||||
)
|
||||
else:
|
||||
if vers == 'native':
|
||||
if vers == "native":
|
||||
short_vers = os.path.basename(sys.path[-1])
|
||||
test_options[vers] = (sys.path[-1],
|
||||
PYC, short_vers)
|
||||
test_options[vers] = (sys.path[-1], PYC, short_vers)
|
||||
else:
|
||||
short_vers = vers[:3]
|
||||
test_options[vers] = (os.path.join(lib_prefix, vers, 'lib', 'python'+short_vers),
|
||||
PYC, 'python-lib'+short_vers)
|
||||
test_options[vers] = (
|
||||
os.path.join(lib_prefix, vers, "lib", "python" + short_vers),
|
||||
PYC,
|
||||
"python-lib" + short_vers,
|
||||
)
|
||||
|
||||
def do_tests(src_dir, patterns, target_dir, start_with=None,
|
||||
do_verify=False, max_files=200):
|
||||
|
||||
def do_tests(
|
||||
src_dir, patterns, target_dir, start_with=None, do_verify=False, max_files=200
|
||||
):
|
||||
def visitor(files, dirname, names):
|
||||
files.extend(
|
||||
[os.path.normpath(os.path.join(dirname, n))
|
||||
for n in names
|
||||
for pat in patterns
|
||||
if fnmatch(n, pat)])
|
||||
[
|
||||
os.path.normpath(os.path.join(dirname, n))
|
||||
for n in names
|
||||
for pat in patterns
|
||||
if fnmatch(n, pat)
|
||||
]
|
||||
)
|
||||
|
||||
files = []
|
||||
cwd = os.getcwd()
|
||||
@@ -93,10 +106,13 @@ def do_tests(src_dir, patterns, target_dir, start_with=None,
|
||||
if PYTHON3:
|
||||
for root, dirname, names in os.walk(os.curdir):
|
||||
files.extend(
|
||||
[os.path.normpath(os.path.join(root, n))
|
||||
for n in names
|
||||
for pat in patterns
|
||||
if fnmatch(n, pat)])
|
||||
[
|
||||
os.path.normpath(os.path.join(root, n))
|
||||
for n in names
|
||||
for pat in patterns
|
||||
if fnmatch(n, pat)
|
||||
]
|
||||
)
|
||||
pass
|
||||
pass
|
||||
else:
|
||||
@@ -108,26 +124,29 @@ def do_tests(src_dir, patterns, target_dir, start_with=None,
|
||||
try:
|
||||
start_with = files.index(start_with)
|
||||
files = files[start_with:]
|
||||
print('>>> starting with file', files[0])
|
||||
print(">>> starting with file", files[0])
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
if len(files) > max_files:
|
||||
files = [file for file in files if not 'site-packages' in file]
|
||||
files = [file for file in files if not 'test' in file]
|
||||
files = [file for file in files if not "site-packages" in file]
|
||||
files = [file for file in files if not "test" in file]
|
||||
if len(files) > max_files:
|
||||
# print("Number of files %d - truncating to last 200" % len(files))
|
||||
print("Number of files %d - truncating to first %s" %
|
||||
(len(files), max_files))
|
||||
print(
|
||||
"Number of files %d - truncating to first %s" % (len(files), max_files)
|
||||
)
|
||||
files = files[:max_files]
|
||||
|
||||
print(time.ctime())
|
||||
(tot_files, okay_files, failed_files,
|
||||
verify_failed_files) = main.main(src_dir, target_dir, files, [], do_verify=do_verify)
|
||||
(tot_files, okay_files, failed_files, verify_failed_files) = main.main(
|
||||
src_dir, target_dir, files, [], do_verify=do_verify
|
||||
)
|
||||
print(time.ctime())
|
||||
return verify_failed_files + failed_files
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
if __name__ == "__main__":
|
||||
import getopt, sys
|
||||
|
||||
do_coverage = do_verify = False
|
||||
@@ -136,38 +155,46 @@ if __name__ == '__main__':
|
||||
|
||||
test_options_keys = list(test_options.keys())
|
||||
test_options_keys.sort()
|
||||
opts, args = getopt.getopt(sys.argv[1:], '',
|
||||
['start-with=', 'verify', 'verify-run', 'syntax-verify',
|
||||
'max=', 'coverage', 'all', ] \
|
||||
+ test_options_keys )
|
||||
vers = ''
|
||||
opts, args = getopt.getopt(
|
||||
sys.argv[1:],
|
||||
"",
|
||||
[
|
||||
"start-with=",
|
||||
"verify",
|
||||
"verify-run",
|
||||
"syntax-verify",
|
||||
"max=",
|
||||
"coverage",
|
||||
"all",
|
||||
]
|
||||
+ test_options_keys,
|
||||
)
|
||||
vers = ""
|
||||
|
||||
for opt, val in opts:
|
||||
if opt == '--verify':
|
||||
do_verify = 'strong'
|
||||
elif opt == '--syntax-verify':
|
||||
do_verify = 'weak'
|
||||
elif opt == '--verify-run':
|
||||
do_verify = 'verify-run'
|
||||
elif opt == '--coverage':
|
||||
if opt == "--verify":
|
||||
do_verify = "strong"
|
||||
elif opt == "--syntax-verify":
|
||||
do_verify = "weak"
|
||||
elif opt == "--verify-run":
|
||||
do_verify = "verify-run"
|
||||
elif opt == "--coverage":
|
||||
do_coverage = True
|
||||
elif opt == '--start-with':
|
||||
elif opt == "--start-with":
|
||||
start_with = val
|
||||
elif opt[2:] in test_options_keys:
|
||||
triple = test_options[opt[2:]]
|
||||
vers = triple[-1]
|
||||
test_dirs.append(triple)
|
||||
elif opt == '--max':
|
||||
test_options['max='] = int(val)
|
||||
elif opt == '--all':
|
||||
vers = 'all'
|
||||
elif opt == "--max":
|
||||
test_options["max="] = int(val)
|
||||
elif opt == "--all":
|
||||
vers = "all"
|
||||
for val in test_options_keys:
|
||||
test_dirs.append(test_options[val])
|
||||
|
||||
if do_coverage:
|
||||
os.environ['SPARK_PARSER_COVERAGE'] = (
|
||||
'/tmp/spark-grammar-%s.cover' % vers
|
||||
)
|
||||
os.environ["SPARK_PARSER_COVERAGE"] = "/tmp/spark-grammar-%s.cover" % vers
|
||||
|
||||
failed = 0
|
||||
for src_dir, pattern, target_dir in test_dirs:
|
||||
@@ -175,8 +202,14 @@ if __name__ == '__main__':
|
||||
target_dir = os.path.join(target_base, target_dir)
|
||||
if os.path.exists(target_dir):
|
||||
shutil.rmtree(target_dir, ignore_errors=1)
|
||||
failed += do_tests(src_dir, pattern, target_dir, start_with,
|
||||
do_verify, test_options['max='])
|
||||
failed += do_tests(
|
||||
src_dir,
|
||||
pattern,
|
||||
target_dir,
|
||||
start_with,
|
||||
do_verify,
|
||||
test_options["max="],
|
||||
)
|
||||
else:
|
||||
print("### Path %s doesn't exist; skipping" % src_dir)
|
||||
pass
|
||||
|
@@ -35,69 +35,89 @@ from uncompyle6 import PYTHON_VERSION
|
||||
from uncompyle6.main import main
|
||||
from fnmatch import fnmatch
|
||||
|
||||
|
||||
def get_srcdir():
|
||||
filename = os.path.normcase(os.path.dirname(__file__))
|
||||
return os.path.realpath(filename)
|
||||
|
||||
|
||||
src_dir = get_srcdir()
|
||||
|
||||
|
||||
#----- configure this for your needs
|
||||
# ----- configure this for your needs
|
||||
|
||||
lib_prefix = '/usr/lib'
|
||||
#lib_prefix = [src_dir, '/usr/lib/', '/usr/local/lib/']
|
||||
lib_prefix = "/usr/lib"
|
||||
# lib_prefix = [src_dir, '/usr/lib/', '/usr/local/lib/']
|
||||
|
||||
target_base = tempfile.mkdtemp(prefix='py-dis-')
|
||||
target_base = tempfile.mkdtemp(prefix="py-dis-")
|
||||
|
||||
PY = ('*.py', )
|
||||
PYC = ('*.pyc', )
|
||||
PYO = ('*.pyo', )
|
||||
PYOC = ('*.pyc', '*.pyo')
|
||||
PY = ("*.py",)
|
||||
PYC = ("*.pyc",)
|
||||
PYO = ("*.pyo",)
|
||||
PYOC = ("*.pyc", "*.pyo")
|
||||
|
||||
test_options = {
|
||||
# name: (src_basedir, pattern, output_base_suffix, python_version)
|
||||
'test':
|
||||
('test', PYC, 'test'),
|
||||
|
||||
'ok-2.6': (os.path.join(src_dir, 'ok_lib2.6'),
|
||||
PYOC, 'ok-2.6', 2.6),
|
||||
|
||||
'ok-2.7': (os.path.join(src_dir, 'ok_lib2.7'),
|
||||
PYOC, 'ok-2.7', 2.7),
|
||||
|
||||
'ok-3.2': (os.path.join(src_dir, 'ok_lib3.2'),
|
||||
PYOC, 'ok-3.2', 3.2),
|
||||
|
||||
'base-2.7': (os.path.join(src_dir, 'base_tests', 'python2.7'),
|
||||
PYOC, 'base_2.7', 2.7),
|
||||
"test": ("test", PYC, "test"),
|
||||
"ok-2.6": (os.path.join(src_dir, "ok_lib2.6"), PYOC, "ok-2.6", 2.6),
|
||||
"ok-2.7": (os.path.join(src_dir, "ok_lib2.7"), PYOC, "ok-2.7", 2.7),
|
||||
"ok-3.2": (os.path.join(src_dir, "ok_lib3.2"), PYOC, "ok-3.2", 3.2),
|
||||
"base-2.7": (
|
||||
os.path.join(src_dir, "base_tests", "python2.7"),
|
||||
PYOC,
|
||||
"base_2.7",
|
||||
2.7,
|
||||
),
|
||||
}
|
||||
|
||||
for vers in (2.7, 3.4, 3.5, 3.6):
|
||||
for vers in (2.7, 3.4, 3.5, 3.6):
|
||||
pythonlib = "ok_lib%s" % vers
|
||||
key = "ok-%s" % vers
|
||||
test_options[key] = (os.path.join(src_dir, pythonlib), PYOC, key, vers)
|
||||
pass
|
||||
|
||||
for vers in (1.3, 1.4, 1.5,
|
||||
2.1, 2.2, 2.3, 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, 'pypy3.2', 'pypy2.7', 'pypy3.6'):
|
||||
for vers in (
|
||||
1.3,
|
||||
1.4,
|
||||
1.5,
|
||||
2.1,
|
||||
2.2,
|
||||
2.3,
|
||||
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,
|
||||
"pypy3.2",
|
||||
"pypy2.7",
|
||||
"pypy3.6",
|
||||
):
|
||||
bytecode = "bytecode_%s" % vers
|
||||
key = "bytecode-%s" % vers
|
||||
test_options[key] = (bytecode, PYC, bytecode, vers)
|
||||
test_options[key] = (bytecode, PYC, bytecode, vers)
|
||||
bytecode = "bytecode_%s_run" % vers
|
||||
key = "bytecode-%s-run" % vers
|
||||
test_options[key] = (bytecode, PYC, bytecode, vers)
|
||||
test_options[key] = (bytecode, PYC, bytecode, vers)
|
||||
key = "%s" % vers
|
||||
pythonlib = "python%s" % vers
|
||||
if isinstance(vers, float) and vers >= 3.0:
|
||||
pythonlib = os.path.join(pythonlib, '__pycache__')
|
||||
test_options[key] = (os.path.join(lib_prefix, pythonlib), PYOC, pythonlib, vers)
|
||||
pythonlib = os.path.join(pythonlib, "__pycache__")
|
||||
test_options[key] = (os.path.join(lib_prefix, pythonlib), PYOC, pythonlib, vers)
|
||||
|
||||
# -----
|
||||
|
||||
#-----
|
||||
|
||||
def help():
|
||||
print("""Usage-Examples:
|
||||
print(
|
||||
"""Usage-Examples:
|
||||
|
||||
# compile, decompyle and verify short tests for Python 2.7:
|
||||
test_pythonlib.py --bytecode-2.7 --verify --compile
|
||||
@@ -107,18 +127,21 @@ def help():
|
||||
|
||||
# decompile and verify known good python 2.7
|
||||
test_pythonlib.py --ok-2.7 --verify
|
||||
""")
|
||||
"""
|
||||
)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
def do_tests(src_dir, obj_patterns, target_dir, opts):
|
||||
|
||||
def file_matches(files, root, basenames, patterns):
|
||||
files.extend(
|
||||
[os.path.normpath(os.path.join(root, n))
|
||||
for n in basenames
|
||||
for pat in patterns
|
||||
if fnmatch(n, pat)])
|
||||
[
|
||||
os.path.normpath(os.path.join(root, n))
|
||||
for n in basenames
|
||||
for pat in patterns
|
||||
if fnmatch(n, pat)
|
||||
]
|
||||
)
|
||||
|
||||
files = []
|
||||
# Change directories so use relative rather than
|
||||
@@ -127,11 +150,14 @@ def do_tests(src_dir, obj_patterns, target_dir, opts):
|
||||
cwd = os.getcwd()
|
||||
os.chdir(src_dir)
|
||||
|
||||
if opts['do_compile']:
|
||||
compiled_version = opts['compiled_version']
|
||||
if opts["do_compile"]:
|
||||
compiled_version = opts["compiled_version"]
|
||||
if compiled_version and PYTHON_VERSION != compiled_version:
|
||||
print("Not compiling: desired Python version is %s but we are running %s" %
|
||||
(compiled_version, PYTHON_VERSION), file=sys.stderr)
|
||||
print(
|
||||
"Not compiling: desired Python version is %s but we are running %s"
|
||||
% (compiled_version, PYTHON_VERSION),
|
||||
file=sys.stderr,
|
||||
)
|
||||
else:
|
||||
for root, dirs, basenames in os.walk(src_dir):
|
||||
file_matches(files, root, basenames, PY)
|
||||
@@ -143,34 +169,36 @@ def do_tests(src_dir, obj_patterns, target_dir, opts):
|
||||
pass
|
||||
pass
|
||||
|
||||
for root, dirs, basenames in os.walk('.'):
|
||||
for root, dirs, basenames in os.walk("."):
|
||||
# Turn root into a relative path
|
||||
dirname = root[2:] # 2 = len('.') + 1
|
||||
file_matches(files, dirname, basenames, obj_patterns)
|
||||
|
||||
if not files:
|
||||
print("Didn't come up with any files to test! Try with --compile?",
|
||||
file=sys.stderr)
|
||||
print(
|
||||
"Didn't come up with any files to test! Try with --compile?",
|
||||
file=sys.stderr,
|
||||
)
|
||||
exit(1)
|
||||
|
||||
os.chdir(cwd)
|
||||
files.sort()
|
||||
|
||||
if opts['start_with']:
|
||||
if opts["start_with"]:
|
||||
try:
|
||||
start_with = files.index(opts['start_with'])
|
||||
start_with = files.index(opts["start_with"])
|
||||
files = files[start_with:]
|
||||
print('>>> starting with file', files[0])
|
||||
print(">>> starting with file", files[0])
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
print(time.ctime())
|
||||
print('Source directory: ', src_dir)
|
||||
print('Output directory: ', target_dir)
|
||||
print("Source directory: ", src_dir)
|
||||
print("Output directory: ", target_dir)
|
||||
try:
|
||||
_, _, failed_files, failed_verify = \
|
||||
main(src_dir, target_dir, files, [],
|
||||
do_verify=opts['do_verify'])
|
||||
_, _, failed_files, failed_verify = main(
|
||||
src_dir, target_dir, files, [], do_verify=opts["do_verify"]
|
||||
)
|
||||
if failed_files != 0:
|
||||
sys.exit(2)
|
||||
elif failed_verify != 0:
|
||||
@@ -179,71 +207,81 @@ def do_tests(src_dir, obj_patterns, target_dir, opts):
|
||||
except (KeyboardInterrupt, OSError):
|
||||
print()
|
||||
sys.exit(1)
|
||||
if test_opts['rmtree']:
|
||||
if test_opts["rmtree"]:
|
||||
parent_dir = os.path.dirname(target_dir)
|
||||
print("Everything good, removing %s" % parent_dir)
|
||||
shutil.rmtree(parent_dir)
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_dirs = []
|
||||
checked_dirs = []
|
||||
start_with = None
|
||||
|
||||
test_options_keys = list(test_options.keys())
|
||||
test_options_keys.sort()
|
||||
opts, args = getopt.getopt(sys.argv[1:], '',
|
||||
['start-with=', 'verify', 'verify-run',
|
||||
'syntax-verify', 'all',
|
||||
'compile', 'coverage',
|
||||
'no-rm'] \
|
||||
+ test_options_keys )
|
||||
if not opts: help()
|
||||
opts, args = getopt.getopt(
|
||||
sys.argv[1:],
|
||||
"",
|
||||
[
|
||||
"start-with=",
|
||||
"verify",
|
||||
"verify-run",
|
||||
"syntax-verify",
|
||||
"all",
|
||||
"compile",
|
||||
"coverage",
|
||||
"no-rm",
|
||||
]
|
||||
+ test_options_keys,
|
||||
)
|
||||
if not opts:
|
||||
help()
|
||||
|
||||
test_opts = {
|
||||
'do_compile': False,
|
||||
'do_verify': False,
|
||||
'start_with': None,
|
||||
'rmtree' : True,
|
||||
'coverage' : False
|
||||
}
|
||||
"do_compile": False,
|
||||
"do_verify": False,
|
||||
"start_with": None,
|
||||
"rmtree": True,
|
||||
"coverage": False,
|
||||
}
|
||||
|
||||
for opt, val in opts:
|
||||
if opt == '--verify':
|
||||
test_opts['do_verify'] = 'strong'
|
||||
elif opt == '--syntax-verify':
|
||||
test_opts['do_verify'] = 'weak'
|
||||
elif opt == '--verify-run':
|
||||
test_opts['do_verify'] = 'verify-run'
|
||||
elif opt == '--compile':
|
||||
test_opts['do_compile'] = True
|
||||
elif opt == '--start-with':
|
||||
test_opts['start_with'] = val
|
||||
elif opt == '--no-rm':
|
||||
test_opts['rmtree'] = False
|
||||
if opt == "--verify":
|
||||
test_opts["do_verify"] = "strong"
|
||||
elif opt == "--syntax-verify":
|
||||
test_opts["do_verify"] = "weak"
|
||||
elif opt == "--verify-run":
|
||||
test_opts["do_verify"] = "verify-run"
|
||||
elif opt == "--compile":
|
||||
test_opts["do_compile"] = True
|
||||
elif opt == "--start-with":
|
||||
test_opts["start_with"] = val
|
||||
elif opt == "--no-rm":
|
||||
test_opts["rmtree"] = False
|
||||
elif opt[2:] in test_options_keys:
|
||||
test_dirs.append(test_options[opt[2:]])
|
||||
elif opt == '--all':
|
||||
elif opt == "--all":
|
||||
for val in test_options_keys:
|
||||
test_dirs.append(test_options[val])
|
||||
elif opt == '--coverage':
|
||||
test_opts['coverage'] = True
|
||||
elif opt == "--coverage":
|
||||
test_opts["coverage"] = True
|
||||
else:
|
||||
help()
|
||||
pass
|
||||
pass
|
||||
|
||||
if test_opts['coverage']:
|
||||
os.environ['SPARK_PARSER_COVERAGE'] = (
|
||||
'/tmp/spark-grammar-python-lib%s.cover' % test_dirs[0][-1]
|
||||
)
|
||||
if test_opts["coverage"]:
|
||||
os.environ["SPARK_PARSER_COVERAGE"] = (
|
||||
"/tmp/spark-grammar-python-lib%s.cover" % test_dirs[0][-1]
|
||||
)
|
||||
|
||||
last_compile_version = None
|
||||
for src_dir, pattern, target_dir, compiled_version in test_dirs:
|
||||
if os.path.isdir(src_dir):
|
||||
checked_dirs.append([src_dir, pattern, target_dir])
|
||||
else:
|
||||
print("Can't find directory %s. Skipping" % src_dir,
|
||||
file=sys.stderr)
|
||||
print("Can't find directory %s. Skipping" % src_dir, file=sys.stderr)
|
||||
continue
|
||||
last_compile_version = compiled_version
|
||||
pass
|
||||
@@ -252,7 +290,7 @@ if __name__ == '__main__':
|
||||
print("No directories found to check", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
test_opts['compiled_version'] = last_compile_version
|
||||
test_opts["compiled_version"] = last_compile_version
|
||||
|
||||
for src_dir, pattern, target_dir in checked_dirs:
|
||||
target_dir = os.path.join(target_base, target_dir)
|
||||
|
@@ -16,122 +16,152 @@
|
||||
"""Isolate Python version-specific semantic actions here.
|
||||
"""
|
||||
|
||||
from uncompyle6.semantics.consts import (
|
||||
TABLE_R, TABLE_DIRECT)
|
||||
from uncompyle6.semantics.consts import TABLE_R, TABLE_DIRECT
|
||||
|
||||
from uncompyle6.parsers.treenode import SyntaxTree
|
||||
from uncompyle6.scanners.tok import Token
|
||||
|
||||
|
||||
def customize_for_version(self, is_pypy, version):
|
||||
if is_pypy:
|
||||
########################
|
||||
# PyPy changes
|
||||
#######################
|
||||
TABLE_DIRECT.update({
|
||||
'assert_pypy': ( '%|assert %c\n' , 1 ),
|
||||
'assert2_pypy': ( '%|assert %c, %c\n' , 1, 4 ),
|
||||
'try_except_pypy': ( '%|try:\n%+%c%-%c\n\n', 1, 2 ),
|
||||
'tryfinallystmt_pypy': ( '%|try:\n%+%c%-%|finally:\n%+%c%-\n\n', 1, 3 ),
|
||||
'assign3_pypy': ( '%|%c, %c, %c = %c, %c, %c\n', 5, 4, 3, 0, 1, 2 ),
|
||||
'assign2_pypy': ( '%|%c, %c = %c, %c\n', 3, 2, 0, 1),
|
||||
})
|
||||
TABLE_DIRECT.update(
|
||||
{
|
||||
"assert2_pypy": ("%|assert %c, %c\n", (1, "assert_expr"), 4),
|
||||
"assert_pypy": ("%|assert %c\n", (1, "assert_expr")),
|
||||
"assign2_pypy": ("%|%c, %c = %c, %c\n", 3, 2, 0, 1),
|
||||
"assign3_pypy": ("%|%c, %c, %c = %c, %c, %c\n", 5, 4, 3, 0, 1, 2),
|
||||
"try_except_pypy": ("%|try:\n%+%c%-%c\n\n", 1, 2),
|
||||
"tryfinallystmt_pypy": ("%|try:\n%+%c%-%|finally:\n%+%c%-\n\n", 1, 3),
|
||||
}
|
||||
)
|
||||
else:
|
||||
########################
|
||||
# Without PyPy
|
||||
#######################
|
||||
TABLE_DIRECT.update({
|
||||
'assert': ( '%|assert %c\n' , 0 ),
|
||||
'assert2': ( '%|assert %c, %c\n' , 0, 3 ),
|
||||
'try_except': ( '%|try:\n%+%c%-%c\n\n', 1, 3 ),
|
||||
'assign2': ( '%|%c, %c = %c, %c\n',
|
||||
3, 4, 0, 1 ),
|
||||
'assign3': ( '%|%c, %c, %c = %c, %c, %c\n',
|
||||
5, 6, 7, 0, 1, 2 ),
|
||||
})
|
||||
if version >= 3.0:
|
||||
TABLE_DIRECT.update(
|
||||
{
|
||||
"assert": ("%|assert %c\n", (0, "assert_expr")),
|
||||
"assert2": ("%|assert %c, %c\n", (0, "assert_expr"), 3),
|
||||
"assign2": ("%|%c, %c = %c, %c\n", 3, 4, 0, 1),
|
||||
"assign3": ("%|%c, %c, %c = %c, %c, %c\n", 5, 6, 7, 0, 1, 2),
|
||||
"try_except": ("%|try:\n%+%c%-%c\n\n", 1, 3),
|
||||
}
|
||||
)
|
||||
if version >= 3.0:
|
||||
if version >= 3.2:
|
||||
TABLE_DIRECT.update({
|
||||
'del_deref_stmt': ( '%|del %c\n', 0),
|
||||
'DELETE_DEREF': ( '%{pattr}', 0 ),
|
||||
})
|
||||
TABLE_DIRECT.update(
|
||||
{"del_deref_stmt": ("%|del %c\n", 0), "DELETE_DEREF": ("%{pattr}", 0)}
|
||||
)
|
||||
from uncompyle6.semantics.customize3 import customize_for_version3
|
||||
|
||||
customize_for_version3(self, version)
|
||||
else: # < 3.0
|
||||
TABLE_DIRECT.update({
|
||||
'except_cond3' : ( '%|except %c, %c:\n',
|
||||
(1, 'expr'), (-2, 'store') )
|
||||
})
|
||||
TABLE_DIRECT.update(
|
||||
{"except_cond3": ("%|except %c, %c:\n", (1, "expr"), (-2, "store"))}
|
||||
)
|
||||
if 2.4 <= version <= 2.6:
|
||||
TABLE_DIRECT.update({
|
||||
'comp_for': ( ' for %c in %c', 3, 1 ),
|
||||
})
|
||||
TABLE_DIRECT.update({"comp_for": (" for %c in %c", 3, 1)})
|
||||
else:
|
||||
TABLE_DIRECT.update({
|
||||
'comp_for': ( ' for %c in %c%c', 2, 0, 3 ),
|
||||
})
|
||||
TABLE_DIRECT.update({"comp_for": (" for %c in %c%c", 2, 0, 3)})
|
||||
|
||||
if version >= 2.5:
|
||||
from uncompyle6.semantics.customize25 import customize_for_version25
|
||||
|
||||
customize_for_version25(self, version)
|
||||
|
||||
if version >= 2.6:
|
||||
from uncompyle6.semantics.customize26_27 import customize_for_version26_27
|
||||
from uncompyle6.semantics.customize26_27 import (
|
||||
customize_for_version26_27,
|
||||
)
|
||||
|
||||
customize_for_version26_27(self, version)
|
||||
pass
|
||||
else: # < 2.5
|
||||
global NAME_MODULE
|
||||
NAME_MODULE = SyntaxTree('stmt',
|
||||
[ SyntaxTree('assign',
|
||||
[ SyntaxTree('expr',
|
||||
[Token('LOAD_GLOBAL', pattr='__name__',
|
||||
offset=0, has_arg=True)]),
|
||||
SyntaxTree('store',
|
||||
[ Token('STORE_NAME', pattr='__module__',
|
||||
offset=3, has_arg=True)])
|
||||
])])
|
||||
TABLE_DIRECT.update({
|
||||
'importmultiple': ( '%|import %c%c\n', 2, 3),
|
||||
'import_cont' : ( ', %c', 2),
|
||||
'tryfinallystmt': ( '%|try:\n%+%c%-%|finally:\n%+%c%-',
|
||||
(1, 'suite_stmts_opt') ,
|
||||
(5, 'suite_stmts_opt') )
|
||||
})
|
||||
NAME_MODULE = SyntaxTree(
|
||||
"stmt",
|
||||
[
|
||||
SyntaxTree(
|
||||
"assign",
|
||||
[
|
||||
SyntaxTree(
|
||||
"expr",
|
||||
[
|
||||
Token(
|
||||
"LOAD_GLOBAL",
|
||||
pattr="__name__",
|
||||
offset=0,
|
||||
has_arg=True,
|
||||
)
|
||||
],
|
||||
),
|
||||
SyntaxTree(
|
||||
"store",
|
||||
[
|
||||
Token(
|
||||
"STORE_NAME",
|
||||
pattr="__module__",
|
||||
offset=3,
|
||||
has_arg=True,
|
||||
)
|
||||
],
|
||||
),
|
||||
],
|
||||
)
|
||||
],
|
||||
)
|
||||
TABLE_DIRECT.update(
|
||||
{
|
||||
"importmultiple": ("%|import %c%c\n", 2, 3),
|
||||
"import_cont": (", %c", 2),
|
||||
"tryfinallystmt": (
|
||||
"%|try:\n%+%c%-%|finally:\n%+%c%-",
|
||||
(1, "suite_stmts_opt"),
|
||||
(5, "suite_stmts_opt"),
|
||||
),
|
||||
}
|
||||
)
|
||||
if version == 2.4:
|
||||
|
||||
def n_iftrue_stmt24(node):
|
||||
self.template_engine(('%c', 0), node)
|
||||
self.template_engine(("%c", 0), node)
|
||||
self.default(node)
|
||||
self.prune()
|
||||
|
||||
self.n_iftrue_stmt24 = n_iftrue_stmt24
|
||||
else: # version <= 2.3:
|
||||
TABLE_DIRECT.update({
|
||||
'if1_stmt': ( '%|if 1\n%+%c%-', 5 )
|
||||
})
|
||||
TABLE_DIRECT.update({"if1_stmt": ("%|if 1\n%+%c%-", 5)})
|
||||
if version <= 2.1:
|
||||
TABLE_DIRECT.update({
|
||||
'importmultiple': ( '%c', 2 ),
|
||||
# FIXME: not quite right. We have indiividual imports
|
||||
# when there is in fact one: "import a, b, ..."
|
||||
'imports_cont': ( '%C%,', (1, 100, '\n') ),
|
||||
})
|
||||
TABLE_DIRECT.update(
|
||||
{
|
||||
"importmultiple": ("%c", 2),
|
||||
# FIXME: not quite right. We have indiividual imports
|
||||
# when there is in fact one: "import a, b, ..."
|
||||
"imports_cont": ("%C%,", (1, 100, "\n")),
|
||||
}
|
||||
)
|
||||
pass
|
||||
pass
|
||||
pass # < 2.5
|
||||
pass # < 2.5
|
||||
|
||||
# < 3.0 continues
|
||||
|
||||
TABLE_R.update({
|
||||
'STORE_SLICE+0': ( '%c[:]', 0 ),
|
||||
'STORE_SLICE+1': ( '%c[%p:]', 0, (1, -1) ),
|
||||
'STORE_SLICE+2': ( '%c[:%p]', 0, (1, -1) ),
|
||||
'STORE_SLICE+3': ( '%c[%p:%p]', 0, (1, -1), (2, -1) ),
|
||||
'DELETE_SLICE+0': ( '%|del %c[:]\n', 0 ),
|
||||
'DELETE_SLICE+1': ( '%|del %c[%c:]\n', 0, 1 ),
|
||||
'DELETE_SLICE+2': ( '%|del %c[:%c]\n', 0, 1 ),
|
||||
'DELETE_SLICE+3': ( '%|del %c[%c:%c]\n', 0, 1, 2 ),
|
||||
})
|
||||
TABLE_DIRECT.update({
|
||||
'raise_stmt2': ( '%|raise %c, %c\n', 0, 1),
|
||||
})
|
||||
TABLE_R.update(
|
||||
{
|
||||
"STORE_SLICE+0": ("%c[:]", 0),
|
||||
"STORE_SLICE+1": ("%c[%p:]", 0, (1, -1)),
|
||||
"STORE_SLICE+2": ("%c[:%p]", 0, (1, -1)),
|
||||
"STORE_SLICE+3": ("%c[%p:%p]", 0, (1, -1), (2, -1)),
|
||||
"DELETE_SLICE+0": ("%|del %c[:]\n", 0),
|
||||
"DELETE_SLICE+1": ("%|del %c[%c:]\n", 0, 1),
|
||||
"DELETE_SLICE+2": ("%|del %c[:%c]\n", 0, 1),
|
||||
"DELETE_SLICE+3": ("%|del %c[%c:%c]\n", 0, 1, 2),
|
||||
}
|
||||
)
|
||||
TABLE_DIRECT.update({"raise_stmt2": ("%|raise %c, %c\n", 0, 1)})
|
||||
|
||||
# exec as a built-in statement is only in Python 2.x
|
||||
def n_exec_stmt(node):
|
||||
@@ -139,17 +169,19 @@ def customize_for_version(self, is_pypy, version):
|
||||
exec_stmt ::= expr exprlist DUP_TOP EXEC_STMT
|
||||
exec_stmt ::= expr exprlist EXEC_STMT
|
||||
"""
|
||||
self.write(self.indent, 'exec ')
|
||||
self.write(self.indent, "exec ")
|
||||
self.preorder(node[0])
|
||||
if not node[1][0].isNone():
|
||||
sep = ' in '
|
||||
sep = " in "
|
||||
for subnode in node[1]:
|
||||
self.write(sep); sep = ", "
|
||||
self.write(sep)
|
||||
sep = ", "
|
||||
self.preorder(subnode)
|
||||
self.println()
|
||||
self.prune() # stop recursing
|
||||
self.prune() # stop recursing
|
||||
|
||||
self.n_exec_smt = n_exec_stmt
|
||||
|
||||
pass # < 3.0
|
||||
pass # < 3.0
|
||||
|
||||
return
|
||||
|
Reference in New Issue
Block a user