From 4b19ac772364778a4b96d7e18834db9a7645f482 Mon Sep 17 00:00:00 2001 From: Tim Burke Date: Thu, 1 Feb 2018 14:30:19 -0800 Subject: [PATCH] py3: port common/storage_policy.py Change-Id: I7030280a8495628df9ed8edcc8abc31f901da72e --- swift/common/storage_policy.py | 20 +++++++++++++++++--- test/unit/common/test_storage_policy.py | 9 ++++++--- tox.ini | 1 + 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/swift/common/storage_policy.py b/swift/common/storage_policy.py index 75650e5bd7..baf224227e 100644 --- a/swift/common/storage_policy.py +++ b/swift/common/storage_policy.py @@ -203,8 +203,17 @@ class BaseStoragePolicy(object): def __int__(self): return self.idx - def __cmp__(self, other): - return cmp(self.idx, int(other)) + def __eq__(self, other): + return self.idx == int(other) + + def __ne__(self, other): + return self.idx != int(other) + + def __lt__(self, other): + return self.idx < int(other) + + def __gt__(self, other): + return self.idx > int(other) def __repr__(self): return ("%s(%d, %r, is_default=%s, " @@ -923,7 +932,12 @@ def reload_storage_policies(): Reload POLICIES from ``swift.conf``. """ global _POLICIES - policy_conf = ConfigParser() + if six.PY2: + policy_conf = ConfigParser() + else: + # Python 3.2 disallows section or option duplicates by default + # strict=False allows us to preserve the older behavior + policy_conf = ConfigParser(strict=False) policy_conf.read(utils.SWIFT_CONF_FILE) try: _POLICIES = parse_storage_policies(policy_conf) diff --git a/test/unit/common/test_storage_policy.py b/test/unit/common/test_storage_policy.py index fc9b3c788e..e82305aaf4 100644 --- a/test/unit/common/test_storage_policy.py +++ b/test/unit/common/test_storage_policy.py @@ -70,7 +70,10 @@ class FakeStoragePolicy(BaseStoragePolicy): class TestStoragePolicies(unittest.TestCase): def _conf(self, conf_str): conf_str = "\n".join(line.strip() for line in conf_str.split("\n")) - conf = ConfigParser() + if six.PY2: + conf = ConfigParser() + else: + conf = ConfigParser(strict=False) conf.readfp(six.StringIO(conf_str)) return conf @@ -679,7 +682,7 @@ class TestStoragePolicies(unittest.TestCase): with capture_logging('swift.common.storage_policy') as records, \ self.assertRaises(PolicyError) as exc_mgr: parse_storage_policies(bad_conf) - self.assertEqual(exc_mgr.exception.message, + self.assertEqual(exc_mgr.exception.args[0], 'Storage policy bad-policy uses an EC ' 'configuration known to harm data durability. This ' 'policy MUST be deprecated.') @@ -1048,7 +1051,7 @@ class TestStoragePolicies(unittest.TestCase): [storage-policy:00] name = double-zero """) - with NamedTemporaryFile() as f: + with NamedTemporaryFile(mode='w+t') as f: conf.write(f) f.flush() with mock.patch('swift.common.utils.SWIFT_CONF_FILE', diff --git a/tox.ini b/tox.ini index f28ca1a25c..6acbb34f5d 100644 --- a/tox.ini +++ b/tox.ini @@ -38,6 +38,7 @@ commands = test/unit/common/test_linkat.py \ test/unit/common/test_manager.py \ test/unit/common/test_splice.py \ + test/unit/common/test_storage_policy.py \ test/unit/common/test_utils.py [testenv:py35]