123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- """
- This file was modified from CPython.
- Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
- 2011, 2012, 2013, 2014, 2015, 2016 Python Software Foundation; All Rights Reserved
- """
- import copy, unittest
- from operator import le, lt, ge, gt, eq, ne
- class C:
- def __init__(self, foo):
- self.foo = foo
- def __copy__(self):
- return C(self.foo)
- class ReduceEx(object):
- def __reduce_ex__(self, proto):
- c.append(1)
- return ""
- def __reduce__(self):
- self.fail("shouldn't call this")
- c = []
- class Reduce(object):
- def __reduce__(self):
- d.append(1)
- return ""
- d = []
- class InstVan:
- def __init__(self, foo):
- self.foo = foo
- # def __eq__(self, other):
- # return self.foo == other.foo
- class Copy_eq:
- def __init__(self, foo):
- self.foo = foo
- def __copy__(self):
- return C(self.foo)
- def __eq__(self, other):
- return self.foo == other.foo
- class User_def:
- def __init__(self, name):
- self.name = name
- def rename(self,newname):
- self.name = newname
- class NewArgs(int):
- def __new__(cls, foo):
- self = int.__new__(cls)
- self.foo = foo
- return self
- def __getnewargs__(self):
- return self.foo,
- def __eq__(self, other):
- return self.foo == other.foo
- class TestCopy(unittest.TestCase):
- def test_exceptions(self):
- self.assertIs(copy.Error, copy.error)
- self.assertTrue(issubclass(copy.Error, Exception))
- # The copy() method
- def test_copy_basic(self):
- x = 42
- y = copy.copy(x)
- self.assertEqual(x, y)
- def test_copy_copy(self):
- x = C(42)
- y = copy.copy(x)
- self.assertEqual(y.__class__, x.__class__)
- self.assertEqual(y.foo, x.foo)
- def test_copy_inst(self):
- x = User_def("One")
- y = copy.copy(x)
- self.assertEqual(x.name, "One")
- self.assertFalse(x==y)
- self.assertFalse(x is y)
- self.assertTrue(x.name is y.name)
- x.rename("Two")
- self.assertFalse(x==y)
- self.assertFalse(x is y)
- self.assertEqual(x.name, "Two")
- self.assertEqual(y.name, "One")
- def test_copy_cant(self):
- class Cant:
- def __getattribute__(self, name):
- pass
- # if name.startswith("__reduce"):
- # raise AttributeError(name)
- # return object.__getattribute__(self, name)
- x = Cant()
- self.assertRaises(TypeError, copy.copy, x)
- def test_copy_atomic(self):
- class Classic:
- pass
- def f():
- pass
- # class WithMetaclass(metaclass=abc.ABCMeta):
- # pass
- tests = [None, 42, 3.14, True, False,
- "hello", "hello\u1234", Classic]
- #None, .../, NotImplemented,
- # 42, 2**100, 3.14, True, False, 1j,
- # "hello", "hello\u1234", f.__code__,
- # b"world", bytes(range(256)), slice(1, 10, 2),
- # NewStyle, Classic, max, WithMetaclass]
- for x in tests:
- self.assertIs(copy.copy(x), x)
- def test_copy_list(self):
- x = [1, 2, 3]
- y = copy.copy(x)
- self.assertEqual(y, x)
- self.assertIsNot(y, x)
- x = []
- y = copy.copy(x)
- self.assertEqual(y, x)
- self.assertIsNot(y, x)
- def test_copy_mixed_set(self):
- class FooSet:
- def bar(self):
- pass
- a = set([1,2,3])
- b = copy.deepcopy(a)
- a = set([1,2,3])
- b = copy.deepcopy(a)
- self.assertTrue(a == b)
- self.assertFalse(a is b)
- mixed = set([(1,2), FooSet.bar])
- mixedcopy = copy.deepcopy(mixed)
- self.assertTrue(mixed == mixedcopy)
- self.assertFalse(mixed is mixedcopy)
- mixed.add(9)
- mixedcopy2 = copy.deepcopy(mixed)
- self.assertTrue(mixed == mixedcopy2)
- self.assertFalse(mixed is mixedcopy2)
- mixed.add(9)
- self.assertFalse(mixed == mixedcopy)
- mixedcopy2 = copy.copy(mixed)
- self.assertTrue(mixed == mixedcopy2)
- def test_copy_tuple(self):
- x = (1, 2, 3)
- self.assertIs(copy.copy(x), x)
- x = ()
- self.assertIs(copy.copy(x), x)
- x = (1, 2, 3, [])
- self.assertIs(copy.copy(x), x)
- # def test_copy_registry(self):
- # class C:
- # def __new__(cls, foo):
- # obj = object.__new__(cls)
- # obj.foo = foo
- # return obj
- # def pickle_C(obj):
- # return (C, (obj.foo,))
- # x = C(42)
- # self.assertRaises(TypeError, copy.copy, x)
- # copyreg.pickle(C, pickle_C, C)
- # y = copy.copy(x)
- def test_copy_reduce_ex(self):
- x = ReduceEx()
- y = copy.copy(x)
- self.assertIs(y, x)
- self.assertEqual(c, [1])
- def test_copy_reduce(self):
- x = Reduce()
- y = copy.copy(x)
- self.assertIs(y, x)
- self.assertEqual(d, [1])
- def test_copy_list(self):
- x = [1, 2, 3]
- y = copy.copy(x)
- self.assertEqual(y, x)
- self.assertIsNot(y, x)
- x = []
- y = copy.copy(x)
- self.assertEqual(y, x)
- self.assertIsNot(y, x)
- def test_copy_tuple(self):
- x = (1, 2, 3)
- self.assertIs(copy.copy(x), x)
- x = ()
- self.assertIs(copy.copy(x), x)
- x = (1, 2, 3, [])
- self.assertIs(copy.copy(x), x)
- def test_copy_dict(self):
- x = {"foo": 1, "bar": 2}
- y = copy.copy(x)
- self.assertEqual(y, x)
- self.assertIsNot(y, x)
- x = {}
- y = copy.copy(x)
- self.assertEqual(y, x)
- self.assertIsNot(y, x)
- def test_copy_set(self):
- x = {1, 2, 3}
- y = copy.copy(x)
- self.assertEqual(y, x)
- self.assertIsNot(y, x)
- x = set()
- y = copy.copy(x)
- self.assertEqual(y, x)
- self.assertIsNot(y, x)
- # # def test_copy_frozenset(self):
- # # x = frozenset({1, 2, 3})
- # # self.assertIs(copy.copy(x), x)
- # # x = frozenset()
- # # self.assertIs(copy.copy(x), x)
-
- # # def test_copy_bytearray(self):
- # # x = bytearray(b'abc')
- # # y = copy.copy(x)
- # # self.assertEqual(y, x)
- # # self.assertIsNot(y, x)
- # # x = bytearray()
- # # y = copy.copy(x)
- # # self.assertEqual(y, x)
- # # self.assertIsNot(y, x)
- def test_copy_inst_vanilla(self):
- x = InstVan(42)
- # print x == copy.copy(x)
- self.assertFalse(copy.copy(x) == x)
- def test_copy_inst_copy(self):
- x = Copy_eq(42)
- self.assertEqual(copy.copy(x), x)
-
- def test_copy_inst_getinitargs(self):
- class C:
- def __init__(self, foo):
- self.foo = foo
- def __getinitargs__(self):
- return (self.foo,)
- def __eq__(self, other):
- return self.foo == other.foo
- x = C(42)
- self.assertRaises(NotImplementedError, copy.copy, x)
- # self.assertEqual(copy.copy(x), x)
- # def test_copy_inst_getnewargs(self):
- # # class NewArgs(int):
- # # def __new__(cls, foo):
- # # self = int.__new__(cls)
- # # self.foo = foo
- # # return self
- # # def __getnewargs__(self):
- # # return self.foo,
- # # def __eq__(self, other):
- # # return self.foo == other.foo
- # x = NewArgs(42)
- # y = copy.copy(x)
- # self.assertIsInstance(y, NewArgs)
- # self.assertEqual(y, x)
- # self.assertIsNot(y, x)
- # self.assertEqual(y.foo, x.foo)
- # def test_copy_inst_getnewargs_ex(self):
- # class C(int):
- # def __new__(cls, *, foo):
- # self = int.__new__(cls)
- # self.foo = foo
- # return self
- # def __getnewargs_ex__(self):
- # return (), {'foo': self.foo}
- # def __eq__(self, other):
- # return self.foo == other.foo
- # x = C(foo=42)
- # y = copy.copy(x)
- # self.assertIsInstance(y, C)
- # self.assertEqual(y, x)
- # self.assertIsNot(y, x)
- # self.assertEqual(y.foo, x.foo)
- def test_copy_inst_getstate(self):
- class C:
- def __init__(self, foo):
- self.foo = foo
- def __getstate__(self):
- return {"foo": self.foo}
- def __eq__(self, other):
- return self.foo == other.foo
- x = C(42)
- self.assertRaises(NotImplementedError, copy.copy, x)
- # self.assertEqual(copy.copy(x), x)
- def test_copy_inst_setstate(self):
- class C:
- def __init__(self, foo):
- self.foo = foo
- def __setstate__(self, state):
- self.foo = state["foo"]
- def __eq__(self, other):
- return self.foo == other.foo
- x = C(42)
- self.assertRaises(NotImplementedError, copy.copy, x)
- # self.assertEqual(copy.copy(x), x)
- def test_copy_inst_getstate_setstate(self):
- class C:
- def __init__(self, foo):
- self.foo = foo
- def __getstate__(self):
- return self.foo
- def __setstate__(self, state):
- self.foo = state
- def __eq__(self, other):
- return self.foo == other.foo
- x = C(42)
- self.assertRaises(NotImplementedError, copy.copy, x)
- # self.assertEqual(copy.copy(x), x)
- # # State with boolean value is false (issue #25718)
- # x = C(0.0)
- # self.assertEqual(copy.copy(x), x)
- if __name__ == "__main__":
- unittest.main()
|