From 27378ccf8e497f6d5d938470db8a9caacba176ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Thu, 16 Jul 2020 21:04:44 +0200 Subject: Clean up more py2 conditional code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes: https://github.com/gentoo/portage/pull/575 Reviewed-by: Zac Medico Signed-off-by: Michał Górny --- lib/portage/cache/anydbm.py | 4 +- lib/portage/cache/mappings.py | 45 ++++++------------ lib/portage/cache/sql_template.py | 5 +- lib/portage/cache/template.py | 12 ++--- lib/portage/elog/messages.py | 3 +- lib/portage/output.py | 3 +- lib/portage/package/ebuild/config.py | 11 +---- .../util/futures/asyncio/test_subprocess_exec.py | 4 -- lib/portage/tests/util/futures/test_retry.py | 2 - lib/portage/tests/util/test_socks5.py | 16 ++----- lib/portage/util/__init__.py | 53 +++++----------------- lib/portage/util/_dyn_libs/NeededEntry.py | 10 ---- lib/portage/util/digraph.py | 3 -- lib/portage/util/listdir.py | 2 - lib/portage/util/whirlpool.py | 25 +++++----- lib/portage/xpak.py | 2 - 16 files changed, 52 insertions(+), 148 deletions(-) (limited to 'lib/portage') diff --git a/lib/portage/cache/anydbm.py b/lib/portage/cache/anydbm.py index 88d85b0da..121a4eaf2 100644 --- a/lib/portage/cache/anydbm.py +++ b/lib/portage/cache/anydbm.py @@ -112,5 +112,5 @@ class database(fs_template.FsBased): self.__db.sync() self.__db.close() - if sys.hexversion >= 0x3000000: - items = iteritems + # TODO: do we need iteritems()? + items = iteritems diff --git a/lib/portage/cache/mappings.py b/lib/portage/cache/mappings.py index 0432fdf60..0adecde4a 100644 --- a/lib/portage/cache/mappings.py +++ b/lib/portage/cache/mappings.py @@ -25,9 +25,6 @@ class Mapping(object): def __iter__(self): return iter(self.keys()) - def keys(self): - return list(self.__iter__()) - def __contains__(self, key): try: value = self[key] @@ -46,12 +43,6 @@ class Mapping(object): for _, v in self.items(): yield v - def values(self): - return [v for _, v in self.iteritems()] - - def items(self): - return list(self.iteritems()) - def get(self, key, default=None): try: return self[key] @@ -64,10 +55,10 @@ class Mapping(object): def __len__(self): return len(list(self)) - if sys.hexversion >= 0x3000000: - items = iteritems - keys = __iter__ - values = itervalues + # TODO: do we need to keep iter*? + items = iteritems + keys = __iter__ + values = itervalues class MutableMapping(Mapping): """ @@ -184,8 +175,8 @@ class UserDict(MutableMapping): def clear(self): self.data.clear() - if sys.hexversion >= 0x3000000: - keys = __iter__ + keys = __iter__ + class ProtectedDict(MutableMapping): """ @@ -234,8 +225,8 @@ class ProtectedDict(MutableMapping): def __contains__(self, key): return key in self.new or (key not in self.blacklist and key in self.orig) - if sys.hexversion >= 0x3000000: - keys = __iter__ + keys = __iter__ + class LazyLoad(Mapping): """ @@ -271,8 +262,8 @@ class LazyLoad(Mapping): self.pull = None return key in self.d - if sys.hexversion >= 0x3000000: - keys = __iter__ + keys = __iter__ + _slot_dict_classes = weakref.WeakValueDictionary() @@ -328,9 +319,6 @@ def slot_dict_class(keys, prefix="_val_"): l += 1 return l - def keys(self): - return list(self) - def iteritems(self): prefix = self._prefix for k in self.allowed_keys: @@ -339,16 +327,10 @@ def slot_dict_class(keys, prefix="_val_"): except AttributeError: pass - def items(self): - return list(self.iteritems()) - def itervalues(self): for k, v in self.iteritems(): yield v - def values(self): - return list(self.itervalues()) - def __delitem__(self, k): try: delattr(self, self._prefix + k) @@ -447,10 +429,9 @@ def slot_dict_class(keys, prefix="_val_"): def __repr__(self): return repr(dict(self.iteritems())) - if sys.hexversion >= 0x3000000: - items = iteritems - keys = __iter__ - values = itervalues + items = iteritems + keys = __iter__ + values = itervalues v = SlotDict _slot_dict_classes[v.allowed_keys] = v diff --git a/lib/portage/cache/sql_template.py b/lib/portage/cache/sql_template.py index d023b1b5d..ba75a529f 100644 --- a/lib/portage/cache/sql_template.py +++ b/lib/portage/cache/sql_template.py @@ -296,6 +296,5 @@ class SQLDatabase(template.database): return [ row[0] for row in self.con.fetchall() ] - if sys.hexversion >= 0x3000000: - items = iteritems - keys = __iter__ + items = iteritems + keys = __iter__ diff --git a/lib/portage/cache/template.py b/lib/portage/cache/template.py index d7fff3e32..e2dc3f088 100644 --- a/lib/portage/cache/template.py +++ b/lib/portage/cache/template.py @@ -171,9 +171,6 @@ class database(object): def has_key(self, cpv): return cpv in self - def keys(self): - return list(self) - def iterkeys(self): return iter(self) @@ -181,9 +178,6 @@ class database(object): for x in self: yield (x, self[x]) - def items(self): - return list(self.iteritems()) - def sync(self, rate=0): self.sync_rate = rate if(rate == 0): @@ -290,9 +284,9 @@ class database(object): if cont: yield cpv - if sys.hexversion >= 0x3000000: - keys = __iter__ - items = iteritems + keys = __iter__ + items = iteritems + _keysorter = operator.itemgetter(0) diff --git a/lib/portage/elog/messages.py b/lib/portage/elog/messages.py index a4897d8d8..4917d44dd 100644 --- a/lib/portage/elog/messages.py +++ b/lib/portage/elog/messages.py @@ -122,8 +122,7 @@ def _elog_base(level, msg, phase="other", key=None, color=None, out=None): if out in (sys.stdout, sys.stderr): formatted_msg = _unicode_encode(formatted_msg, encoding=_encodings['stdio'], errors='backslashreplace') - if sys.hexversion >= 0x3000000: - out = out.buffer + out = out.buffer out.write(formatted_msg) diff --git a/lib/portage/output.py b/lib/portage/output.py index 26880adca..dbfb81714 100644 --- a/lib/portage/output.py +++ b/lib/portage/output.py @@ -396,8 +396,7 @@ class ConsoleStyleFile(object): if f in (sys.stdout, sys.stderr): s = _unicode_encode(s, encoding=_encodings['stdio'], errors='backslashreplace') - if sys.hexversion >= 0x3000000: - f = f.buffer + f = f.buffer f.write(s) def writelines(self, lines): diff --git a/lib/portage/package/ebuild/config.py b/lib/portage/package/ebuild/config.py index 4ae53f5b2..2e62ef5ce 100644 --- a/lib/portage/package/ebuild/config.py +++ b/lib/portage/package/ebuild/config.py @@ -2708,9 +2708,6 @@ class config(object): self[k] = x return x - def keys(self): - return list(self) - def __iter__(self): keys = set() keys.update(self._constant_keys) @@ -2725,9 +2722,6 @@ class config(object): for k in self: yield (k, self._getitem(k)) - def items(self): - return list(self.iteritems()) - def __setitem__(self,mykey,myvalue): "set a value; will be thrown away at reset() time" if not isinstance(myvalue, str): @@ -2918,6 +2912,5 @@ class config(object): return self._selinux_enabled - if sys.hexversion >= 0x3000000: - keys = __iter__ - items = iteritems + keys = __iter__ + items = iteritems diff --git a/lib/portage/tests/util/futures/asyncio/test_subprocess_exec.py b/lib/portage/tests/util/futures/asyncio/test_subprocess_exec.py index d7e94d132..6ad987316 100644 --- a/lib/portage/tests/util/futures/asyncio/test_subprocess_exec.py +++ b/lib/portage/tests/util/futures/asyncio/test_subprocess_exec.py @@ -132,8 +132,6 @@ class SubprocessExecTestCase(TestCase): requires an AbstractEventLoop.connect_read_pipe implementation (and a ReadTransport implementation for it to return). """ - if sys.version_info.major < 3: - self.skipTest('ReadTransport not implemented for python2') args_tuple = (b'hello', b'world') echo_binary = find_binary("echo") @@ -162,8 +160,6 @@ class SubprocessExecTestCase(TestCase): requires an AbstractEventLoop.connect_write_pipe implementation (and a WriteTransport implementation for it to return). """ - if sys.version_info.major < 3: - self.skipTest('WriteTransport not implemented for python2') stdin_data = b'hello world' cat_binary = find_binary("cat") diff --git a/lib/portage/tests/util/futures/test_retry.py b/lib/portage/tests/util/futures/test_retry.py index 4530bba83..94ede2e17 100644 --- a/lib/portage/tests/util/futures/test_retry.py +++ b/lib/portage/tests/util/futures/test_retry.py @@ -229,6 +229,4 @@ class RetryForkExecutorTestCase(RetryTestCase): class RetryThreadExecutorTestCase(RetryForkExecutorTestCase): def _setUpExecutor(self): - if sys.version_info.major < 3: - self.skipTest('ThreadPoolExecutor not supported for python2') self._executor = ThreadPoolExecutor(max_workers=1) diff --git a/lib/portage/tests/util/test_socks5.py b/lib/portage/tests/util/test_socks5.py index 5db85b0a6..f7b893996 100644 --- a/lib/portage/tests/util/test_socks5.py +++ b/lib/portage/tests/util/test_socks5.py @@ -193,19 +193,13 @@ class Socks5ServerTestCase(TestCase): 'PORTAGE_BIN_PATH': PORTAGE_BIN_PATH, } - try: - proxy = socks5.get_socks5_proxy(settings) - except NotImplementedError: - # bug 658172 for python2.7 - self.skipTest('get_socks5_proxy not implemented for {} {}.{}'.format( - platform.python_implementation(), *sys.version_info[:2])) - else: - loop.run_until_complete(socks5.proxy.ready()) + proxy = socks5.get_socks5_proxy(settings) + loop.run_until_complete(socks5.proxy.ready()) - result = loop.run_until_complete(loop.run_in_executor(None, - self._fetch_via_proxy, proxy, host, server.server_port, path)) + result = loop.run_until_complete(loop.run_in_executor(None, + self._fetch_via_proxy, proxy, host, server.server_port, path)) - self.assertEqual(result, content) + self.assertEqual(result, content) finally: socks5.proxy.stop() shutil.rmtree(tempdir) diff --git a/lib/portage/util/__init__.py b/lib/portage/util/__init__.py index 0ff34da30..e390874f2 100644 --- a/lib/portage/util/__init__.py +++ b/lib/portage/util/__init__.py @@ -72,7 +72,7 @@ def writemsg(mystr, noiselevel=0, fd=None): else: mystr = _unicode_encode(mystr, encoding=_encodings['stdio'], errors='backslashreplace') - if sys.hexversion >= 0x3000000 and fd in (sys.stdout, sys.stderr): + if fd in (sys.stdout, sys.stderr): fd = fd.buffer fd.write(mystr) fd.flush() @@ -107,7 +107,7 @@ def normalize_path(mypath): We dislike this behavior so we create our own normpath func to fix it. """ - if sys.hexversion >= 0x3000000 and isinstance(mypath, bytes): + if isinstance(mypath, bytes): path_sep = os.path.sep.encode() else: path_sep = os.path.sep @@ -591,19 +591,15 @@ def writedict(mydict, myfilename, writekey=True): lines.append("%s %s\n" % (k, " ".join(v))) write_atomic(myfilename, "".join(lines)) + def shlex_split(s): """ This is equivalent to shlex.split, but if the current interpreter is python2, it temporarily encodes unicode strings to bytes since python2's shlex.split() doesn't handle unicode strings. """ - convert_to_bytes = sys.hexversion < 0x3000000 and not isinstance(s, bytes) - if convert_to_bytes: - s = _unicode_encode(s) - rval = shlex.split(s) - if convert_to_bytes: - rval = [_unicode_decode(x) for x in rval] - return rval + return shlex.split(s) + class _getconfig_shlex(shlex.shlex): @@ -668,15 +664,9 @@ def getconfig(mycfg, tolerant=False, allow_sourcing=False, expand=True, f = None try: - # NOTE: shlex doesn't support unicode objects with Python 2 - # (produces spurious \0 characters). - if sys.hexversion < 0x3000000: - f = open(_unicode_encode(mycfg, - encoding=_encodings['fs'], errors='strict'), 'rb') - else: - f = open(_unicode_encode(mycfg, - encoding=_encodings['fs'], errors='strict'), mode='r', - encoding=_encodings['content'], errors='replace') + f = open(_unicode_encode(mycfg, + encoding=_encodings['fs'], errors='strict'), mode='r', + encoding=_encodings['content'], errors='replace') content = f.read() except IOError as e: if e.errno == PermissionDenied.errno: @@ -1309,29 +1299,10 @@ class atomic_ofstream(ObjectProxy): def _get_target(self): return object.__getattribute__(self, '_file') - if sys.hexversion >= 0x3000000: - - def __getattribute__(self, attr): - if attr in ('close', 'abort', '__del__'): - return object.__getattribute__(self, attr) - return getattr(object.__getattribute__(self, '_file'), attr) - - else: - - # For TextIOWrapper, automatically coerce write calls to - # unicode, in order to avoid TypeError when writing raw - # bytes with python2. - - def __getattribute__(self, attr): - if attr in ('close', 'abort', 'write', '__del__'): - return object.__getattribute__(self, attr) - return getattr(object.__getattribute__(self, '_file'), attr) - - def write(self, s): - f = object.__getattribute__(self, '_file') - if isinstance(f, io.TextIOWrapper): - s = _unicode_decode(s) - return f.write(s) + def __getattribute__(self, attr): + if attr in ('close', 'abort', '__del__'): + return object.__getattribute__(self, attr) + return getattr(object.__getattribute__(self, '_file'), attr) def close(self): """Closes the temporary file, copies permissions (if possible), diff --git a/lib/portage/util/_dyn_libs/NeededEntry.py b/lib/portage/util/_dyn_libs/NeededEntry.py index 59c4cf87d..154f50690 100644 --- a/lib/portage/util/_dyn_libs/NeededEntry.py +++ b/lib/portage/util/_dyn_libs/NeededEntry.py @@ -73,13 +73,3 @@ class NeededEntry(object): (self.multilib_category if self.multilib_category is not None else "") ]) + "\n" - - if sys.hexversion < 0x3000000: - - __unicode__ = __str__ - - def __str__(self): - return _unicode_encode(self.__unicode__(), - encoding=_encodings['content']) - - __str__.__doc__ = __unicode__.__doc__ diff --git a/lib/portage/util/digraph.py b/lib/portage/util/digraph.py index 23c9e3c1a..e75a3a686 100644 --- a/lib/portage/util/digraph.py +++ b/lib/portage/util/digraph.py @@ -383,6 +383,3 @@ class digraph(object): __contains__ = contains empty = is_empty copy = clone - - if sys.hexversion < 0x3000000: - __nonzero__ = __bool__ diff --git a/lib/portage/util/listdir.py b/lib/portage/util/listdir.py index 2012e145f..37312beb6 100644 --- a/lib/portage/util/listdir.py +++ b/lib/portage/util/listdir.py @@ -7,8 +7,6 @@ import errno import stat import sys -if sys.hexversion < 0x3000000: - from itertools import izip as zip from portage import os from portage.const import VCS_DIRS diff --git a/lib/portage/util/whirlpool.py b/lib/portage/util/whirlpool.py index 170ae73f8..a947dd719 100644 --- a/lib/portage/util/whirlpool.py +++ b/lib/portage/util/whirlpool.py @@ -26,8 +26,7 @@ ## This Python implementation is therefore also placed in the public domain. import sys -if sys.hexversion >= 0x3000000: - xrange = range + #block_size = 64 digest_size = 64 @@ -641,8 +640,6 @@ def WhirlpoolInit(ctx): def WhirlpoolAdd(source, sourceBits, ctx): if not isinstance(source, bytes): raise TypeError("Expected %s, got %s" % (bytes, type(source))) - if sys.hexversion < 0x3000000: - source = [ord(s)&0xff for s in source] carry = 0 value = sourceBits @@ -700,19 +697,19 @@ def WhirlpoolFinalize(ctx): bufferPos += 1 if bufferPos > 32: if bufferPos < 64: - for i in xrange(64 - bufferPos): + for i in range(64 - bufferPos): ctx.buffer[bufferPos+i] = 0 processBuffer(ctx) bufferPos = 0 if bufferPos < 32: - for i in xrange(32 - bufferPos): + for i in range(32 - bufferPos): ctx.buffer[bufferPos+i] = 0 bufferPos = 32 - for i in xrange(32): + for i in range(32): ctx.buffer[32+i] = ctx.bitLength[i] processBuffer(ctx) digest = '' - for i in xrange(8): + for i in range(8): digest += chr((ctx.hash[i] >> 56) % 0x100) digest += chr((ctx.hash[i] >> 48) % 0x100) digest += chr((ctx.hash[i] >> 40) % 0x100) @@ -743,7 +740,7 @@ def processBuffer(ctx): buffr = ctx.buffer buf_cnt = 0 - for i in xrange(8): + for i in range(8): block[i] = ((buffr[buf_cnt+0] & 0xff) << 56) ^ \ ((buffr[buf_cnt+1] & 0xff) << 48) ^ \ ((buffr[buf_cnt+2] & 0xff) << 40) ^ \ @@ -753,11 +750,11 @@ def processBuffer(ctx): ((buffr[buf_cnt+6] & 0xff) << 8) ^ \ ((buffr[buf_cnt+7] & 0xff) << 0) buf_cnt += 8 - for i in xrange(8): + for i in range(8): K[i] = ctx.hash[i] state[i] = block[i] ^ K[i] - for r in xrange(1, R+1): + for r in range(1, R+1): L[0] = CDo(K, 0, 7, 6, 5, 4, 3, 2, 1) ^ rc[r] L[1] = CDo(K, 1, 0, 7, 6, 5, 4, 3, 2) L[2] = CDo(K, 2, 1, 0, 7, 6, 5, 4, 3) @@ -766,7 +763,7 @@ def processBuffer(ctx): L[5] = CDo(K, 5, 4, 3, 2, 1, 0, 7, 6) L[6] = CDo(K, 6, 5, 4, 3, 2, 1, 0, 7) L[7] = CDo(K, 7, 6, 5, 4, 3, 2, 1, 0) - for i in xrange(8): + for i in range(8): K[i] = L[i] L[0] = CDo(state, 0, 7, 6, 5, 4, 3, 2, 1) ^ K[0] L[1] = CDo(state, 1, 0, 7, 6, 5, 4, 3, 2) ^ K[1] @@ -776,10 +773,10 @@ def processBuffer(ctx): L[5] = CDo(state, 5, 4, 3, 2, 1, 0, 7, 6) ^ K[5] L[6] = CDo(state, 6, 5, 4, 3, 2, 1, 0, 7) ^ K[6] L[7] = CDo(state, 7, 6, 5, 4, 3, 2, 1, 0) ^ K[7] - for i in xrange(8): + for i in range(8): state[i] = L[i] # apply the Miyaguchi-Preneel compression function - for i in xrange(8): + for i in range(8): ctx.hash[i] ^= state[i] ^ block[i] return diff --git a/lib/portage/xpak.py b/lib/portage/xpak.py index c708190b9..23539b6be 100644 --- a/lib/portage/xpak.py +++ b/lib/portage/xpak.py @@ -78,8 +78,6 @@ def encodeint(myint): def decodeint(mystring): """Takes a 4 byte string and converts it into a 4 byte integer. Returns an integer.""" - if sys.hexversion < 0x3000000: - mystring = [ord(x) for x in mystring] myint = 0 myint += mystring[3] myint += mystring[2] << 8 -- cgit v1.2.3