From 394faec5f14c2a933f172ce13935f3f2ad2b7101 Mon Sep 17 00:00:00 2001 From: Mats Wichmann Date: Sun, 11 Oct 2020 07:02:52 -0600 Subject: Drop some more Py2 compat things Change exception type in a a couple of try block to what could go wrong, Py3 would not raise UniCodeDecodeError for these cases One try-import of StringIO module sconsign does not need a decode that was claimed as compat hack Remove some sys.version_info checks Use more modern way to get Python details in test frawmework AddMethod updated and RenameFunction dropped - it had become a one-liner and had no clients other than AddMethod (never exposed as public) Signed-off-by: Mats Wichmann --- CHANGES.txt | 1 + SCons/Node/FS.py | 2 +- SCons/Node/Python.py | 13 +++++-------- SCons/Script/__init__.py | 6 +----- SCons/SubstTests.py | 4 +--- SCons/Tool/clang.py | 3 +-- SCons/Tool/clangxx.py | 3 +-- SCons/Tool/textfile.py | 4 ++-- SCons/UtilTests.py | 11 +---------- SCons/Utilities/sconsign.py | 9 ++------- testing/framework/TestRuntest.py | 2 -- testing/framework/TestSCons.py | 7 ++----- 12 files changed, 18 insertions(+), 47 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index ae754ef..48ceb57 100755 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -63,6 +63,7 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER - Make sure cProfile is used if profiling - SCons was expecting the Util module to monkeypatch in cProfile as profile if available, but this is no longer being done. + - Some Python 2 compat dropped From Simon Tegelid - Fix using TEMPFILE in multiple actions in an action list. Previously a builder, or command diff --git a/SCons/Node/FS.py b/SCons/Node/FS.py index 3989a64..967f007 100644 --- a/SCons/Node/FS.py +++ b/SCons/Node/FS.py @@ -434,7 +434,7 @@ class EntryProxy(SCons.Util.Proxy): # In PY3 if a class defines __eq__, then it must explicitly provide # __hash__. Since SCons.Util.Proxy provides __eq__ we need the following - # see: https://docs.python.org/3.1/reference/datamodel.html#object.__hash__ + # see: https://docs.python.org/3/reference/datamodel.html#object.__hash__ __hash__ = SCons.Util.Delegate('__hash__') def __get_abspath(self): diff --git a/SCons/Node/Python.py b/SCons/Node/Python.py index a124c03..95fd274 100644 --- a/SCons/Node/Python.py +++ b/SCons/Node/Python.py @@ -129,7 +129,7 @@ class Value(SCons.Node.Node): self.built_value = self.value return self.built_value - def get_text_contents(self): + def get_text_contents(self) -> str: """By the assumption that the node.built_value is a deterministic product of the sources, the contents of a Value are the concatenation of all the contents of its sources. As @@ -141,16 +141,13 @@ class Value(SCons.Node.Node): contents = contents + kid.get_contents().decode() return contents - def get_contents(self): - """ - Get contents for signature calculations. - :return: bytes - """ + def get_contents(self) -> bytes: + """Get contents for signature calculations.""" text_contents = self.get_text_contents() try: return text_contents.encode() - except UnicodeDecodeError: - # Already encoded as python2 str are bytes + except AttributeError: + # Should not happen, as get_text_contents returns str return text_contents def changed_since_last_build(self, target, prev_ni): diff --git a/SCons/Script/__init__.py b/SCons/Script/__init__.py index 87e6f98..dff1567 100644 --- a/SCons/Script/__init__.py +++ b/SCons/Script/__init__.py @@ -36,11 +36,7 @@ start_time = time.time() import collections import os - -try: - from StringIO import StringIO -except ImportError: - from io import StringIO +from io import StringIO import sys diff --git a/SCons/SubstTests.py b/SCons/SubstTests.py index b6d91f0..8443cad 100644 --- a/SCons/SubstTests.py +++ b/SCons/SubstTests.py @@ -541,9 +541,7 @@ class scons_subst_TestCase(SubstTestCase): scons_subst('$foo.bar.3.0', env) except SCons.Errors.UserError as e: expect = [ - # Python 2.3, 2.4 - "SyntaxError `invalid syntax (line 1)' trying to evaluate `$foo.bar.3.0'", - # Python 2.5 + # Python 2.5 and later "SyntaxError `invalid syntax (, line 1)' trying to evaluate `$foo.bar.3.0'", ] assert str(e) in expect, e diff --git a/SCons/Tool/clang.py b/SCons/Tool/clang.py index 162daad..e39c742 100644 --- a/SCons/Tool/clang.py +++ b/SCons/Tool/clang.py @@ -84,8 +84,7 @@ def generate(env): # clang -dumpversion is of no use with pipe.stdout: line = pipe.stdout.readline() - if sys.version_info[0] > 2: - line = line.decode() + line = line.decode() match = re.search(r'clang +version +([0-9]+(?:\.[0-9]+)+)', line) if match: env['CCVERSION'] = match.group(1) diff --git a/SCons/Tool/clangxx.py b/SCons/Tool/clangxx.py index b1dc6f3..736d455 100644 --- a/SCons/Tool/clangxx.py +++ b/SCons/Tool/clangxx.py @@ -92,8 +92,7 @@ def generate(env): # clang -dumpversion is of no use with pipe.stdout: line = pipe.stdout.readline() - if sys.version_info[0] > 2: - line = line.decode() + line = line.decode() match = re.search(r'clang +version +([0-9]+(?:\.[0-9]+)+)', line) if match: env['CXXVERSION'] = match.group(1) diff --git a/SCons/Tool/textfile.py b/SCons/Tool/textfile.py index 10e07f9..c1b597f 100644 --- a/SCons/Tool/textfile.py +++ b/SCons/Tool/textfile.py @@ -74,8 +74,8 @@ def _do_subst(node, subs): if 'b' in TEXTFILE_FILE_WRITE_MODE: try: contents = bytearray(contents, 'utf-8') - except UnicodeDecodeError: - # contents is already utf-8 encoded python 2 str i.e. a byte array + except TypeError: + # TODO: this should not happen, get_text_contents returns text contents = bytearray(contents) return contents diff --git a/SCons/UtilTests.py b/SCons/UtilTests.py index 50c7ed3..72bbaf3 100644 --- a/SCons/UtilTests.py +++ b/SCons/UtilTests.py @@ -193,11 +193,7 @@ class UtilTestCase(unittest.TestCase): try: node, expect, withtags = self.tree_case_1() - if sys.version_info.major < 3: - IOStream = io.BytesIO - else: - IOStream = io.StringIO - + IOStream = io.StringIO sys.stdout = IOStream() print_tree(node, get_children) actual = sys.stdout.getvalue() @@ -249,11 +245,6 @@ class UtilTestCase(unittest.TestCase): def test_is_Dict(self): assert is_Dict({}) assert is_Dict(UserDict()) - - # os.environ is not a dictionary in python 3 - if sys.version_info < (3, 0): - assert is_Dict(os.environ) - try: class mydict(dict): pass diff --git a/SCons/Utilities/sconsign.py b/SCons/Utilities/sconsign.py index 8cde51b..3288779 100644 --- a/SCons/Utilities/sconsign.py +++ b/SCons/Utilities/sconsign.py @@ -98,11 +98,6 @@ def default_mapper(entry, name): val = eval("entry." + name) except AttributeError: val = None - if sys.version_info.major >= 3 and isinstance(val, bytes): - # This is a dirty hack for py 2/3 compatibility. csig is a bytes object - # in Python3 while Python2 bytes are str. Hence, we decode the csig to a - # Python3 string - val = val.decode() return str(val) @@ -327,8 +322,8 @@ class Do_SConsignDB: sys.stderr.write("sconsign: ignoring invalid `%s' file `%s': %s\n" % (self.dbm_name, fname, e)) exc_type, _, _ = sys.exc_info() - if exc_type.__name__ == "ValueError" and sys.version_info < (3,0,0): - sys.stderr.write("Python 2 only supports pickle protocols 0-2.\n") + if exc_type.__name__ == "ValueError": + sys.stderr.write("unrecognized pickle protocol.\n") return if Print_Directories: diff --git a/testing/framework/TestRuntest.py b/testing/framework/TestRuntest.py index 801f710..a70110c 100644 --- a/testing/framework/TestRuntest.py +++ b/testing/framework/TestRuntest.py @@ -36,8 +36,6 @@ else: pythonstring = python pythonstring = pythonstring.replace('\\', '\\\\') pythonflags = '' -if sys.version_info[0] < 3: - pythonflags = ' -tt' failing_test_template = """\ import sys diff --git a/testing/framework/TestSCons.py b/testing/framework/TestSCons.py index a7eb4c5..6ec63a9 100644 --- a/testing/framework/TestSCons.py +++ b/testing/framework/TestSCons.py @@ -1491,12 +1491,9 @@ SConscript(sconscript) self.run(program=python, stdin="""\ import sysconfig, sys, os.path py_ver = 'python%d%d' % sys.version_info[:2] -# use distutils to help find include and lib path -# TODO: PY3 fine to use sysconfig.get_config_var("INCLUDEPY") try: - import distutils.sysconfig - exec_prefix = distutils.sysconfig.EXEC_PREFIX - include = distutils.sysconfig.get_python_inc() + exec_prefix = sysconfig.get_config_var("exec_prefix") + include = sysconfig.get_config_var("INCLUDEPY") print(include) lib_path = os.path.join(exec_prefix, 'libs') if not os.path.exists(lib_path): -- cgit v0.12