10a8c90248264a8b26970b4473770bcc3df8515fJosh Gao"""Unit tests for the copy module.""" 20a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 30a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport copy 40a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport copy_reg 50a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport weakref 60a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 70a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport unittest 80a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom test import test_support 90a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 100a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass TestCopy(unittest.TestCase): 110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Attempt full line coverage of copy.py from top to bottom 130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_exceptions(self): 150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(copy.Error is copy.error) 160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(issubclass(copy.Error, Exception)) 170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # The copy() method 190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy_basic(self): 210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = 42 220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.copy(x) 230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(x, y) 240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy_copy(self): 260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(object): 270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, foo): 280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.foo = foo 290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __copy__(self): 300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return C(self.foo) 310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C(42) 320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.copy(x) 330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(y.__class__, x.__class__) 340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(y.foo, x.foo) 350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy_registry(self): 370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(object): 380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __new__(cls, foo): 390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj = object.__new__(cls) 400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj.foo = foo 410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return obj 420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def pickle_C(obj): 430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (C, (obj.foo,)) 440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C(42) 450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, copy.copy, x) 460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao copy_reg.pickle(C, pickle_C, C) 470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.copy(x) 480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy_reduce_ex(self): 500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(object): 510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __reduce_ex__(self, proto): 520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return "" 530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __reduce__(self): 540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise test_support.TestFailed, "shouldn't call this" 550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C() 560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.copy(x) 570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y is x) 580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy_reduce(self): 600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(object): 610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __reduce__(self): 620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return "" 630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C() 640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.copy(x) 650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y is x) 660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy_cant(self): 680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(object): 690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __getattribute__(self, name): 700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if name.startswith("__reduce"): 710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise AttributeError, name 720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return object.__getattribute__(self, name) 730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C() 740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(copy.Error, copy.copy, x) 750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Type-specific _copy_xxx() methods 770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy_atomic(self): 790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Classic: 800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class NewStyle(object): 820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def f(): 840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao tests = [None, 42, 2L**100, 3.14, True, False, 1j, 860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "hello", u"hello\u1234", f.func_code, 870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao NewStyle, xrange(10), Classic, max] 880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for x in tests: 890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(copy.copy(x) is x, repr(x)) 900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy_list(self): 920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = [1, 2, 3] 930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(copy.copy(x), x) 940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy_tuple(self): 960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = (1, 2, 3) 970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(copy.copy(x), x) 980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy_dict(self): 1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = {"foo": 1, "bar": 2} 1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(copy.copy(x), x) 1020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy_inst_vanilla(self): 1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C: 1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, foo): 1060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.foo = foo 1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __cmp__(self, other): 1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return cmp(self.foo, other.foo) 1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C(42) 1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(copy.copy(x), x) 1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy_inst_copy(self): 1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C: 1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, foo): 1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.foo = foo 1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __copy__(self): 1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return C(self.foo) 1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __cmp__(self, other): 1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return cmp(self.foo, other.foo) 1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C(42) 1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(copy.copy(x), x) 1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy_inst_getinitargs(self): 1240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C: 1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, foo): 1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.foo = foo 1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __getinitargs__(self): 1280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (self.foo,) 1290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __cmp__(self, other): 1300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return cmp(self.foo, other.foo) 1310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C(42) 1320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(copy.copy(x), x) 1330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy_inst_getstate(self): 1350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C: 1360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, foo): 1370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.foo = foo 1380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __getstate__(self): 1390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return {"foo": self.foo} 1400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __cmp__(self, other): 1410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return cmp(self.foo, other.foo) 1420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C(42) 1430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(copy.copy(x), x) 1440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy_inst_setstate(self): 1460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C: 1470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, foo): 1480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.foo = foo 1490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __setstate__(self, state): 1500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.foo = state["foo"] 1510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __cmp__(self, other): 1520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return cmp(self.foo, other.foo) 1530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C(42) 1540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(copy.copy(x), x) 1550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy_inst_getstate_setstate(self): 1570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C: 1580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, foo): 1590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.foo = foo 1600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __getstate__(self): 1610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.foo 1620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __setstate__(self, state): 1630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.foo = state 1640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __cmp__(self, other): 1650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return cmp(self.foo, other.foo) 1660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C(42) 1670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(copy.copy(x), x) 1680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # The deepcopy() method 1700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_basic(self): 1720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = 42 1730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 1740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(y, x) 1750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_memo(self): 1770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Tests of reflexive objects are under type-specific sections below. 1780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # This tests only repetitions of objects. 1790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = [] 1800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = [x, x] 1810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 1820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(y, x) 1830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y is not x) 1840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y[0] is not x[0]) 1850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y[0] is y[1]) 1860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_issubclass(self): 1880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # XXX Note: there's no way to test the TypeError coming out of 1890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # issubclass() -- this can only happen when an extension 1900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # module defines a "type" that doesn't formally inherit from 1910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # type. 1920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Meta(type): 1930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 1940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C: 1950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao __metaclass__ = Meta 1960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(copy.deepcopy(C), C) 1970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 1980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_deepcopy(self): 1990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(object): 2000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, foo): 2010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.foo = foo 2020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __deepcopy__(self, memo=None): 2030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return C(self.foo) 2040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C(42) 2050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 2060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(y.__class__, x.__class__) 2070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(y.foo, x.foo) 2080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_registry(self): 2100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(object): 2110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __new__(cls, foo): 2120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj = object.__new__(cls) 2130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj.foo = foo 2140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return obj 2150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def pickle_C(obj): 2160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (C, (obj.foo,)) 2170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C(42) 2180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(TypeError, copy.deepcopy, x) 2190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao copy_reg.pickle(C, pickle_C, C) 2200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 2210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_reduce_ex(self): 2230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(object): 2240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __reduce_ex__(self, proto): 2250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return "" 2260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __reduce__(self): 2270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise test_support.TestFailed, "shouldn't call this" 2280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C() 2290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 2300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y is x) 2310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_reduce(self): 2330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(object): 2340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __reduce__(self): 2350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return "" 2360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C() 2370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 2380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y is x) 2390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_cant(self): 2410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(object): 2420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __getattribute__(self, name): 2430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if name.startswith("__reduce"): 2440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise AttributeError, name 2450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return object.__getattribute__(self, name) 2460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C() 2470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(copy.Error, copy.deepcopy, x) 2480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Type-specific _deepcopy_xxx() methods 2500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_atomic(self): 2520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Classic: 2530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 2540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class NewStyle(object): 2550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 2560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def f(): 2570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 2580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao tests = [None, 42, 2L**100, 3.14, True, False, 1j, 2590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao "hello", u"hello\u1234", f.func_code, 2600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao NewStyle, xrange(10), Classic, max] 2610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao for x in tests: 2620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(copy.deepcopy(x) is x, repr(x)) 2630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_list(self): 2650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = [[1, 2], 3] 2660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 2670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(y, x) 2680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(x is not y) 2690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(x[0] is not y[0]) 2700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_reflexive_list(self): 2720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = [] 2730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x.append(x) 2740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 2750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(RuntimeError, cmp, y, x) 2760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y is not x) 2770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y[0] is y) 2780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(y), 1) 2790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_tuple(self): 2810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = ([1, 2], 3) 2820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 2830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(y, x) 2840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(x is not y) 2850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(x[0] is not y[0]) 2860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_reflexive_tuple(self): 2880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = ([],) 2890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x[0].append(x) 2900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 2910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(RuntimeError, cmp, y, x) 2920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y is not x) 2930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y[0] is not x[0]) 2940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y[0][0] is y) 2950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 2960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_dict(self): 2970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = {"foo": [1, 2], "bar": 3} 2980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 2990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(y, x) 3000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(x is not y) 3010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(x["foo"] is not y["foo"]) 3020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_reflexive_dict(self): 3040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = {} 3050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x['foo'] = x 3060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 3070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(RuntimeError, cmp, y, x) 3080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y is not x) 3090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y['foo'] is y) 3100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(y), 1) 3110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_keepalive(self): 3130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao memo = {} 3140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = 42 3150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x, memo) 3160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(memo[id(x)] is x) 3170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_inst_vanilla(self): 3190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C: 3200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, foo): 3210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.foo = foo 3220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __cmp__(self, other): 3230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return cmp(self.foo, other.foo) 3240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C([42]) 3250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 3260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(y, x) 3270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y.foo is not x.foo) 3280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_inst_deepcopy(self): 3300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C: 3310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, foo): 3320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.foo = foo 3330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __deepcopy__(self, memo): 3340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return C(copy.deepcopy(self.foo, memo)) 3350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __cmp__(self, other): 3360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return cmp(self.foo, other.foo) 3370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C([42]) 3380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 3390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(y, x) 3400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y is not x) 3410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y.foo is not x.foo) 3420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_inst_getinitargs(self): 3440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C: 3450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, foo): 3460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.foo = foo 3470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __getinitargs__(self): 3480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (self.foo,) 3490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __cmp__(self, other): 3500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return cmp(self.foo, other.foo) 3510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C([42]) 3520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 3530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(y, x) 3540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y is not x) 3550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y.foo is not x.foo) 3560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_inst_getstate(self): 3580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C: 3590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, foo): 3600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.foo = foo 3610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __getstate__(self): 3620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return {"foo": self.foo} 3630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __cmp__(self, other): 3640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return cmp(self.foo, other.foo) 3650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C([42]) 3660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 3670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(y, x) 3680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y is not x) 3690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y.foo is not x.foo) 3700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_inst_setstate(self): 3720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C: 3730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, foo): 3740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.foo = foo 3750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __setstate__(self, state): 3760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.foo = state["foo"] 3770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __cmp__(self, other): 3780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return cmp(self.foo, other.foo) 3790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C([42]) 3800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 3810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(y, x) 3820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y is not x) 3830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y.foo is not x.foo) 3840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 3850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_inst_getstate_setstate(self): 3860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C: 3870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, foo): 3880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.foo = foo 3890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __getstate__(self): 3900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return self.foo 3910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __setstate__(self, state): 3920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.foo = state 3930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __cmp__(self, other): 3940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return cmp(self.foo, other.foo) 3950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C([42]) 3960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 3970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(y, x) 3980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y is not x) 3990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y.foo is not x.foo) 4000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_reflexive_inst(self): 4020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C: 4030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 4040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C() 4050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x.foo = x 4060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 4070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y is not x) 4080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y.foo is y) 4090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # _reconstruct() 4110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_reconstruct_string(self): 4130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(object): 4140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __reduce__(self): 4150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return "" 4160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C() 4170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.copy(x) 4180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y is x) 4190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 4200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y is x) 4210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_reconstruct_nostate(self): 4230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(object): 4240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __reduce__(self): 4250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (C, ()) 4260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C() 4270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x.foo = 42 4280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.copy(x) 4290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y.__class__ is x.__class__) 4300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 4310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y.__class__ is x.__class__) 4320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_reconstruct_state(self): 4340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(object): 4350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __reduce__(self): 4360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (C, (), self.__dict__) 4370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __cmp__(self, other): 4380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return cmp(self.__dict__, other.__dict__) 4390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao __hash__ = None # Silence Py3k warning 4400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C() 4410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x.foo = [42] 4420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.copy(x) 4430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(y, x) 4440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 4450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(y, x) 4460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y.foo is not x.foo) 4470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_reconstruct_state_setstate(self): 4490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(object): 4500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __reduce__(self): 4510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (C, (), self.__dict__) 4520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __setstate__(self, state): 4530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.__dict__.update(state) 4540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __cmp__(self, other): 4550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return cmp(self.__dict__, other.__dict__) 4560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao __hash__ = None # Silence Py3k warning 4570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C() 4580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x.foo = [42] 4590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.copy(x) 4600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(y, x) 4610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 4620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(y, x) 4630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y.foo is not x.foo) 4640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_reconstruct_reflexive(self): 4660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(object): 4670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 4680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C() 4690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x.foo = x 4700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 4710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y is not x) 4720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y.foo is y) 4730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Additions for Python 2.3 and pickle protocol 2 4750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_reduce_4tuple(self): 4770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(list): 4780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __reduce__(self): 4790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (C, (), self.__dict__, iter(self)) 4800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __cmp__(self, other): 4810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (cmp(list(self), list(other)) or 4820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao cmp(self.__dict__, other.__dict__)) 4830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao __hash__ = None # Silence Py3k warning 4840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C([[1, 2], 3]) 4850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.copy(x) 4860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(x, y) 4870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(x is not y) 4880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(x[0] is y[0]) 4890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 4900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(x, y) 4910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(x is not y) 4920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(x[0] is not y[0]) 4930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 4940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_reduce_5tuple(self): 4950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(dict): 4960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __reduce__(self): 4970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (C, (), self.__dict__, None, self.iteritems()) 4980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __cmp__(self, other): 4990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao return (cmp(dict(self), list(dict)) or 5000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao cmp(self.__dict__, other.__dict__)) 5010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao __hash__ = None # Silence Py3k warning 5020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C([("foo", [1, 2]), ("bar", 3)]) 5030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.copy(x) 5040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(x, y) 5050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(x is not y) 5060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(x["foo"] is y["foo"]) 5070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 5080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(x, y) 5090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(x is not y) 5100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(x["foo"] is not y["foo"]) 5110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy_slots(self): 5130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(object): 5140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao __slots__ = ["foo"] 5150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C() 5160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x.foo = [42] 5170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.copy(x) 5180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(x.foo is y.foo) 5190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_slots(self): 5210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(object): 5220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao __slots__ = ["foo"] 5230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C() 5240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x.foo = [42] 5250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 5260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(x.foo, y.foo) 5270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(x.foo is not y.foo) 5280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_dict_subclass(self): 5300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(dict): 5310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, d=None): 5320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if not d: 5330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao d = {} 5340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._keys = list(d.keys()) 5350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dict.__init__(self, d) 5360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __setitem__(self, key, item): 5370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao dict.__setitem__(self, key, item) 5380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao if key not in self._keys: 5390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._keys.append(key) 5400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C(d={'foo':0}) 5410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 5420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(x, y) 5430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(x._keys, y._keys) 5440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(x is not y) 5450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x['bar'] = 1 5460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotEqual(x, y) 5470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotEqual(x._keys, y._keys) 5480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy_list_subclass(self): 5500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(list): 5510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 5520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C([[1, 2], 3]) 5530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x.foo = [4, 5] 5540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.copy(x) 5550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(list(x), list(y)) 5560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(x.foo, y.foo) 5570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(x[0] is y[0]) 5580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(x.foo is y.foo) 5590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_list_subclass(self): 5610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(list): 5620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 5630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C([[1, 2], 3]) 5640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x.foo = [4, 5] 5650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 5660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(list(x), list(y)) 5670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(x.foo, y.foo) 5680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(x[0] is not y[0]) 5690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(x.foo is not y.foo) 5700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy_tuple_subclass(self): 5720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(tuple): 5730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 5740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C([1, 2, 3]) 5750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(tuple(x), (1, 2, 3)) 5760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.copy(x) 5770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(tuple(y), (1, 2, 3)) 5780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_tuple_subclass(self): 5800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(tuple): 5810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 5820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = C([[1, 2], 3]) 5830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(tuple(x), ([1, 2], 3)) 5840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = copy.deepcopy(x) 5850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(tuple(y), ([1, 2], 3)) 5860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(x is not y) 5870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(x[0] is not y[0]) 5880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_getstate_exc(self): 5900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class EvilState(object): 5910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __getstate__(self): 5920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao raise ValueError, "ain't got no stickin' state" 5930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertRaises(ValueError, copy.copy, EvilState()) 5940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 5950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy_function(self): 5960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(copy.copy(global_foo), global_foo) 5970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def foo(x, y): return x+y 5980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(copy.copy(foo), foo) 5990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao bar = lambda: None 6000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(copy.copy(bar), bar) 6010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_function(self): 6030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(copy.deepcopy(global_foo), global_foo) 6040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def foo(x, y): return x+y 6050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(copy.deepcopy(foo), foo) 6060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao bar = lambda: None 6070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(copy.deepcopy(bar), bar) 6080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _check_weakref(self, _copy): 6100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(object): 6110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 6120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao obj = C() 6130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x = weakref.ref(obj) 6140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = _copy(x) 6150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y is x) 6160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao del obj 6170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao y = _copy(x) 6180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y is x) 6190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy_weakref(self): 6210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._check_weakref(copy.copy) 6220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_weakref(self): 6240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._check_weakref(copy.deepcopy) 6250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def _check_copy_weakdict(self, _dicttype): 6270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(object): 6280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 6290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a, b, c, d = [C() for i in xrange(4)] 6300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao u = _dicttype() 6310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao u[a] = b 6320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao u[c] = d 6330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao v = copy.copy(u) 6340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertFalse(v is u) 6350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(v, u) 6360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(v[a], b) 6370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(v[c], d) 6380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(v), 2) 6390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao del c, d 6400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(v), 1) 6410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao x, y = C(), C() 6420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # The underlying containers are decoupled 6430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao v[x] = y 6440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotIn(x, u) 6450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy_weakkeydict(self): 6470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._check_copy_weakdict(weakref.WeakKeyDictionary) 6480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_copy_weakvaluedict(self): 6500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self._check_copy_weakdict(weakref.WeakValueDictionary) 6510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_weakkeydict(self): 6530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(object): 6540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, i): 6550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.i = i 6560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a, b, c, d = [C(i) for i in xrange(4)] 6570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao u = weakref.WeakKeyDictionary() 6580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao u[a] = b 6590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao u[c] = d 6600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Keys aren't copied, values are 6610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao v = copy.deepcopy(u) 6620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotEqual(v, u) 6630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(v), 2) 6640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertFalse(v[a] is b) 6650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertFalse(v[c] is d) 6660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(v[a].i, b.i) 6670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(v[c].i, d.i) 6680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao del c 6690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(v), 1) 6700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_weakvaluedict(self): 6720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class C(object): 6730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def __init__(self, i): 6740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.i = i 6750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao a, b, c, d = [C(i) for i in xrange(4)] 6760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao u = weakref.WeakValueDictionary() 6770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao u[a] = b 6780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao u[c] = d 6790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao # Keys are copied, values aren't 6800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao v = copy.deepcopy(u) 6810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertNotEqual(v, u) 6820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(v), 2) 6830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao (x, y), (z, t) = sorted(v.items(), key=lambda pair: pair[0].i) 6840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertFalse(x is a) 6850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(x.i, a.i) 6860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(y is b) 6870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertFalse(z is c) 6880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(z.i, c.i) 6890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(t is d) 6900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao del x, y, z, t 6910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao del d 6920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(len(v), 1) 6930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 6940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def test_deepcopy_bound_method(self): 6950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao class Foo(object): 6960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao def m(self): 6970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao pass 6980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao f = Foo() 6990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao f.b = f.m 7000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao g = copy.deepcopy(f) 7010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertEqual(g.m, g.b) 7020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao self.assertTrue(g.b.im_self is g) 7030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao g.b() 7040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7060a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef global_foo(x, y): return x+y 7070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7080a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef test_main(): 7090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test_support.run_unittest(TestCopy) 7100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao 7110a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif __name__ == "__main__": 7120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao test_main() 713