From 74f86654b147fb9073b890834b72a32507b69b96 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Sat, 1 Jan 2022 18:11:36 +0100 Subject: [PATCH] Remove fallbacks for old Python versions. --- plugins/Autocomplete/__init__.py | 5 +---- plugins/Fediverse/__init__.py | 5 +---- plugins/Owner/test.py | 7 +------ plugins/Poll/__init__.py | 5 +---- plugins/SedRegex/plugin.py | 5 ----- scripts/supybot | 5 ----- scripts/supybot-plugin-create | 8 +------- scripts/supybot-test | 8 ++------ scripts/supybot-wizard | 3 --- setup.py | 3 +-- src/drivers/Socket.py | 5 +---- src/test.py | 28 ---------------------------- src/utils/gen.py | 3 +-- src/utils/str.py | 5 +---- src/world.py | 23 ----------------------- test/test_ircutils.py | 15 +++------------ test/test_utils.py | 29 +++++++++++++---------------- test/test_yn.py | 7 +------ 18 files changed, 28 insertions(+), 141 deletions(-) diff --git a/plugins/Autocomplete/__init__.py b/plugins/Autocomplete/__init__.py index 5a1996b2c..afc1573ef 100644 --- a/plugins/Autocomplete/__init__.py +++ b/plugins/Autocomplete/__init__.py @@ -60,10 +60,7 @@ __url__ = "" from . import config from . import plugin -if sys.version_info >= (3, 4): - from importlib import reload -else: - from imp import reload +from importlib import reload # In case we're being reloaded. reload(config) reload(plugin) diff --git a/plugins/Fediverse/__init__.py b/plugins/Fediverse/__init__.py index ebb5c93e3..c31057f6e 100644 --- a/plugins/Fediverse/__init__.py +++ b/plugins/Fediverse/__init__.py @@ -52,10 +52,7 @@ __url__ = "" from . import config from . import plugin -if sys.version_info >= (3, 4): - from importlib import reload -else: - from imp import reload +from importlib import reload # In case we're being reloaded. reload(config) reload(plugin) diff --git a/plugins/Owner/test.py b/plugins/Owner/test.py index c15f0fb90..555f9201b 100644 --- a/plugins/Owner/test.py +++ b/plugins/Owner/test.py @@ -30,13 +30,8 @@ ### import sys +from unittest import skip -if sys.version_info >= (2, 7, 0): - from unittest import skip -else: - # Workaround - def skip(string): - return lambda x:None from supybot.test import * import supybot.conf as conf diff --git a/plugins/Poll/__init__.py b/plugins/Poll/__init__.py index ca55dd13a..e11be72f7 100644 --- a/plugins/Poll/__init__.py +++ b/plugins/Poll/__init__.py @@ -52,10 +52,7 @@ __url__ = "" from . import config from . import plugin -if sys.version_info >= (3, 4): - from importlib import reload -else: - from imp import reload +from importlib import reload # In case we're being reloaded. reload(config) reload(plugin) diff --git a/plugins/SedRegex/plugin.py b/plugins/SedRegex/plugin.py index c95384384..79062f908 100644 --- a/plugins/SedRegex/plugin.py +++ b/plugins/SedRegex/plugin.py @@ -48,11 +48,6 @@ try: except ImportError: _ = lambda x: x -if sys.version_info[0] < 3: - raise ImportError('This plugin requires Python 3. For a legacy version of this plugin that still ' - 'supports Python 2, consult the python2-legacy branch at ' - 'https://github.com/jlu5/SupyPlugins/tree/python2-legacy') - from .constants import SED_REGEX, TAG_SEEN, TAG_IS_REGEX # Replace newlines and friends with things like literal "\n" (backslash and "n") diff --git a/scripts/supybot b/scripts/supybot index 37131c37f..e955433be 100644 --- a/scripts/supybot +++ b/scripts/supybot @@ -44,11 +44,6 @@ import atexit import shutil import signal -if sys.version_info < (3, 4, 0): - sys.stderr.write('This program requires Python 3.4 or later.') - sys.stderr.write(os.linesep) - sys.exit(-1) - from io import StringIO # Import this after version check since this will fail on Python 2 def _termHandler(signalNumber, stackFrame): diff --git a/scripts/supybot-plugin-create b/scripts/supybot-plugin-create index ef8ee87af..49a9d29be 100644 --- a/scripts/supybot-plugin-create +++ b/scripts/supybot-plugin-create @@ -46,9 +46,6 @@ def error(s): sys.stderr.write(os.linesep) sys.exit(-1) -if sys.version_info < (3, 4, 0): - error('This script requires Python 3.4 or newer.') - import supybot.conf as conf from supybot.questions import * @@ -173,10 +170,7 @@ __url__ = '' from . import config from . import plugin -if sys.version_info >= (3, 4): - from importlib import reload -else: - from imp import reload +from importlib import reload # In case we're being reloaded. reload(config) reload(plugin) diff --git a/scripts/supybot-test b/scripts/supybot-test index 069b30ebd..f21a1f1ff 100644 --- a/scripts/supybot-test +++ b/scripts/supybot-test @@ -223,12 +223,8 @@ if __name__ == '__main__': suite = unittest.TestSuite(test.suites) if options.fail_fast: - if sys.version_info < (2, 7, 0): - print('--fail-fast is not supported on Python 2.6.') - sys.exit(1) - else: - runner = unittest.TextTestRunner(verbosity=2, - failfast=True) + runner = unittest.TextTestRunner(verbosity=2, + failfast=True) else: runner = unittest.TextTestRunner(verbosity=2) print('Testing began at %s (pid %s)' % (time.ctime(), os.getpid())) diff --git a/scripts/supybot-wizard b/scripts/supybot-wizard index 0bd76d025..a21faea2b 100644 --- a/scripts/supybot-wizard +++ b/scripts/supybot-wizard @@ -42,9 +42,6 @@ def error(s): sys.stderr.write(os.linesep) sys.exit(-1) -if sys.version_info < (3, 4, 0): - error('This program requires Python 3.4 or later.') - import supybot import re diff --git a/setup.py b/setup.py index 2fab2604d..11db0af2e 100644 --- a/setup.py +++ b/setup.py @@ -77,8 +77,7 @@ try: proc = subprocess.Popen('git show HEAD --format=%ct', shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) date = proc.stdout.readline() - if sys.version_info[0] >= 3: - date = date.decode() + date = date.decode() date = int(date.strip()) version = ".".join(str(i).zfill(2) for i in time.strptime(time.asctime(time.gmtime(date)))[:3]) diff --git a/src/drivers/Socket.py b/src/drivers/Socket.py index e289b3520..354fc1f7c 100644 --- a/src/drivers/Socket.py +++ b/src/drivers/Socket.py @@ -325,10 +325,7 @@ class SocketDriver(drivers.IrcDriver, drivers.ServersMixin): self.starttls() # Suppress this warning for loopback IPs. - if sys.version_info[0] < 3: - # Backported Python 2 ipaddress demands unicode instead of str - address = address.decode('utf-8') - elif (not network_config.requireStarttls()) and \ + if (not network_config.requireStarttls()) and \ (not network_config.ssl()) and \ (not self.currentServer.force_tls_verification): diff --git a/src/test.py b/src/test.py index 8b94d43d9..08e43ba07 100644 --- a/src/test.py +++ b/src/test.py @@ -160,34 +160,6 @@ class SupyTestCase(unittest.TestCase): irc._reallyDie() teardownMockTime() - if sys.version_info < (2, 7, 0): - def assertIn(self, member, container, msg=None): - """Just like self.assertTrue(a in b), but with a nicer default message.""" - if member not in container: - standardMsg = '%s not found in %s' % (repr(member), - repr(container)) - self.fail(self._formatMessage(msg, standardMsg)) - - def assertNotIn(self, member, container, msg=None): - """Just like self.assertTrue(a not in b), but with a nicer default message.""" - if member in container: - standardMsg = '%s unexpectedly found in %s' % (repr(member), - repr(container)) - self.fail(self._formatMessage(msg, standardMsg)) - - def assertIs(self, expr1, expr2, msg=None): - """Just like self.assertTrue(a is b), but with a nicer default message.""" - if expr1 is not expr2: - standardMsg = '%s is not %s' % (repr(expr1), - repr(expr2)) - self.fail(self._formatMessage(msg, standardMsg)) - - def assertIsNot(self, expr1, expr2, msg=None): - """Just like self.assertTrue(a is not b), but with a nicer default message.""" - if expr1 is expr2: - standardMsg = 'unexpectedly identical: %s' % (repr(expr1),) - self.fail(self._formatMessage(msg, standardMsg)) - class PluginTestCase(SupyTestCase): """Subclass this to write a test case for a plugin. See diff --git a/src/utils/gen.py b/src/utils/gen.py index c7ec0b758..f732d28f9 100644 --- a/src/utils/gen.py +++ b/src/utils/gen.py @@ -194,8 +194,7 @@ def safeEval(s, namespace=None): return True else: return False - elif sys.version_info[0:2] >= (3, 4) and \ - node.__class__ is ast.NameConstant: + elif node.__class__ is ast.NameConstant: return True elif sys.version_info[0:2] >= (3, 8) and \ node.__class__ is ast.Constant: diff --git a/src/utils/str.py b/src/utils/str.py index 854a80cc4..f6a43e58e 100644 --- a/src/utils/str.py +++ b/src/utils/str.py @@ -310,10 +310,7 @@ def splitBytes(word, size): # I'm going to hell for this function for i in range(4): # a character takes at most 4 bytes in UTF-8 try: - if sys.version_info[0] >= 3: - word[size-i:].decode() - else: - word[size-i:].encode('utf8') + word[size-i:].decode() except UnicodeDecodeError: continue else: diff --git a/src/world.py b/src/world.py index a34a23619..85f581ec5 100644 --- a/src/world.py +++ b/src/world.py @@ -72,29 +72,6 @@ class SupyProcess(multiprocessing.Process): super(SupyProcess, self).__init__(*args, **kwargs) log.debug('Spawning process %q.', self.name) -if sys.version_info[0:3] == (3, 3, 1) and hasattr(select, 'poll'): - # http://bugs.python.org/issue17707 - import multiprocessing.connection - def _poll(fds, timeout): - if timeout is not None: - timeout = int(timeout * 1000) # timeout is in milliseconds - fd_map = {} - pollster = select.poll() - for fd in fds: - pollster.register(fd, select.POLLIN) - if hasattr(fd, 'fileno'): - fd_map[fd.fileno()] = fd - else: - fd_map[fd] = fd - ls = [] - for fd, event in pollster.poll(timeout): - if event & select.POLLNVAL: - raise ValueError('invalid file descriptor %i' % fd) - ls.append(fd_map[fd]) - return ls - multiprocessing.connection._poll = _poll - - commandsProcessed = 0 ircs = [] # A list of all the IRCs. diff --git a/test/test_ircutils.py b/test/test_ircutils.py index 52e203eaa..cf0d98e4c 100644 --- a/test/test_ircutils.py +++ b/test/test_ircutils.py @@ -231,10 +231,7 @@ class FunctionsTestCase(SupyTestCase): self.assertEqual(ircutils.stripFormatting(s), '[09:21') def testWrap(self): - if sys.version_info[0] < 3: - pred = len - else: - pred = lambda s:len(s.encode()) + pred = lambda s:len(s.encode()) s = ('foo bar baz qux ' * 100)[0:-1] @@ -246,13 +243,7 @@ class FunctionsTestCase(SupyTestCase): self.assertTrue(max(map(pred, r)) <= 100) self.assertEqual(''.join(r), s) - if sys.version_info[0] < 3: - uchr = unichr - u = lambda s: s.decode('utf8') - else: - uchr = chr - u = lambda x: x - s = (u('').join([uchr(0x1f527), uchr(0x1f527), uchr(0x1f527), u(' ')]) * 100)\ + s = (''.join([chr(0x1f527), chr(0x1f527), chr(0x1f527), ' ']) * 100)\ [0:-1] r = ircutils.wrap(s, 20) @@ -283,7 +274,7 @@ class FunctionsTestCase(SupyTestCase): self.assertTrue(max(map(pred, r)) <= 100) self.assertEqual(''.join(r), s) - s = uchr(233)*500 + s = chr(233)*500 r = ircutils.wrap(s, 500) self.assertTrue(max(map(pred, r)) <= 500) r = ircutils.wrap(s, 139) diff --git a/test/test_utils.py b/test/test_utils.py index 26378f11f..b0a5dc2fb 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -39,9 +39,6 @@ import supybot.utils as utils from supybot.utils.structures import * import supybot.utils.minisix as minisix -if sys.version_info[0] >= 0: - xrange = range - class UtilsTest(SupyTestCase): def testReversed(self): L = list(range(10)) @@ -54,7 +51,7 @@ class UtilsTest(SupyTestCase): class SeqTest(SupyTestCase): def testRenumerate(self): - for i in xrange(5): + for i in range(5): L = list(enumerate(range(i))) LL = list(utils.seq.renumerate(range(i))) self.assertEqual(L, LL[::-1]) @@ -423,7 +420,7 @@ class IterTest(SupyTestCase): L = [1, 2] seenList = set() seenIterable = set() - for n in xrange(300): + for n in range(300): # 2**266 > 10**80, the number of atoms in the known universe. # (ignoring dark matter, but that likely doesn't exist in atoms # anyway, so it shouldn't have a significant impact on that #) @@ -748,13 +745,13 @@ class QueueTest(SupyTestCase): q = queue() n = 10 self.assertRaises(IndexError, q.__getitem__, 0) - for i in xrange(n): + for i in range(n): q.enqueue(i) - for i in xrange(n): + for i in range(n): self.assertEqual(q[i], i) - for i in xrange(n, 0, -1): + for i in range(n, 0, -1): self.assertEqual(q[-i], n-i) - for i in xrange(len(q)): + for i in range(len(q)): self.assertEqual(list(q), list(q[:i]) + list(q[i:])) self.assertRaises(IndexError, q.__getitem__, -(n+1)) self.assertRaises(IndexError, q.__getitem__, n) @@ -763,7 +760,7 @@ class QueueTest(SupyTestCase): def testSetitem(self): q1 = queue() self.assertRaises(IndexError, q1.__setitem__, 0, 0) - for i in xrange(10): + for i in range(10): q1.enqueue(i) q2 = eval(repr(q1)) for (i, elt) in enumerate(q2): @@ -913,13 +910,13 @@ class SmallQueueTest(SupyTestCase): q = queue() n = 10 self.assertRaises(IndexError, q.__getitem__, 0) - for i in xrange(n): + for i in range(n): q.enqueue(i) - for i in xrange(n): + for i in range(n): self.assertEqual(q[i], i) - for i in xrange(n, 0, -1): + for i in range(n, 0, -1): self.assertEqual(q[-i], n-i) - for i in xrange(len(q)): + for i in range(len(q)): self.assertEqual(list(q), list(q[:i]) + list(q[i:])) self.assertRaises(IndexError, q.__getitem__, -(n+1)) self.assertRaises(IndexError, q.__getitem__, n) @@ -928,7 +925,7 @@ class SmallQueueTest(SupyTestCase): def testSetitem(self): q1 = queue() self.assertRaises(IndexError, q1.__setitem__, 0, 0) - for i in xrange(10): + for i in range(10): q1.enqueue(i) q2 = eval(repr(q1)) for (i, elt) in enumerate(q2): @@ -1161,7 +1158,7 @@ class TestCacheDict(SupyTestCase): def testMaxNeverExceeded(self): max = 10 d = CacheDict(10) - for i in xrange(max**2): + for i in range(max**2): d[i] = i self.assertTrue(len(d) <= max) self.assertTrue(i in d) diff --git a/test/test_yn.py b/test/test_yn.py index 8818937ac..37b7bfa85 100644 --- a/test/test_yn.py +++ b/test/test_yn.py @@ -33,11 +33,6 @@ import unittest from supybot import questions from supybot.test import SupyTestCase -if sys.version_info >= (2, 7, 0): - skipif = unittest.skipIf -else: - skipif = lambda x, y: lambda z:None - try: from unittest import mock # Python 3.3+ except ImportError: @@ -53,7 +48,7 @@ except ImportError: # better solution is usage of '==' operator ;) _yes_answer = ''.join(['', 'y']) -@skipif(mock is None, 'python-mock is not installed.') +@unittest.skipIf(mock is None, 'python-mock is not installed.') class TestYn(SupyTestCase): def test_default_yes_selected(self): questions.expect = mock.Mock(return_value=_yes_answer)