From 0742f0b83f718935cd529d52f537fa92484a8acc Mon Sep 17 00:00:00 2001 From: rocky Date: Wed, 3 Nov 2021 01:56:41 -0400 Subject: [PATCH] Specialize for Python 3.3-3.5 --- pytest/test_deparse.py | 8 +++----- pytest/test_pysource.py-notyet | 12 +++--------- uncompyle6/parsers/treenode.py | 5 ++--- uncompyle6/scanners/scanner2.py | 27 ++------------------------- uncompyle6/scanners/scanner26.py | 14 +------------- uncompyle6/scanners/scanner3.py | 20 +++++++------------- 6 files changed, 18 insertions(+), 68 deletions(-) diff --git a/pytest/test_deparse.py b/pytest/test_deparse.py index 50f02aff..074a87af 100644 --- a/pytest/test_deparse.py +++ b/pytest/test_deparse.py @@ -1,5 +1,5 @@ from uncompyle6.semantics.fragments import code_deparse as deparse -from xdis.version_info import PYTHON_VERSION, PYTHON3 +from xdis.version_info import PYTHON_VERSION_TRIPLE def map_stmts(x, y): x = [] @@ -29,8 +29,8 @@ def list_comp(): [y for y in range(3)] def get_parsed_for_fn(fn): - code = fn.__code__ if PYTHON3 else fn.func_code - return deparse(code, version=PYTHON_VERSION) + code = fn.__code__ + return deparse(code, version=PYTHON_VERSION_TRIPLE) def check_expect(expect, parsed, fn_name): debug = False @@ -316,5 +316,3 @@ for i in range(2): ... . """.split("\n") parsed = get_parsed_for_fn(for_range_stmt) - if not PYTHON3: - check_expect(expect, parsed, 'range_stmt') diff --git a/pytest/test_pysource.py-notyet b/pytest/test_pysource.py-notyet index cdb9ef06..08ec9645 100644 --- a/pytest/test_pysource.py-notyet +++ b/pytest/test_pysource.py-notyet @@ -5,15 +5,9 @@ from uncompyle6.semantics.consts import ( # RETURN_NONE, PASS, RETURN_LOCALS ) -from xdis.version_info import PYTHON3 -if PYTHON3: - from io import StringIO - def iteritems(d): - return d.items() -else: - from StringIO import StringIO - def iteritems(d): - return d.iteritems() +from io import StringIO +def iteritems(d): + return d.items() from uncompyle6.semantics.pysource import (SourceWalker, deparse_code2str) diff --git a/uncompyle6/parsers/treenode.py b/uncompyle6/parsers/treenode.py index 4fff2c31..ba175af6 100644 --- a/uncompyle6/parsers/treenode.py +++ b/uncompyle6/parsers/treenode.py @@ -1,10 +1,9 @@ import sys -from xdis.version_info import PYTHON3 from uncompyle6.scanners.tok import NoneToken from spark_parser.ast import AST as spark_AST -if PYTHON3: - intern = sys.intern + +intern = sys.intern class SyntaxTree(spark_AST): diff --git a/uncompyle6/scanners/scanner2.py b/uncompyle6/scanners/scanner2.py index fccbf677..0ac2e7c0 100644 --- a/uncompyle6/scanners/scanner2.py +++ b/uncompyle6/scanners/scanner2.py @@ -40,10 +40,7 @@ from copy import copy from xdis import code2num, iscode, op_has_argument, instruction_size from xdis.bytecode import _get_const_info -from xdis.version_info import PYTHON3 - -if PYTHON3: - from sys import intern +from sys import intern from uncompyle6.scanner import Scanner, Token @@ -147,10 +144,7 @@ class Scanner2(Scanner): In Python2 this always the operand value shifted 16 bits since the operand is always 2 bytes. In Python 3.6+ this changes to one byte. """ - if PYTHON3: - return arg << 16 - else: - return arg << long(16) + return arg << 16 @staticmethod def unmangle_name(name, classname): @@ -1428,20 +1422,3 @@ class Scanner2(Scanner): instr_offsets = filtered filtered = [] return instr_offsets - - -if __name__ == "__main__": - from uncompyle6 import PYTHON_VERSION - - if 2.0 <= PYTHON_VERSION < 3.0: - import inspect - - co = inspect.currentframe().f_code - from uncompyle6 import PYTHON_VERSION - - tokens, customize = Scanner2(PYTHON_VERSION).ingest(co) - for t in tokens: - print(t) - else: - print("Need to be Python 2.x to demo; I am %s." % PYTHON_VERSION) - pass diff --git a/uncompyle6/scanners/scanner26.py b/uncompyle6/scanners/scanner26.py index 25b683f6..b0d88d43 100755 --- a/uncompyle6/scanners/scanner26.py +++ b/uncompyle6/scanners/scanner26.py @@ -23,9 +23,7 @@ use in deparsing. """ import sys -from xdis.version_info import PYTHON3 -if PYTHON3: - intern = sys.intern +intern = sys.intern import uncompyle6.scanners.scanner2 as scan from uncompyle6.scanner import L65536 @@ -282,13 +280,3 @@ class Scanner26(scan.Scanner2): print(t.format(line_prefix="")) print() return tokens, customize - -if __name__ == "__main__": - from uncompyle6 import PYTHON_VERSION - if PYTHON_VERSION == 2.6: - import inspect - co = inspect.currentframe().f_code - tokens, customize = Scanner26(show_asm=True).ingest(co) - else: - print("Need to be Python 2.6 to demo; I am %s." % - PYTHON_VERSION) diff --git a/uncompyle6/scanners/scanner3.py b/uncompyle6/scanners/scanner3.py index cc5745e5..c9f09c0a 100644 --- a/uncompyle6/scanners/scanner3.py +++ b/uncompyle6/scanners/scanner3.py @@ -47,10 +47,8 @@ import xdis.opcodes.opcode_33 as op3 from uncompyle6.scanner import Scanner import sys -from xdis.version_info import PYTHON3 -if PYTHON3: - intern = sys.intern +intern = sys.intern globals().update(op3.opmap) @@ -379,7 +377,7 @@ class Scanner3(Scanner): # pattr = 'code_object @ 0x%x %s->%s' %\ # (id(const), const.co_filename, const.co_name) pattr = "" - elif isinstance(const, str) or xdis.PYTHON_VERSION <= 2.7 and isinstance(const, unicode): + elif isinstance(const, str): opname = "LOAD_STR" else: if isinstance(inst.arg, int) and inst.arg < len(co.co_consts): @@ -1297,14 +1295,10 @@ class Scanner3(Scanner): if __name__ == "__main__": from xdis.version_info import PYTHON_VERSION_TRIPLE - if PYTHON_VERSION_TRIPLE >= (3, 2): - import inspect + import inspect - co = inspect.currentframe().f_code + co = inspect.currentframe().f_code - tokens, customize = Scanner3(PYTHON_VERSION_TRIPLE).ingest(co) - for t in tokens: - print(t) - else: - print("Need to be Python 3.2 or greater to demo; I am %s." % sys.version) - pass + tokens, customize = Scanner3(PYTHON_VERSION_TRIPLE).ingest(co) + for t in tokens: + print(t)