diff --git a/pytest/test_basic.py b/pytest/test_basic.py new file mode 100644 index 00000000..9517b510 --- /dev/null +++ b/pytest/test_basic.py @@ -0,0 +1,11 @@ +from uncompyle6.scanner import get_scanner +from uncompyle6.parser import get_python_parser + +def test_get_scanner(): + # See that we can retrieve a scanner using a full version number + assert get_scanner('2.7.13') + + +def test_get_parser(): + # See that we can retrieve a sparser using a full version number + assert get_python_parser('2.7.13') diff --git a/uncompyle6/parser.py b/uncompyle6/parser.py index 9ab80c95..2225945d 100644 --- a/uncompyle6/parser.py +++ b/uncompyle6/parser.py @@ -14,6 +14,9 @@ from xdis.code import iscode from spark_parser import GenericASTBuilder, DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG from uncompyle6.show import maybe_show_asm +# FIXME: put in xdis +from uncompyle6.scanner import version_str2float + class ParserError(Exception): def __init__(self, token, offset): @@ -605,7 +608,15 @@ def get_python_parser( explanation of the different modes. """ + # If version is a string, turn that into the corresponding float. + if isinstance(version, str): + version = version_str2float(version) + # FIXME: there has to be a better way... + # We could do this as a table lookup, but that would force us + # in import all of the parsers all of the time. Perhaps there is + # a lazy way of doing the import? + if version < 3.0: if version == 1.5: import uncompyle6.parsers.parse15 as parse15 @@ -758,6 +769,7 @@ def python_parser(version, co, out=sys.stdout, showasm=False, if __name__ == '__main__': def parse_test(co): from uncompyle6 import PYTHON_VERSION, IS_PYPY + ast = python_parser('2.7.13', co, showasm=True, is_pypy=True) ast = python_parser(PYTHON_VERSION, co, showasm=True, is_pypy=IS_PYPY) print(ast) return diff --git a/uncompyle6/scanner.py b/uncompyle6/scanner.py index f66a477a..bd62c944 100755 --- a/uncompyle6/scanner.py +++ b/uncompyle6/scanner.py @@ -257,7 +257,30 @@ class Scanner(object): def parse_fn_counts(argc): return ((argc & 0xFF), (argc >> 8) & 0xFF, (argc >> 16) & 0x7FFF) + +# FIXME: put in xdis +from xdis.magics import magics +def version_str2float(version): + if version in magics: + magic = magics[version] + for v, m in list(magics.items()): + if m == magic: + try: + return float(v) + except: + pass + pass + pass + raise RuntimeError("Can't find a valid Python version for version %s" + % version) + return + def get_scanner(version, is_pypy=False, show_asm=None): + + # If version is a string, turn that into the corresponding float. + if isinstance(version, str): + version = version_str2float(version) + # Pick up appropriate scanner if version in PYTHON_VERSIONS: v_str = "%s" % (int(version * 10)) @@ -284,5 +307,6 @@ def get_scanner(version, is_pypy=False, show_asm=None): if __name__ == "__main__": import inspect, uncompyle6 co = inspect.currentframe().f_code + scanner = get_scanner('2.7.13', True) scanner = get_scanner(uncompyle6.PYTHON_VERSION, IS_PYPY, True) tokens, customize = scanner.ingest(co, {})