Skip to content

Remove some superflous Python 2 checks #4917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions easybuild/_deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"""
import contextlib
import functools
import json
import os
import re
import signal
Expand Down Expand Up @@ -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).
Expand Down
3 changes: 1 addition & 2 deletions easybuild/base/fancylogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion easybuild/framework/easyblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
3 changes: 1 addition & 2 deletions easybuild/scripts/findPythonDeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
10 changes: 5 additions & 5 deletions easybuild/tools/filetools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 1 addition & 14 deletions easybuild/tools/py2vs3/py3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions test/framework/easyblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
11 changes: 3 additions & 8 deletions test/framework/filetools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down