diff options
Diffstat (limited to 'Lib/test/test_copy.py')
-rw-r--r-- | Lib/test/test_copy.py | 138 |
1 files changed, 72 insertions, 66 deletions
diff --git a/Lib/test/test_copy.py b/Lib/test/test_copy.py index 6b64f10112e..a84c109b87b 100644 --- a/Lib/test/test_copy.py +++ b/Lib/test/test_copy.py @@ -1,11 +1,16 @@ """Unit tests for the copy module.""" import copy -import copy_reg +import copyreg import weakref +from operator import le, lt, ge, gt, eq, ne import unittest -from test import test_support +from test import support + +order_comparisons = le, lt, ge, gt +equality_comparisons = eq, ne +comparisons = order_comparisons + equality_comparisons class TestCopy(unittest.TestCase): @@ -43,7 +48,7 @@ class TestCopy(unittest.TestCase): return (C, (obj.foo,)) x = C(42) self.assertRaises(TypeError, copy.copy, x) - copy_reg.pickle(C, pickle_C, C) + copyreg.pickle(C, pickle_C, C) y = copy.copy(x) def test_copy_reduce_ex(self): @@ -51,7 +56,7 @@ class TestCopy(unittest.TestCase): def __reduce_ex__(self, proto): return "" def __reduce__(self): - raise test_support.TestFailed, "shouldn't call this" + raise support.TestFailed("shouldn't call this") x = C() y = copy.copy(x) self.assertTrue(y is x) @@ -68,7 +73,7 @@ class TestCopy(unittest.TestCase): class C(object): def __getattribute__(self, name): if name.startswith("__reduce"): - raise AttributeError, name + raise AttributeError(name) return object.__getattribute__(self, name) x = C() self.assertRaises(copy.Error, copy.copy, x) @@ -82,9 +87,9 @@ class TestCopy(unittest.TestCase): pass def f(): pass - tests = [None, 42, 2L**100, 3.14, True, False, 1j, - "hello", u"hello\u1234", f.func_code, - NewStyle, xrange(10), Classic, max] + tests = [None, 42, 2**100, 3.14, True, False, 1j, + "hello", "hello\u1234", f.__code__, + NewStyle, range(10), Classic, max] for x in tests: self.assertTrue(copy.copy(x) is x, repr(x)) @@ -104,8 +109,8 @@ class TestCopy(unittest.TestCase): class C: def __init__(self, foo): self.foo = foo - def __cmp__(self, other): - return cmp(self.foo, other.foo) + def __eq__(self, other): + return self.foo == other.foo x = C(42) self.assertEqual(copy.copy(x), x) @@ -115,8 +120,8 @@ class TestCopy(unittest.TestCase): self.foo = foo def __copy__(self): return C(self.foo) - def __cmp__(self, other): - return cmp(self.foo, other.foo) + def __eq__(self, other): + return self.foo == other.foo x = C(42) self.assertEqual(copy.copy(x), x) @@ -126,8 +131,8 @@ class TestCopy(unittest.TestCase): self.foo = foo def __getinitargs__(self): return (self.foo,) - def __cmp__(self, other): - return cmp(self.foo, other.foo) + def __eq__(self, other): + return self.foo == other.foo x = C(42) self.assertEqual(copy.copy(x), x) @@ -137,8 +142,8 @@ class TestCopy(unittest.TestCase): self.foo = foo def __getstate__(self): return {"foo": self.foo} - def __cmp__(self, other): - return cmp(self.foo, other.foo) + def __eq__(self, other): + return self.foo == other.foo x = C(42) self.assertEqual(copy.copy(x), x) @@ -148,8 +153,8 @@ class TestCopy(unittest.TestCase): self.foo = foo def __setstate__(self, state): self.foo = state["foo"] - def __cmp__(self, other): - return cmp(self.foo, other.foo) + def __eq__(self, other): + return self.foo == other.foo x = C(42) self.assertEqual(copy.copy(x), x) @@ -161,8 +166,8 @@ class TestCopy(unittest.TestCase): return self.foo def __setstate__(self, state): self.foo = state - def __cmp__(self, other): - return cmp(self.foo, other.foo) + def __eq__(self, other): + return self.foo == other.foo x = C(42) self.assertEqual(copy.copy(x), x) @@ -191,8 +196,8 @@ class TestCopy(unittest.TestCase): # type. class Meta(type): pass - class C: - __metaclass__ = Meta + class C(metaclass=Meta): + pass self.assertEqual(copy.deepcopy(C), C) def test_deepcopy_deepcopy(self): @@ -216,7 +221,7 @@ class TestCopy(unittest.TestCase): return (C, (obj.foo,)) x = C(42) self.assertRaises(TypeError, copy.deepcopy, x) - copy_reg.pickle(C, pickle_C, C) + copyreg.pickle(C, pickle_C, C) y = copy.deepcopy(x) def test_deepcopy_reduce_ex(self): @@ -224,7 +229,7 @@ class TestCopy(unittest.TestCase): def __reduce_ex__(self, proto): return "" def __reduce__(self): - raise test_support.TestFailed, "shouldn't call this" + raise support.TestFailed("shouldn't call this") x = C() y = copy.deepcopy(x) self.assertTrue(y is x) @@ -241,7 +246,7 @@ class TestCopy(unittest.TestCase): class C(object): def __getattribute__(self, name): if name.startswith("__reduce"): - raise AttributeError, name + raise AttributeError(name) return object.__getattribute__(self, name) x = C() self.assertRaises(copy.Error, copy.deepcopy, x) @@ -255,9 +260,9 @@ class TestCopy(unittest.TestCase): pass def f(): pass - tests = [None, 42, 2L**100, 3.14, True, False, 1j, - "hello", u"hello\u1234", f.func_code, - NewStyle, xrange(10), Classic, max] + tests = [None, 42, 2**100, 3.14, True, False, 1j, + "hello", "hello\u1234", f.__code__, + NewStyle, range(10), Classic, max] for x in tests: self.assertTrue(copy.deepcopy(x) is x, repr(x)) @@ -272,7 +277,8 @@ class TestCopy(unittest.TestCase): x = [] x.append(x) y = copy.deepcopy(x) - self.assertRaises(RuntimeError, cmp, y, x) + for op in comparisons: + self.assertRaises(RuntimeError, op, y, x) self.assertTrue(y is not x) self.assertTrue(y[0] is y) self.assertEqual(len(y), 1) @@ -288,7 +294,8 @@ class TestCopy(unittest.TestCase): x = ([],) x[0].append(x) y = copy.deepcopy(x) - self.assertRaises(RuntimeError, cmp, y, x) + for op in comparisons: + self.assertRaises(RuntimeError, op, y, x) self.assertTrue(y is not x) self.assertTrue(y[0] is not x[0]) self.assertTrue(y[0][0] is y) @@ -304,7 +311,10 @@ class TestCopy(unittest.TestCase): x = {} x['foo'] = x y = copy.deepcopy(x) - self.assertRaises(RuntimeError, cmp, y, x) + for op in order_comparisons: + self.assertRaises(TypeError, op, y, x) + for op in equality_comparisons: + self.assertRaises(RuntimeError, op, y, x) self.assertTrue(y is not x) self.assertTrue(y['foo'] is y) self.assertEqual(len(y), 1) @@ -319,8 +329,8 @@ class TestCopy(unittest.TestCase): class C: def __init__(self, foo): self.foo = foo - def __cmp__(self, other): - return cmp(self.foo, other.foo) + def __eq__(self, other): + return self.foo == other.foo x = C([42]) y = copy.deepcopy(x) self.assertEqual(y, x) @@ -332,8 +342,8 @@ class TestCopy(unittest.TestCase): self.foo = foo def __deepcopy__(self, memo): return C(copy.deepcopy(self.foo, memo)) - def __cmp__(self, other): - return cmp(self.foo, other.foo) + def __eq__(self, other): + return self.foo == other.foo x = C([42]) y = copy.deepcopy(x) self.assertEqual(y, x) @@ -346,8 +356,8 @@ class TestCopy(unittest.TestCase): self.foo = foo def __getinitargs__(self): return (self.foo,) - def __cmp__(self, other): - return cmp(self.foo, other.foo) + def __eq__(self, other): + return self.foo == other.foo x = C([42]) y = copy.deepcopy(x) self.assertEqual(y, x) @@ -360,8 +370,8 @@ class TestCopy(unittest.TestCase): self.foo = foo def __getstate__(self): return {"foo": self.foo} - def __cmp__(self, other): - return cmp(self.foo, other.foo) + def __eq__(self, other): + return self.foo == other.foo x = C([42]) y = copy.deepcopy(x) self.assertEqual(y, x) @@ -374,8 +384,8 @@ class TestCopy(unittest.TestCase): self.foo = foo def __setstate__(self, state): self.foo = state["foo"] - def __cmp__(self, other): - return cmp(self.foo, other.foo) + def __eq__(self, other): + return self.foo == other.foo x = C([42]) y = copy.deepcopy(x) self.assertEqual(y, x) @@ -390,8 +400,8 @@ class TestCopy(unittest.TestCase): return self.foo def __setstate__(self, state): self.foo = state - def __cmp__(self, other): - return cmp(self.foo, other.foo) + def __eq__(self, other): + return self.foo == other.foo x = C([42]) y = copy.deepcopy(x) self.assertEqual(y, x) @@ -434,9 +444,8 @@ class TestCopy(unittest.TestCase): class C(object): def __reduce__(self): return (C, (), self.__dict__) - def __cmp__(self, other): - return cmp(self.__dict__, other.__dict__) - __hash__ = None # Silence Py3k warning + def __eq__(self, other): + return self.__dict__ == other.__dict__ x = C() x.foo = [42] y = copy.copy(x) @@ -451,9 +460,8 @@ class TestCopy(unittest.TestCase): return (C, (), self.__dict__) def __setstate__(self, state): self.__dict__.update(state) - def __cmp__(self, other): - return cmp(self.__dict__, other.__dict__) - __hash__ = None # Silence Py3k warning + def __eq__(self, other): + return self.__dict__ == other.__dict__ x = C() x.foo = [42] y = copy.copy(x) @@ -477,10 +485,9 @@ class TestCopy(unittest.TestCase): class C(list): def __reduce__(self): return (C, (), self.__dict__, iter(self)) - def __cmp__(self, other): - return (cmp(list(self), list(other)) or - cmp(self.__dict__, other.__dict__)) - __hash__ = None # Silence Py3k warning + def __eq__(self, other): + return (list(self) == list(other) and + self.__dict__ == other.__dict__) x = C([[1, 2], 3]) y = copy.copy(x) self.assertEqual(x, y) @@ -494,11 +501,10 @@ class TestCopy(unittest.TestCase): def test_reduce_5tuple(self): class C(dict): def __reduce__(self): - return (C, (), self.__dict__, None, self.iteritems()) - def __cmp__(self, other): - return (cmp(dict(self), list(dict)) or - cmp(self.__dict__, other.__dict__)) - __hash__ = None # Silence Py3k warning + return (C, (), self.__dict__, None, self.items()) + def __eq__(self, other): + return (dict(self) == dict(other) and + self.__dict__ == other.__dict__) x = C([("foo", [1, 2]), ("bar", 3)]) y = copy.copy(x) self.assertEqual(x, y) @@ -532,9 +538,9 @@ class TestCopy(unittest.TestCase): if not d: d = {} self._keys = list(d.keys()) - dict.__init__(self, d) + super().__init__(d) def __setitem__(self, key, item): - dict.__setitem__(self, key, item) + super().__setitem__(key, item) if key not in self._keys: self._keys.append(key) x = C(d={'foo':0}) @@ -589,7 +595,7 @@ class TestCopy(unittest.TestCase): def test_getstate_exc(self): class EvilState(object): def __getstate__(self): - raise ValueError, "ain't got no stickin' state" + raise ValueError("ain't got no stickin' state") self.assertRaises(ValueError, copy.copy, EvilState()) def test_copy_function(self): @@ -626,7 +632,7 @@ class TestCopy(unittest.TestCase): def _check_copy_weakdict(self, _dicttype): class C(object): pass - a, b, c, d = [C() for i in xrange(4)] + a, b, c, d = [C() for i in range(4)] u = _dicttype() u[a] = b u[c] = d @@ -653,7 +659,7 @@ class TestCopy(unittest.TestCase): class C(object): def __init__(self, i): self.i = i - a, b, c, d = [C(i) for i in xrange(4)] + a, b, c, d = [C(i) for i in range(4)] u = weakref.WeakKeyDictionary() u[a] = b u[c] = d @@ -672,7 +678,7 @@ class TestCopy(unittest.TestCase): class C(object): def __init__(self, i): self.i = i - a, b, c, d = [C(i) for i in xrange(4)] + a, b, c, d = [C(i) for i in range(4)] u = weakref.WeakValueDictionary() u[a] = b u[c] = d @@ -699,14 +705,14 @@ class TestCopy(unittest.TestCase): f.b = f.m g = copy.deepcopy(f) self.assertEqual(g.m, g.b) - self.assertTrue(g.b.im_self is g) + self.assertTrue(g.b.__self__ is g) g.b() def global_foo(x, y): return x+y def test_main(): - test_support.run_unittest(TestCopy) + support.run_unittest(TestCopy) if __name__ == "__main__": test_main() |