python-uncompyle6/test/add-test.py

50 lines
1.4 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python
""" Trivial helper program to byte compile and uncompile the bytecode file.
"""
import os, sys, py_compile
2021-11-23 21:38:30 +00:00
from xdis.version_info import version_tuple_to_str, PYTHON_VERSION_TRIPLE
2021-11-24 20:12:53 +00:00
if len(sys.argv) < 2:
print("Usage: add-test.py [--run] *python-source*... [optimize-level]")
sys.exit(1)
assert 2 <= len(sys.argv) <= 4
version = sys.version[0:3]
vers = sys.version_info[:2]
if sys.argv[1] in ("--run", "-r"):
suffix = "_run"
assert len(sys.argv) >= 3
2019-04-10 11:22:43 +00:00
py_source = sys.argv[2:]
i = 2
2019-04-10 11:22:43 +00:00
else:
suffix = ""
2019-04-10 11:22:43 +00:00
py_source = sys.argv[1:]
i = 1
try:
optimize = int(sys.argv[-1])
2021-11-24 20:12:53 +00:00
assert sys.argv >= i + 2
py_source = sys.argv[i:-1]
2021-11-24 20:12:53 +00:00
i = 2
except:
optimize = 2
2019-04-10 11:22:43 +00:00
for path in py_source:
2016-07-08 18:56:57 +00:00
short = os.path.basename(path)
if short.endswith(".py"):
short = short[: -len(".py")]
if hasattr(sys, "pypy_version_info"):
2021-11-23 21:38:30 +00:00
version = version_tuple_to_str(end=2, delimiter="")
bytecode = "bytecode_pypy%s%s/%spy%s.pyc" % (version, suffix, short, version)
else:
2021-11-23 21:38:30 +00:00
version = version_tuple_to_str(end=2)
bytecode = "bytecode_%s%s/%s.pyc" % (version, suffix, short)
print("byte-compiling %s to %s" % (path, bytecode))
2021-11-24 20:12:53 +00:00
if PYTHON_VERSION_TRIPLE >= (3, 2):
2021-11-23 21:38:30 +00:00
py_compile.compile(path, bytecode, optimize=optimize)
else:
2021-11-23 21:38:30 +00:00
py_compile.compile(path, bytecode)
if PYTHON_VERSION_TRIPLE >= (2, 6):
os.system("../bin/uncompyle6 -a -t %s" % bytecode)