提交 dfb6f577 编写于 作者: C Cleber Rosa

Python 2 leftovers: drop conditions and keep Python 3 only

There's no need to keep the special conditions for Python 2, now that
it's not supported anymore.
Signed-off-by: NCleber Rosa <crosa@redhat.com>
上级 8c0c742a
......@@ -28,7 +28,6 @@ And not notice until their code starts failing.
import itertools
import locale
import re
import sys
import string
from six import string_types, PY3
......@@ -315,8 +314,6 @@ def is_text(data):
That is, if it can hold text that requires more than one byte for
each character.
"""
if sys.version_info[0] < 3:
return isinstance(data, unicode) # pylint: disable=E0602
return isinstance(data, str)
......@@ -341,8 +338,5 @@ def to_text(data, encoding=ENCODING, errors='strict'):
encoding = ENCODING
return data.decode(encoding, errors=errors)
elif not isinstance(data, string_types):
if sys.version_info[0] < 3:
return unicode(data) # pylint: disable=E0602
else:
return str(data)
return str(data)
return data
......@@ -28,7 +28,6 @@ import shutil
import signal
import stat
import subprocess
import sys
import threading
import time
......@@ -312,14 +311,8 @@ def cmd_split(cmd):
:param cmd: text (a multi byte string) encoded as 'utf-8'
"""
if sys.version_info[0] < 3:
data = cmd.encode('utf-8')
result = shlex.split(data)
result = [i.decode('utf-8') for i in result]
else:
data = astring.to_text(cmd, 'utf-8')
result = shlex.split(data)
return result
data = astring.to_text(cmd, 'utf-8')
return shlex.split(data)
class CmdResult(object):
......
......@@ -156,13 +156,9 @@ class RunnerOperationTest(unittest.TestCase):
def test_show_version(self):
result = process.run('%s -v' % AVOCADO, ignore_status=True)
self.assertEqual(result.exit_status, 0)
if sys.version_info[0] == 3:
content = result.stdout_text
else:
content = result.stderr_text
self.assertTrue(re.match(r"^Avocado \d+\.\d+$", content),
self.assertTrue(re.match(r"^Avocado \d+\.\d+$", result.stdout_text),
"Version string does not match 'Avocado \\d\\.\\d:'\n"
"%r" % (content))
"%r" % (result.stdout_text))
def test_alternate_config_datadir(self):
"""
......@@ -438,11 +434,8 @@ class RunnerOperationTest(unittest.TestCase):
cmd_line = AVOCADO
result = process.run(cmd_line, ignore_status=True)
self.assertEqual(result.exit_status, exit_codes.AVOCADO_FAIL)
if sys.version_info[0] == 3:
exp = b'avocado: error: the following arguments are required'
else:
exp = b'error: too few arguments'
self.assertIn(exp, result.stderr)
self.assertIn(b'avocado: error: the following arguments are required',
result.stderr)
def test_empty_test_list(self):
cmd_line = '%s run --sysinfo=off --job-results-dir %s' % (AVOCADO,
......@@ -1120,8 +1113,7 @@ class PluginsTest(AbsPluginsTest, unittest.TestCase):
self.assertEqual(result.exit_status, expected_rc,
"Avocado did not return rc %d:\n%s" %
(expected_rc, result))
if sys.version_info[:2] >= (2, 7, 0):
self.assertNotIn(b'Disabled', result.stdout)
self.assertNotIn(b'Disabled', result.stdout)
def test_config_plugin(self):
cmd_line = '%s config --paginator off' % AVOCADO
......
......@@ -48,10 +48,7 @@ class StandaloneTests(unittest.TestCase):
cmd_line = '%s ./examples/tests/errortest_nasty.py -r' % PY_CMD
expected_rc = exit_codes.AVOCADO_TESTS_FAIL
result = self.run_and_check(cmd_line, expected_rc, 'errortest_nasty')
if sys.version_info[0] == 3:
exc = u"errortest_nasty.NastyException: Nasty-string-like-exception\u017e"
else:
exc = u"NastyException: Nasty-string-like-exception\\u017e"
exc = u"errortest_nasty.NastyException: Nasty-string-like-exception\u017e"
count = result.stdout_text.count(u"\n%s" % exc)
self.assertEqual(count, 2, "Exception \\n%s should be present twice in"
"the log (once from the log, second time when parsing"
......@@ -68,10 +65,7 @@ class StandaloneTests(unittest.TestCase):
cmd_line = '%s ./examples/tests/errortest_nasty3.py -r' % PY_CMD
expected_rc = exit_codes.AVOCADO_TESTS_FAIL
result = self.run_and_check(cmd_line, expected_rc, 'errortest_nasty3')
if sys.version_info[0] == 3:
exc = b"TypeError: exceptions must derive from BaseException"
else:
exc = b"TestError: <errortest_nasty3.NastyException instance at "
exc = b"TypeError: exceptions must derive from BaseException"
self.assertIn(exc, result.stdout)
def test_errortest(self):
......
......@@ -88,11 +88,7 @@ class AstringTest(unittest.TestCase):
self.assertFalse(astring.is_bytes(text))
self.assertTrue(hasattr(binary, 'decode'))
self.assertTrue(astring.is_text(binary.decode()))
# on Python 2, each str member is also a single byte char
if sys.version_info[0] < 3:
self.assertTrue(astring.is_bytes(str('')))
else:
self.assertFalse(astring.is_bytes(str('')))
self.assertFalse(astring.is_bytes(str('')))
def test_is_text(self):
"""
......
import sys
import unittest.mock
from .. import recent_mock
......@@ -40,11 +39,6 @@ PROC_MOUNTS = (
class Disk(unittest.TestCase):
@property
def builtin_open(self):
py_version = sys.version_info[0]
return 'builtins.open' if py_version == 3 else '__builtin__.open'
def test_empty(self):
mock_result = process.CmdResult(
command='lsblk --json',
......@@ -66,7 +60,7 @@ class Disk(unittest.TestCase):
def test_get_filesystems(self):
expected_fs = ['dax', 'bpf', 'pipefs', 'hugetlbfs', 'devpts', 'ext3']
open_mocked = unittest.mock.mock_open(read_data=PROC_FILESYSTEMS)
with unittest.mock.patch(self.builtin_open, open_mocked):
with unittest.mock.patch('builtins.open', open_mocked):
self.assertEqual(sorted(expected_fs),
sorted(disk.get_available_filesystems()))
......@@ -74,14 +68,14 @@ class Disk(unittest.TestCase):
"mock library version cannot (easily) patch open()")
def test_get_filesystem_type_default_root(self):
open_mocked = unittest.mock.mock_open(read_data=PROC_MOUNTS)
with unittest.mock.patch(self.builtin_open, open_mocked):
with unittest.mock.patch('builtins.open', open_mocked):
self.assertEqual('ext4', disk.get_filesystem_type())
@unittest.skipUnless(recent_mock(),
"mock library version cannot (easily) patch open()")
def test_get_filesystem_type(self):
open_mocked = unittest.mock.mock_open(read_data=PROC_MOUNTS)
with unittest.mock.patch(self.builtin_open, open_mocked):
with unittest.mock.patch('builtins.open', open_mocked):
self.assertEqual('ext2', disk.get_filesystem_type(mount_point='/home'))
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册