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