From 13e292295eeaa9d4b1682243ca1679c724f11f6b Mon Sep 17 00:00:00 2001 From: Maciej Fijalkowski Date: Sat, 25 Jul 2020 11:22:03 +0200 Subject: [PATCH] Revert "futurize and make the tests pass back under py2" This reverts commit 361de059800a5f150775340dee273bde8e3ce11e. --- src/allmydata/test/common_util.py | 2 +- src/allmydata/test/test_util.py | 47 +++++++++++++----------------- src/allmydata/util/encodingutil.py | 3 +- src/allmydata/util/fileutil.py | 21 ++++--------- 4 files changed, 29 insertions(+), 44 deletions(-) diff --git a/src/allmydata/test/common_util.py b/src/allmydata/test/common_util.py index 510994d12..996052692 100644 --- a/src/allmydata/test/common_util.py +++ b/src/allmydata/test/common_util.py @@ -11,7 +11,7 @@ from twisted.trial import unittest from ..util.assertutil import precondition from allmydata.util.encodingutil import (unicode_platform, get_filesystem_encoding, get_io_encoding) -#from ..scripts import runner +from ..scripts import runner def skip_if_cannot_represent_filename(u): precondition(isinstance(u, unicode)) diff --git a/src/allmydata/test/test_util.py b/src/allmydata/test/test_util.py index 042875c6a..8c0e937f3 100644 --- a/src/allmydata/test/test_util.py +++ b/src/allmydata/test/test_util.py @@ -1,11 +1,5 @@ from __future__ import print_function -from __future__ import absolute_import -from __future__ import division -from __future__ import unicode_literals -from future.utils import PY2 -if PY2: - from builtins import zip, str, range, object import binascii import six import hashlib @@ -244,9 +238,9 @@ class FileUtil(ReallyEqualMixin, unittest.TestCase): basedir = "util/FileUtil/test_write_atomically" fileutil.make_dirs(basedir) fn = os.path.join(basedir, "here") - fileutil.write_atomically(fn, u"one") + fileutil.write_atomically(fn, "one") self.failUnlessEqual(fileutil.read(fn), "one") - fileutil.write_atomically(fn, u"two", mode="") # non-binary + fileutil.write_atomically(fn, "two", mode="") # non-binary self.failUnlessEqual(fileutil.read(fn), "two") def test_rename(self): @@ -329,13 +323,13 @@ class FileUtil(ReallyEqualMixin, unittest.TestCase): self.failUnlessEqual(10+11+12+13, used) def test_abspath_expanduser_unicode(self): - self.failUnlessRaises(AssertionError, fileutil.abspath_expanduser_unicode, b"bytestring") + self.failUnlessRaises(AssertionError, fileutil.abspath_expanduser_unicode, "bytestring") - saved_cwd = os.path.normpath(os.getcwd()).decode("utf8") + saved_cwd = os.path.normpath(os.getcwdu()) abspath_cwd = fileutil.abspath_expanduser_unicode(u".") abspath_cwd_notlong = fileutil.abspath_expanduser_unicode(u".", long_path=False) - self.failUnless(isinstance(saved_cwd, str), saved_cwd) - self.failUnless(isinstance(abspath_cwd, str), abspath_cwd) + self.failUnless(isinstance(saved_cwd, unicode), saved_cwd) + self.failUnless(isinstance(abspath_cwd, unicode), abspath_cwd) if sys.platform == "win32": self.failUnlessReallyEqual(abspath_cwd, fileutil.to_windows_long_path(saved_cwd)) else: @@ -396,10 +390,10 @@ class FileUtil(ReallyEqualMixin, unittest.TestCase): os.chdir(cwd) for upath in (u'', u'fuu', u'f\xf9\xf9', u'/fuu', u'U:\\', u'~'): uabspath = fileutil.abspath_expanduser_unicode(upath) - self.failUnless(isinstance(uabspath, str), uabspath) + self.failUnless(isinstance(uabspath, unicode), uabspath) uabspath_notlong = fileutil.abspath_expanduser_unicode(upath, long_path=False) - self.failUnless(isinstance(uabspath_notlong, str), uabspath_notlong) + self.failUnless(isinstance(uabspath_notlong, unicode), uabspath_notlong) finally: os.chdir(saved_cwd) @@ -553,7 +547,7 @@ class FileUtil(ReallyEqualMixin, unittest.TestCase): def test_encrypted_tempfile(self): f = EncryptedTemporaryFile() - f.write(b"foobar") + f.write("foobar") f.close() @@ -713,7 +707,7 @@ class DictUtil(unittest.TestCase): # we put the serialized form in the auxdata d.set_with_aux("key", ("filecap", "metadata"), "serialized") - self.failUnlessEqual(list(d.keys()), ["key"]) + self.failUnlessEqual(d.keys(), ["key"]) self.failUnlessEqual(d["key"], ("filecap", "metadata")) self.failUnlessEqual(d.get_aux("key"), "serialized") def _get_missing(key): @@ -731,7 +725,7 @@ class DictUtil(unittest.TestCase): d.set_with_aux("key2", "value2", "aux2") self.failUnlessEqual(sorted(d.keys()), ["key", "key2"]) del d["key2"] - self.failUnlessEqual(list(d.keys()), ["key"]) + self.failUnlessEqual(d.keys(), ["key"]) self.failIf("key2" in d) self.failUnlessRaises(KeyError, _get_missing, "key2") self.failUnlessEqual(d.get("key2"), None) @@ -812,7 +806,7 @@ class SimpleSpans(object): if prevstart is not None: yield (prevstart, prevend-prevstart+1) - def __bool__(self): # this gets us bool() + def __nonzero__(self): # this gets us bool() return self.len() def len(self): @@ -865,7 +859,7 @@ class ByteSpans(unittest.TestCase): s1 = Spans(3, 4) # 3,4,5,6 self._check1(s1) - s1 = Spans(int(3), int(4)) # 3,4,5,6 + s1 = Spans(long(3), long(4)) # 3,4,5,6 self._check1(s1) s2 = Spans(s1) @@ -1106,7 +1100,7 @@ class SimpleDataSpans(object): for (start, data) in other.get_chunks(): self.add(start, data) - def __bool__(self): # this gets us bool() + def __nonzero__(self): # this gets us bool() return self.len() def len(self): return len(self.missing.replace("1", "")) @@ -1192,9 +1186,9 @@ class StringSpans(unittest.TestCase): self.failUnlessEqual(ds.get(2, 4), "fear") ds = klass() - ds.add(int(2), "four") - ds.add(int(3), "ea") - self.failUnlessEqual(ds.get(int(2), int(4)), "fear") + ds.add(long(2), "four") + ds.add(long(3), "ea") + self.failUnlessEqual(ds.get(long(2), long(4)), "fear") def do_scan(self, klass): @@ -1357,7 +1351,6 @@ class YAML(unittest.TestCase): def test_convert(self): data = yaml.safe_dump(["str", u"unicode", u"\u1234nicode"]) back = yamlutil.safe_load(data) - str_type = type("x") # workaround for future.builtins.str - self.failUnlessEqual(type(back[0]), str_type) - self.failUnlessEqual(type(back[1]), str_type) - self.failUnlessEqual(type(back[2]), str_type) + self.failUnlessEqual(type(back[0]), unicode) + self.failUnlessEqual(type(back[1]), unicode) + self.failUnlessEqual(type(back[2]), unicode) diff --git a/src/allmydata/util/encodingutil.py b/src/allmydata/util/encodingutil.py index 491e5d61d..568267b35 100644 --- a/src/allmydata/util/encodingutil.py +++ b/src/allmydata/util/encodingutil.py @@ -4,6 +4,7 @@ unicode and back. """ import sys, os, re, locale +from types import NoneType from allmydata.util.assertutil import precondition, _assert from twisted.python import usage @@ -138,7 +139,7 @@ def to_str(s): return s.encode('utf-8') def from_utf8_or_none(s): - precondition(isinstance(s, str) or s is None, s) + precondition(isinstance(s, (NoneType, str)), s) if s is None: return s return s.decode('utf-8') diff --git a/src/allmydata/util/fileutil.py b/src/allmydata/util/fileutil.py index bdabe5aeb..269a8a356 100644 --- a/src/allmydata/util/fileutil.py +++ b/src/allmydata/util/fileutil.py @@ -1,16 +1,10 @@ from __future__ import print_function -from __future__ import absolute_import -from __future__ import division -from __future__ import unicode_literals -from future.utils import PY2 -if PY2: - from future.builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, int, list, object, range, str, max, min # noqa: F401from builtins import range """ Futz with files like a pro. """ -import sys, os, stat, tempfile, time, binascii +import sys, exceptions, os, stat, tempfile, time, binascii import six from collections import namedtuple from errno import ENOENT @@ -196,7 +190,7 @@ def make_dirs(dirname, mode=0o777): if not os.path.isdir(dirname): if tx: raise tx - raise IOError("unknown error prevented creation of directory, or deleted the directory immediately after creation: %s" % dirname) # careful not to construct an IOError with a 2-tuple, as that has a special meaning... + raise exceptions.IOError("unknown error prevented creation of directory, or deleted the directory immediately after creation: %s" % dirname) # careful not to construct an IOError with a 2-tuple, as that has a special meaning... def rm_dir(dirname): """ @@ -283,7 +277,7 @@ def put_file(path, inf): outf.write(data) def precondition_abspath(path): - if not isinstance(path, str): + if not isinstance(path, unicode): raise AssertionError("an abspath must be a Unicode string") if sys.platform == "win32": @@ -315,7 +309,7 @@ def abspath_expanduser_unicode(path, base=None, long_path=True): abspath_expanduser_unicode. On Windows, the result will be a long path unless long_path is given as False. """ - if not isinstance(path, str): + if not isinstance(path, unicode): raise AssertionError("paths must be Unicode strings") if base is not None and long_path: precondition_abspath(base) @@ -336,10 +330,7 @@ def abspath_expanduser_unicode(path, base=None, long_path=True): if not os.path.isabs(path): if base is None: - cwd = os.getcwd() - if PY2: - cwd = cwd.decode('utf8') - path = os.path.join(cwd, path) + path = os.path.join(os.getcwdu(), path) else: path = os.path.join(base, path) @@ -424,7 +415,7 @@ ERROR_ENVVAR_NOT_FOUND = 203 def windows_getenv(name): # Based on , # with improved error handling. Returns None if there is no enivronment variable of the given name. - if not isinstance(name, str): + if not isinstance(name, unicode): raise AssertionError("name must be Unicode") n = GetEnvironmentVariableW(name, None, 0)