From 8094f3bb12c4e864e361eea61f2351a5085ab8c8 Mon Sep 17 00:00:00 2001 From: rocky Date: Wed, 3 Nov 2021 03:00:43 -0400 Subject: [PATCH] Remove PYTHON3 --- pytest/test_grammar.py | 45 ++++++++++++++------------------ pytest/validate.py | 8 +----- test/test_pyenvlib.py | 24 +++++++---------- uncompyle6/parsers/parse3.py | 18 ++----------- uncompyle6/parsers/treenode.py | 4 +-- uncompyle6/scanner.py | 15 ++++------- uncompyle6/scanners/scanner27.py | 8 +++--- uncompyle6/semantics/helper.py | 5 +--- uncompyle6/semantics/pysource.py | 29 +++----------------- 9 files changed, 47 insertions(+), 109 deletions(-) diff --git a/pytest/test_grammar.py b/pytest/test_grammar.py index 406ad508..1b8b9fca 100644 --- a/pytest/test_grammar.py +++ b/pytest/test_grammar.py @@ -1,7 +1,7 @@ import re from uncompyle6.parser import get_python_parser, python_parser from uncompyle6.scanner import get_scanner -from xdis.version_info import PYTHON_VERSION_TRIPLE, PYTHON3, IS_PYPY +from xdis.version_info import PYTHON_VERSION_TRIPLE, IS_PYPY def test_grammar(): @@ -50,34 +50,29 @@ def test_grammar(): # NOTE: this may disappear expect_lhs.add("except_handler_else") - if PYTHON3: - expect_lhs.add("load_genexpr") + expect_lhs.add("load_genexpr") - unused_rhs = unused_rhs.union( - set( - """ - except_pop_except generator_exp - """.split() - ) + unused_rhs = unused_rhs.union( + set( + """ + except_pop_except generator_exp + """.split() ) - if PYTHON_VERSION_TRIPLE >= (3, 0): - if PYTHON_VERSION_TRIPLE < (3, 7): - expect_lhs.add("annotate_arg") - expect_lhs.add("annotate_tuple") - unused_rhs.add("mkfunc_annotate") + ) + if PYTHON_VERSION_TRIPLE < (3, 7): + expect_lhs.add("annotate_arg") + expect_lhs.add("annotate_tuple") + unused_rhs.add("mkfunc_annotate") - unused_rhs.add("dict_comp") - unused_rhs.add("classdefdeco1") - unused_rhs.add("tryelsestmtl") - if PYTHON_VERSION_TRIPLE >= (3, 5): - expect_right_recursive.add( - (("l_stmts", ("lastl_stmt", "come_froms", "l_stmts"))) - ) - pass - pass + unused_rhs.add("dict_comp") + unused_rhs.add("classdefdeco1") + unused_rhs.add("tryelsestmtl") + if PYTHON_VERSION_TRIPLE >= (3, 5): + expect_right_recursive.add( + (("l_stmts", ("lastl_stmt", "come_froms", "l_stmts"))) + ) pass - else: - expect_lhs.add("kwarg") + pass # FIXME if PYTHON_VERSION_TRIPLE < (3, 8): diff --git a/pytest/validate.py b/pytest/validate.py index 7ba3d527..f1dd0193 100644 --- a/pytest/validate.py +++ b/pytest/validate.py @@ -10,7 +10,7 @@ import functools # uncompyle6 / xdis from uncompyle6 import code_deparse -from xdis.version_info import PYTHON_VERSION_TRIPLE, PYTHON3, IS_PYPY +from xdis.version_info import PYTHON_VERSION_TRIPLE, IS_PYPY # TODO : I think we can get xdis to support the dis api (python 3 version) by doing something like this there from xdis import Bytecode, get_opcode @@ -19,12 +19,6 @@ opc = get_opcode(PYTHON_VERSION_TRIPLE, IS_PYPY) Bytecode = functools.partial(Bytecode, opc=opc) import six -if PYTHON3: - from io import StringIO -else: - from StringIO import StringIO - - def _dis_to_text(co): return Bytecode(co).dis() diff --git a/test/test_pyenvlib.py b/test/test_pyenvlib.py index bfac82c9..5005f1d2 100755 --- a/test/test_pyenvlib.py +++ b/test/test_pyenvlib.py @@ -25,7 +25,7 @@ from __future__ import print_function import os, time, re, shutil, sys from fnmatch import fnmatch -from uncompyle6 import main, PYTHON3 +from uncompyle6 import main import xdis.magics as magics # ----- configure this for your needs @@ -111,20 +111,16 @@ def do_tests( files = [] cwd = os.getcwd() os.chdir(src_dir) - 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) - ] - ) - pass + 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) + ] + ) pass - else: - os.path.walk(os.curdir, visitor, files) os.chdir(cwd) files.sort() diff --git a/uncompyle6/parsers/parse3.py b/uncompyle6/parsers/parse3.py index ca400273..bac6ae8e 100644 --- a/uncompyle6/parsers/parse3.py +++ b/uncompyle6/parsers/parse3.py @@ -43,7 +43,6 @@ from uncompyle6.parsers.reducecheck import ( ) from uncompyle6.parsers.treenode import SyntaxTree from spark_parser import DEFAULT_DEBUG as PARSER_DEFAULT_DEBUG -from xdis import PYTHON3 class Python3Parser(PythonParser): @@ -626,10 +625,7 @@ class Python3Parser(PythonParser): this has an effect on many rules. """ if self.version >= (3, 3): - if PYTHON3 or not self.is_pypy: - load_op = "LOAD_STR " - else: - load_op = "LOAD_CONST " + load_op = "LOAD_STR " new_rule = rule % ((load_op) * 1) else: @@ -1157,7 +1153,7 @@ class Python3Parser(PythonParser): ) elif self.version >= (3, 4): - if PYTHON3 or not self.is_pypy: + if not self.is_pypy: load_op = "LOAD_STR" else: load_op = "LOAD_CONST" @@ -1235,14 +1231,6 @@ class Python3Parser(PythonParser): opname, ) self.add_unique_rule(rule, opname, token.attr, customize) - if not PYTHON3 and self.is_pypy: - rule = "mkfunc ::= %s%s%s%s" % ( - "expr " * stack_count, - "load_closure " * closure, - "LOAD_CODE LOAD_CONST ", - opname, - ) - self.add_unique_rule(rule, opname, token.attr, customize) if has_get_iter_call_function1: rule_pat = ( @@ -1566,8 +1554,6 @@ class Python3Parser(PythonParser): self.check_reduce["iflaststmtl"] = "AST" self.check_reduce["or"] = "AST" self.check_reduce["testtrue"] = "tokens" - if not PYTHON3: - self.check_reduce["kwarg"] = "noAST" if self.version < (3, 6) and not self.is_pypy: # 3.6+ can remove a JUMP_FORWARD which messes up our testing here # Pypy we need to go over in better detail diff --git a/uncompyle6/parsers/treenode.py b/uncompyle6/parsers/treenode.py index 4fff2c31..8f856124 100644 --- a/uncompyle6/parsers/treenode.py +++ b/uncompyle6/parsers/treenode.py @@ -1,10 +1,8 @@ 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/scanner.py b/uncompyle6/scanner.py index 8287b802..7ac5e8e0 100755 --- a/uncompyle6/scanner.py +++ b/uncompyle6/scanner.py @@ -26,7 +26,7 @@ from collections import namedtuple import sys from uncompyle6.scanners.tok import Token -from xdis.version_info import IS_PYPY, PYTHON3, version_tuple_to_str +from xdis.version_info import IS_PYPY, version_tuple_to_str import xdis from xdis import ( Bytecode, @@ -77,16 +77,11 @@ CANONIC2VERSION["3.5.2"] = 3.5 # FIXME: DRY -if PYTHON3: - intern = sys.intern - L65536 = 65536 +intern = sys.intern +L65536 = 65536 - def long(num): - return num - - -else: - L65536 = long(65536) # NOQA +def long(num): + return num class Code(object): diff --git a/uncompyle6/scanners/scanner27.py b/uncompyle6/scanners/scanner27.py index 1ebf0dd1..37628043 100755 --- a/uncompyle6/scanners/scanner27.py +++ b/uncompyle6/scanners/scanner27.py @@ -9,12 +9,10 @@ grammar parsing. from uncompyle6.scanners.scanner2 import Scanner2 -from xdis.version_info import PYTHON3, version_tuple_to_str +from xdis.version_info import version_tuple_to_str +import sys -if PYTHON3: - import sys - - intern = sys.intern +intern = sys.intern # bytecode verification, verify(), uses JUMP_OPs from here from xdis.opcodes import opcode_27 diff --git a/uncompyle6/semantics/helper.py b/uncompyle6/semantics/helper.py index 43782231..47b17324 100644 --- a/uncompyle6/semantics/helper.py +++ b/uncompyle6/semantics/helper.py @@ -212,10 +212,7 @@ def strip_quotes(s): # if __name__ == '__main__': -# if PYTHON3: -# from io import StringIO -# else: -# from StringIO import StringIO +# from io import StringIO # class PrintFake(): # def __init__(self): # self.pending_newlines = 0 diff --git a/uncompyle6/semantics/pysource.py b/uncompyle6/semantics/pysource.py index 734967c8..ec5e5bfd 100644 --- a/uncompyle6/semantics/pysource.py +++ b/uncompyle6/semantics/pysource.py @@ -133,7 +133,6 @@ Python. import sys IS_PYPY = "__pypy__" in sys.builtin_module_names -PYTHON3 = sys.version_info >= (3, 0) from xdis import iscode, COMPILER_FLAG_BIT from xdis.version_info import PYTHON_VERSION_TRIPLE @@ -184,11 +183,8 @@ from uncompyle6.show import maybe_show_tree from uncompyle6.util import better_repr -if PYTHON3: - def unicode(x): return x - from io import StringIO -else: - from StringIO import StringIO +def unicode(x): return x +from io import StringIO class SourceWalkerError(Exception): @@ -437,10 +433,7 @@ class SourceWalker(GenericASTTraversal, object): def write(self, *data): if (len(data) == 0) or (len(data) == 1 and data[0] == ""): return - if not PYTHON3: - out = "".join((unicode(j) for j in data)) - else: - out = "".join((str(j) for j in data)) + out = "".join((str(j) for j in data)) n = 0 for i in out: if i == "\n": @@ -467,8 +460,6 @@ class SourceWalker(GenericASTTraversal, object): if self.pending_newlines: out = out[: -self.pending_newlines] - if isinstance(out, str) and not (PYTHON3 or self.FUTURE_UNICODE_LITERALS): - out = unicode(out, "utf-8") self.f.write(out) def println(self, *data): @@ -684,23 +675,11 @@ class SourceWalker(GenericASTTraversal, object): # strings are interpreted: # u'xxx' -> 'xxx' # xxx' -> b'xxx' - if not PYTHON3 and isinstance(data, unicode): - try: - data = str(data) - except UnicodeEncodeError: - # Have to keep data as it is: in Unicode. - pass - self.write(repr(data)) - elif isinstance(data, str): + if isinstance(data, str): self.write("b" + repr(data)) else: self.write(repr(data)) else: - if not PYTHON3: - try: - repr(data).encode("ascii") - except UnicodeEncodeError: - self.write("u") self.write(repr(data)) # LOAD_CONST is a terminal, so stop processing/recursing early self.prune()