diff --git a/easybuild/_deprecated.py b/easybuild/_deprecated.py index 2beebd8174..961bf81311 100644 --- a/easybuild/_deprecated.py +++ b/easybuild/_deprecated.py @@ -31,6 +31,7 @@ """ import contextlib import functools +import json import os import re import signal @@ -96,6 +97,12 @@ def cache_aware_func(cmd, *args, **kwargs): return cache_aware_func +def json_loads(body): + """Deprecated wrapper for json.loads""" + _log.deprecated("json_loads is deprecated, use json.loads", '6.0') + return json.loads(body) + + def get_output_from_process(proc, read_size=None, asynchronous=False, print_deprecation_warning=True): """ Get output from running process (that was opened with subprocess.Popen). diff --git a/easybuild/base/fancylogger.py b/easybuild/base/fancylogger.py index 9c32fb7816..c19243ba13 100644 --- a/easybuild/base/fancylogger.py +++ b/easybuild/base/fancylogger.py @@ -578,8 +578,7 @@ def logToFile(filename, enable=True, filehandler=None, name=None, max_bytes=MAX_ 'maxBytes': max_bytes, 'backupCount': backup_count, } - if sys.version_info[0] >= 3: - handleropts['encoding'] = 'utf-8' + handleropts['encoding'] = 'utf-8' # logging to a file is going to create the file later on, so let's try to be helpful and create the path if needed directory = os.path.dirname(filename) if not os.path.exists(directory): diff --git a/easybuild/framework/easyblock.py b/easybuild/framework/easyblock.py index df88ad9ffd..2c3fd52b3c 100644 --- a/easybuild/framework/easyblock.py +++ b/easybuild/framework/easyblock.py @@ -448,7 +448,7 @@ def get_checksum_for(self, checksums, filename=None, index=None): if checksum and chksum_input_git is not None: # ignore any checksum for given filename due to changes in https://github.com/python/cpython/issues/90021 # tarballs made for git repos are not reproducible when created with Python < 3.9 - if sys.version_info[0] >= 3 and sys.version_info[1] < 9: + if sys.version_info < (3, 9): print_warning( "Reproducible tarballs of Git repos are only possible when using Python 3.9+ to run EasyBuild. " f"Skipping checksum verification of {chksum_input} since Python < 3.9 is used." diff --git a/easybuild/scripts/findPythonDeps.py b/easybuild/scripts/findPythonDeps.py index 9ed862bf4c..5578e2c632 100755 --- a/easybuild/scripts/findPythonDeps.py +++ b/easybuild/scripts/findPythonDeps.py @@ -85,8 +85,7 @@ def can_run(cmd, *arguments): def run_shell_cmd(arguments, action_desc, capture_stderr=True, **kwargs): """Run the command and return the return code and output""" extra_args = kwargs or {} - if sys.version_info[0] >= 3: - extra_args['universal_newlines'] = True + extra_args['universal_newlines'] = True stderr = subprocess.STDOUT if capture_stderr else subprocess.PIPE p = subprocess.Popen(arguments, stdout=subprocess.PIPE, stderr=stderr, **extra_args) out, err = p.communicate() diff --git a/easybuild/tools/filetools.py b/easybuild/tools/filetools.py index 8e891a0c5a..49545d13ec 100644 --- a/easybuild/tools/filetools.py +++ b/easybuild/tools/filetools.py @@ -137,7 +137,7 @@ def _hashlib_md5(): to set usedforsecurity to False when supported (Python >= 3.9) """ kwargs = {} - if sys.version_info[0] >= 3 and sys.version_info[1] >= 9: + if sys.version_info >= (3, 9): kwargs = {'usedforsecurity': False} return hashlib.md5(**kwargs) @@ -219,10 +219,10 @@ def is_readable(path): def open_file(path, mode): - """Open a (usually) text file. If mode is not binary, then utf-8 encoding will be used for Python 3.x""" + """Open a (usually) text file. If mode is not binary, then utf-8 encoding will be used""" # This is required for text files in Python 3, especially until Python 3.7 which implements PEP 540. # This PEP opens files in UTF-8 mode if the C locale is used, see https://www.python.org/dev/peps/pep-0540 - if sys.version_info[0] >= 3 and 'b' not in mode: + if 'b' not in mode: return open(path, mode, encoding='utf-8') else: return open(path, mode) @@ -281,8 +281,8 @@ def write_file(path, data, append=False, forced=False, backup=False, always_over data_is_file_obj = hasattr(data, 'read') - # special care must be taken with binary data in Python 3 - if sys.version_info[0] >= 3 and (isinstance(data, bytes) or data_is_file_obj): + # special care must be taken with binary data + if isinstance(data, bytes) or data_is_file_obj: mode += 'b' # don't bother showing a progress bar for small files (< 10MB) diff --git a/easybuild/tools/py2vs3/py3.py b/easybuild/tools/py2vs3/py3.py index 01c5a3a397..00de4c0a65 100644 --- a/easybuild/tools/py2vs3/py3.py +++ b/easybuild/tools/py2vs3/py3.py @@ -33,8 +33,6 @@ """ # these are not used here, but imported from here in other places import configparser # noqa -import json -import sys import urllib.request as std_urllib # noqa from collections.abc import Mapping # noqa from functools import cmp_to_key @@ -59,6 +57,7 @@ except ImportError: HAVE_DISTUTILS = False +from easybuild._deprecated import json_loads # noqa from easybuild.base.wrapper import mk_wrapper_baseclass # noqa from easybuild.tools.run import subprocess_popen_text, subprocess_terminate # noqa @@ -75,18 +74,6 @@ def load_source(filename, path): return module -def json_loads(body): - """Wrapper for json.loads that takes into account that Python versions older than 3.6 require a string value.""" - - if isinstance(body, bytes) and sys.version_info[0] == 3 and sys.version_info[1] < 6: - # decode bytes string as regular string with UTF-8 encoding for Python 3.5.x and older - # only Python 3.6 and newer have support for passing bytes string to json.loads - # cfr. https://docs.python.org/2/library/json.html#json.loads - body = body.decode('utf-8', 'ignore') - - return json.loads(body) - - def raise_with_traceback(exception_class, message, traceback): """Raise exception of specified class with given message and traceback.""" raise exception_class(message).with_traceback(traceback) diff --git a/test/framework/easyblock.py b/test/framework/easyblock.py index 737be6ecbc..feab869dff 100644 --- a/test/framework/easyblock.py +++ b/test/framework/easyblock.py @@ -1955,13 +1955,13 @@ def test_fetch_sources_git(self): ] checksums = ["00000000"] - if sys.version_info[0] >= 3 and sys.version_info[1] < 9: + if sys.version_info < (3, 9): self.allow_deprecated_behaviour() with self.mocked_stdout_stderr(): eb.fetch_sources(sources, checksums=checksums) - if sys.version_info[0] >= 3 and sys.version_info[1] < 9: + if sys.version_info < (3, 9): self.disallow_deprecated_behaviour() self.assertEqual(len(eb.src), 1) @@ -1970,7 +1970,7 @@ def test_fetch_sources_git(self): self.assertEqual(eb.src[0]['cmd'], None) reference_checksum = "00000000" - if sys.version_info[0] >= 3 and sys.version_info[1] < 9: + if sys.version_info[0] < (3, 9): # checksums of tarballs made by EB cannot be reliably checked prior to Python 3.9 # due to changes introduced in python/cpython#90021 reference_checksum = None diff --git a/test/framework/filetools.py b/test/framework/filetools.py index 3f30aa88dd..21c327b4a7 100644 --- a/test/framework/filetools.py +++ b/test/framework/filetools.py @@ -933,10 +933,7 @@ def test_read_write_file(self): utf8_file = os.path.join(self.test_prefix, 'utf8.txt') txt = b'Hyphen: \xe2\x80\x93\nEuro sign: \xe2\x82\xac\na with dots: \xc3\xa4' - if sys.version_info[0] == 3: - txt_decoded = txt.decode('utf-8') - else: - txt_decoded = txt + txt_decoded = txt.decode('utf-8') # Must work as binary and string ft.write_file(utf8_file, txt) self.assertEqual(ft.read_file(utf8_file), txt_decoded) @@ -1631,9 +1628,7 @@ def test_apply_regex_substitutions(self): ft.write_file(testfile, testtxt) ft.apply_regex_substitutions(testfile, [('foo', 'FOO')]) txt = ft.read_file(testfile) - if sys.version_info[0] == 3: - testtxt = testtxt.decode('utf-8') - self.assertEqual(txt, testtxt.replace('foo', 'FOO')) + self.assertEqual(txt, testtxt.decode('utf-8').replace('foo', 'FOO')) # make sure apply_regex_substitutions can patch files that include non-UTF-8 characters testtxt = b"foo \xe2 bar" @@ -3379,7 +3374,7 @@ def test_make_archive(self): reference_checksum_txz = "ec0f91a462c2743b19b428f4c177d7109d2ccc018dcdedc12570d9d735d6fb1b" reference_checksum_tar = "6e902e77925ab2faeef8377722434d4482f1fcc74af958c984c3f22509ae5084" - if sys.version_info[0] >= 3 and sys.version_info[1] >= 9: + if sys.version_info >= (3, 9): # checksums of tarballs made by EB cannot be reliably checked prior to Python 3.9 # due to changes introduced in python/cpython#90021 self.assertNotEqual(unreprod_txz_chksum, reference_checksum_txz)