blob: e450f70d07f55f82fd9a829651c547db7db78a85 [file] [log] [blame]
Guido van Rossum581cb932003-02-06 17:52:15 +00001"""Unit tests for the copy module."""
2
Guido van Rossum581cb932003-02-06 17:52:15 +00003import copy
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +00004import copyreg
Antoine Pitrou6e610062009-05-15 17:04:50 +00005import weakref
Mark Dickinsona56c4672009-01-27 18:17:45 +00006from operator import le, lt, ge, gt, eq, ne
Antoine Pitrou6e610062009-05-15 17:04:50 +00007
Guido van Rossum581cb932003-02-06 17:52:15 +00008import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00009from test import support
Guido van Rossum581cb932003-02-06 17:52:15 +000010
Mark Dickinsona56c4672009-01-27 18:17:45 +000011order_comparisons = le, lt, ge, gt
12equality_comparisons = eq, ne
13comparisons = order_comparisons + equality_comparisons
14
Guido van Rossum581cb932003-02-06 17:52:15 +000015class TestCopy(unittest.TestCase):
16
17 # Attempt full line coverage of copy.py from top to bottom
18
19 def test_exceptions(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000020 self.assertTrue(copy.Error is copy.error)
21 self.assertTrue(issubclass(copy.Error, Exception))
Guido van Rossum581cb932003-02-06 17:52:15 +000022
23 # The copy() method
24
25 def test_copy_basic(self):
26 x = 42
27 y = copy.copy(x)
28 self.assertEqual(x, y)
29
30 def test_copy_copy(self):
31 class C(object):
32 def __init__(self, foo):
33 self.foo = foo
34 def __copy__(self):
35 return C(self.foo)
36 x = C(42)
37 y = copy.copy(x)
38 self.assertEqual(y.__class__, x.__class__)
39 self.assertEqual(y.foo, x.foo)
40
Guido van Rossumc06e3ac2003-02-07 17:30:18 +000041 def test_copy_registry(self):
42 class C(object):
43 def __new__(cls, foo):
44 obj = object.__new__(cls)
45 obj.foo = foo
46 return obj
47 def pickle_C(obj):
48 return (C, (obj.foo,))
49 x = C(42)
50 self.assertRaises(TypeError, copy.copy, x)
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +000051 copyreg.pickle(C, pickle_C, C)
Guido van Rossumc06e3ac2003-02-07 17:30:18 +000052 y = copy.copy(x)
53
Guido van Rossume6908832003-02-19 01:19:28 +000054 def test_copy_reduce_ex(self):
55 class C(object):
56 def __reduce_ex__(self, proto):
57 return ""
58 def __reduce__(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +000059 raise support.TestFailed("shouldn't call this")
Guido van Rossume6908832003-02-19 01:19:28 +000060 x = C()
61 y = copy.copy(x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000062 self.assertTrue(y is x)
Guido van Rossume6908832003-02-19 01:19:28 +000063
Guido van Rossum581cb932003-02-06 17:52:15 +000064 def test_copy_reduce(self):
65 class C(object):
66 def __reduce__(self):
67 return ""
68 x = C()
69 y = copy.copy(x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000070 self.assertTrue(y is x)
Guido van Rossum581cb932003-02-06 17:52:15 +000071
72 def test_copy_cant(self):
Guido van Rossume6908832003-02-19 01:19:28 +000073 class C(object):
Guido van Rossum581cb932003-02-06 17:52:15 +000074 def __getattribute__(self, name):
Guido van Rossume6908832003-02-19 01:19:28 +000075 if name.startswith("__reduce"):
Collin Winter3add4d72007-08-29 23:37:32 +000076 raise AttributeError(name)
Guido van Rossum581cb932003-02-06 17:52:15 +000077 return object.__getattribute__(self, name)
78 x = C()
79 self.assertRaises(copy.Error, copy.copy, x)
80
81 # Type-specific _copy_xxx() methods
82
83 def test_copy_atomic(self):
84 class Classic:
85 pass
86 class NewStyle(object):
87 pass
88 def f():
89 pass
Guido van Rossume2a383d2007-01-15 16:59:06 +000090 tests = [None, 42, 2**100, 3.14, True, False, 1j,
Guido van Rossumef87d6e2007-05-02 19:09:54 +000091 "hello", "hello\u1234", f.__code__,
Guido van Rossum805365e2007-05-07 22:24:25 +000092 NewStyle, range(10), Classic, max]
Guido van Rossum581cb932003-02-06 17:52:15 +000093 for x in tests:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000094 self.assertTrue(copy.copy(x) is x, repr(x))
Guido van Rossum581cb932003-02-06 17:52:15 +000095
96 def test_copy_list(self):
97 x = [1, 2, 3]
98 self.assertEqual(copy.copy(x), x)
99
100 def test_copy_tuple(self):
101 x = (1, 2, 3)
102 self.assertEqual(copy.copy(x), x)
103
104 def test_copy_dict(self):
105 x = {"foo": 1, "bar": 2}
106 self.assertEqual(copy.copy(x), x)
107
108 def test_copy_inst_vanilla(self):
109 class C:
110 def __init__(self, foo):
111 self.foo = foo
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000112 def __eq__(self, other):
113 return self.foo == other.foo
Guido van Rossum581cb932003-02-06 17:52:15 +0000114 x = C(42)
115 self.assertEqual(copy.copy(x), x)
116
117 def test_copy_inst_copy(self):
118 class C:
119 def __init__(self, foo):
120 self.foo = foo
121 def __copy__(self):
122 return C(self.foo)
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000123 def __eq__(self, other):
124 return self.foo == other.foo
Guido van Rossum581cb932003-02-06 17:52:15 +0000125 x = C(42)
126 self.assertEqual(copy.copy(x), x)
127
128 def test_copy_inst_getinitargs(self):
129 class C:
130 def __init__(self, foo):
131 self.foo = foo
132 def __getinitargs__(self):
133 return (self.foo,)
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000134 def __eq__(self, other):
135 return self.foo == other.foo
Guido van Rossum581cb932003-02-06 17:52:15 +0000136 x = C(42)
137 self.assertEqual(copy.copy(x), x)
138
139 def test_copy_inst_getstate(self):
140 class C:
141 def __init__(self, foo):
142 self.foo = foo
143 def __getstate__(self):
144 return {"foo": self.foo}
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000145 def __eq__(self, other):
146 return self.foo == other.foo
Guido van Rossum581cb932003-02-06 17:52:15 +0000147 x = C(42)
148 self.assertEqual(copy.copy(x), x)
149
150 def test_copy_inst_setstate(self):
151 class C:
152 def __init__(self, foo):
153 self.foo = foo
154 def __setstate__(self, state):
155 self.foo = state["foo"]
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000156 def __eq__(self, other):
157 return self.foo == other.foo
Guido van Rossum581cb932003-02-06 17:52:15 +0000158 x = C(42)
159 self.assertEqual(copy.copy(x), x)
160
161 def test_copy_inst_getstate_setstate(self):
162 class C:
163 def __init__(self, foo):
164 self.foo = foo
165 def __getstate__(self):
166 return self.foo
167 def __setstate__(self, state):
168 self.foo = state
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000169 def __eq__(self, other):
170 return self.foo == other.foo
Guido van Rossum581cb932003-02-06 17:52:15 +0000171 x = C(42)
172 self.assertEqual(copy.copy(x), x)
173
174 # The deepcopy() method
175
176 def test_deepcopy_basic(self):
177 x = 42
178 y = copy.deepcopy(x)
179 self.assertEqual(y, x)
180
181 def test_deepcopy_memo(self):
Guido van Rossum99d2c252003-06-13 19:28:47 +0000182 # Tests of reflexive objects are under type-specific sections below.
183 # This tests only repetitions of objects.
Guido van Rossum581cb932003-02-06 17:52:15 +0000184 x = []
Guido van Rossum99d2c252003-06-13 19:28:47 +0000185 x = [x, x]
Guido van Rossum581cb932003-02-06 17:52:15 +0000186 y = copy.deepcopy(x)
187 self.assertEqual(y, x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000188 self.assertTrue(y is not x)
189 self.assertTrue(y[0] is not x[0])
190 self.assertTrue(y[0] is y[1])
Guido van Rossum581cb932003-02-06 17:52:15 +0000191
192 def test_deepcopy_issubclass(self):
193 # XXX Note: there's no way to test the TypeError coming out of
194 # issubclass() -- this can only happen when an extension
195 # module defines a "type" that doesn't formally inherit from
196 # type.
197 class Meta(type):
198 pass
Guido van Rossum52cc1d82007-03-18 15:41:51 +0000199 class C(metaclass=Meta):
200 pass
Guido van Rossum581cb932003-02-06 17:52:15 +0000201 self.assertEqual(copy.deepcopy(C), C)
202
203 def test_deepcopy_deepcopy(self):
204 class C(object):
205 def __init__(self, foo):
206 self.foo = foo
207 def __deepcopy__(self, memo=None):
208 return C(self.foo)
209 x = C(42)
210 y = copy.deepcopy(x)
211 self.assertEqual(y.__class__, x.__class__)
212 self.assertEqual(y.foo, x.foo)
213
Guido van Rossumc06e3ac2003-02-07 17:30:18 +0000214 def test_deepcopy_registry(self):
215 class C(object):
216 def __new__(cls, foo):
217 obj = object.__new__(cls)
218 obj.foo = foo
219 return obj
220 def pickle_C(obj):
221 return (C, (obj.foo,))
222 x = C(42)
223 self.assertRaises(TypeError, copy.deepcopy, x)
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +0000224 copyreg.pickle(C, pickle_C, C)
Guido van Rossumc06e3ac2003-02-07 17:30:18 +0000225 y = copy.deepcopy(x)
226
Guido van Rossume6908832003-02-19 01:19:28 +0000227 def test_deepcopy_reduce_ex(self):
228 class C(object):
229 def __reduce_ex__(self, proto):
230 return ""
231 def __reduce__(self):
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000232 raise support.TestFailed("shouldn't call this")
Guido van Rossume6908832003-02-19 01:19:28 +0000233 x = C()
234 y = copy.deepcopy(x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000235 self.assertTrue(y is x)
Guido van Rossume6908832003-02-19 01:19:28 +0000236
Guido van Rossum581cb932003-02-06 17:52:15 +0000237 def test_deepcopy_reduce(self):
238 class C(object):
239 def __reduce__(self):
240 return ""
241 x = C()
242 y = copy.deepcopy(x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000243 self.assertTrue(y is x)
Guido van Rossum581cb932003-02-06 17:52:15 +0000244
245 def test_deepcopy_cant(self):
Guido van Rossume6908832003-02-19 01:19:28 +0000246 class C(object):
Guido van Rossum581cb932003-02-06 17:52:15 +0000247 def __getattribute__(self, name):
Guido van Rossume6908832003-02-19 01:19:28 +0000248 if name.startswith("__reduce"):
Collin Winter3add4d72007-08-29 23:37:32 +0000249 raise AttributeError(name)
Guido van Rossum581cb932003-02-06 17:52:15 +0000250 return object.__getattribute__(self, name)
251 x = C()
252 self.assertRaises(copy.Error, copy.deepcopy, x)
253
254 # Type-specific _deepcopy_xxx() methods
255
256 def test_deepcopy_atomic(self):
257 class Classic:
258 pass
259 class NewStyle(object):
260 pass
261 def f():
262 pass
Guido van Rossume2a383d2007-01-15 16:59:06 +0000263 tests = [None, 42, 2**100, 3.14, True, False, 1j,
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000264 "hello", "hello\u1234", f.__code__,
Guido van Rossum805365e2007-05-07 22:24:25 +0000265 NewStyle, range(10), Classic, max]
Guido van Rossum581cb932003-02-06 17:52:15 +0000266 for x in tests:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000267 self.assertTrue(copy.deepcopy(x) is x, repr(x))
Guido van Rossum581cb932003-02-06 17:52:15 +0000268
269 def test_deepcopy_list(self):
270 x = [[1, 2], 3]
271 y = copy.deepcopy(x)
272 self.assertEqual(y, x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000273 self.assertTrue(x is not y)
274 self.assertTrue(x[0] is not y[0])
Guido van Rossum581cb932003-02-06 17:52:15 +0000275
Guido van Rossum99d2c252003-06-13 19:28:47 +0000276 def test_deepcopy_reflexive_list(self):
277 x = []
278 x.append(x)
279 y = copy.deepcopy(x)
Mark Dickinsona56c4672009-01-27 18:17:45 +0000280 for op in comparisons:
281 self.assertRaises(RuntimeError, op, y, x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000282 self.assertTrue(y is not x)
283 self.assertTrue(y[0] is y)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000284 self.assertEqual(len(y), 1)
Guido van Rossum99d2c252003-06-13 19:28:47 +0000285
Guido van Rossum581cb932003-02-06 17:52:15 +0000286 def test_deepcopy_tuple(self):
287 x = ([1, 2], 3)
288 y = copy.deepcopy(x)
289 self.assertEqual(y, x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000290 self.assertTrue(x is not y)
291 self.assertTrue(x[0] is not y[0])
Guido van Rossum581cb932003-02-06 17:52:15 +0000292
Guido van Rossum99d2c252003-06-13 19:28:47 +0000293 def test_deepcopy_reflexive_tuple(self):
294 x = ([],)
295 x[0].append(x)
296 y = copy.deepcopy(x)
Mark Dickinsona56c4672009-01-27 18:17:45 +0000297 for op in comparisons:
298 self.assertRaises(RuntimeError, op, y, x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000299 self.assertTrue(y is not x)
300 self.assertTrue(y[0] is not x[0])
301 self.assertTrue(y[0][0] is y)
Guido van Rossum99d2c252003-06-13 19:28:47 +0000302
Guido van Rossum581cb932003-02-06 17:52:15 +0000303 def test_deepcopy_dict(self):
304 x = {"foo": [1, 2], "bar": 3}
305 y = copy.deepcopy(x)
306 self.assertEqual(y, x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000307 self.assertTrue(x is not y)
308 self.assertTrue(x["foo"] is not y["foo"])
Guido van Rossum581cb932003-02-06 17:52:15 +0000309
Guido van Rossum99d2c252003-06-13 19:28:47 +0000310 def test_deepcopy_reflexive_dict(self):
311 x = {}
312 x['foo'] = x
313 y = copy.deepcopy(x)
Mark Dickinsona56c4672009-01-27 18:17:45 +0000314 for op in order_comparisons:
315 self.assertRaises(TypeError, op, y, x)
316 for op in equality_comparisons:
317 self.assertRaises(RuntimeError, op, y, x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000318 self.assertTrue(y is not x)
319 self.assertTrue(y['foo'] is y)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000320 self.assertEqual(len(y), 1)
Guido van Rossum99d2c252003-06-13 19:28:47 +0000321
Guido van Rossum581cb932003-02-06 17:52:15 +0000322 def test_deepcopy_keepalive(self):
323 memo = {}
Benjamin Petersone90ec362011-06-27 16:22:46 -0500324 x = []
Guido van Rossum581cb932003-02-06 17:52:15 +0000325 y = copy.deepcopy(x, memo)
Benjamin Petersone90ec362011-06-27 16:22:46 -0500326 self.assertIs(memo[id(memo)][0], x)
327
328 def test_deepcopy_dont_memo_immutable(self):
329 memo = {}
330 x = [1, 2, 3, 4]
331 y = copy.deepcopy(x, memo)
332 self.assertEqual(y, x)
333 # There's the entry for the new list, and the keep alive.
334 self.assertEqual(len(memo), 2)
335
336 memo = {}
337 x = [(1, 2)]
338 y = copy.deepcopy(x, memo)
339 self.assertEqual(y, x)
340 # Tuples with immutable contents are immutable for deepcopy.
341 self.assertEqual(len(memo), 2)
Guido van Rossum581cb932003-02-06 17:52:15 +0000342
343 def test_deepcopy_inst_vanilla(self):
344 class C:
345 def __init__(self, foo):
346 self.foo = foo
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000347 def __eq__(self, other):
348 return self.foo == other.foo
Guido van Rossum581cb932003-02-06 17:52:15 +0000349 x = C([42])
350 y = copy.deepcopy(x)
351 self.assertEqual(y, x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000352 self.assertTrue(y.foo is not x.foo)
Guido van Rossum581cb932003-02-06 17:52:15 +0000353
354 def test_deepcopy_inst_deepcopy(self):
355 class C:
356 def __init__(self, foo):
357 self.foo = foo
358 def __deepcopy__(self, memo):
359 return C(copy.deepcopy(self.foo, memo))
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000360 def __eq__(self, other):
361 return self.foo == other.foo
Guido van Rossum581cb932003-02-06 17:52:15 +0000362 x = C([42])
363 y = copy.deepcopy(x)
364 self.assertEqual(y, x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000365 self.assertTrue(y is not x)
366 self.assertTrue(y.foo is not x.foo)
Guido van Rossum581cb932003-02-06 17:52:15 +0000367
368 def test_deepcopy_inst_getinitargs(self):
369 class C:
370 def __init__(self, foo):
371 self.foo = foo
372 def __getinitargs__(self):
373 return (self.foo,)
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000374 def __eq__(self, other):
375 return self.foo == other.foo
Guido van Rossum581cb932003-02-06 17:52:15 +0000376 x = C([42])
377 y = copy.deepcopy(x)
378 self.assertEqual(y, x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000379 self.assertTrue(y is not x)
380 self.assertTrue(y.foo is not x.foo)
Guido van Rossum581cb932003-02-06 17:52:15 +0000381
382 def test_deepcopy_inst_getstate(self):
383 class C:
384 def __init__(self, foo):
385 self.foo = foo
386 def __getstate__(self):
387 return {"foo": self.foo}
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000388 def __eq__(self, other):
389 return self.foo == other.foo
Guido van Rossum581cb932003-02-06 17:52:15 +0000390 x = C([42])
391 y = copy.deepcopy(x)
392 self.assertEqual(y, x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000393 self.assertTrue(y is not x)
394 self.assertTrue(y.foo is not x.foo)
Guido van Rossum581cb932003-02-06 17:52:15 +0000395
396 def test_deepcopy_inst_setstate(self):
397 class C:
398 def __init__(self, foo):
399 self.foo = foo
400 def __setstate__(self, state):
401 self.foo = state["foo"]
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000402 def __eq__(self, other):
403 return self.foo == other.foo
Guido van Rossum581cb932003-02-06 17:52:15 +0000404 x = C([42])
405 y = copy.deepcopy(x)
406 self.assertEqual(y, x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000407 self.assertTrue(y is not x)
408 self.assertTrue(y.foo is not x.foo)
Guido van Rossum581cb932003-02-06 17:52:15 +0000409
410 def test_deepcopy_inst_getstate_setstate(self):
411 class C:
412 def __init__(self, foo):
413 self.foo = foo
414 def __getstate__(self):
415 return self.foo
416 def __setstate__(self, state):
417 self.foo = state
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000418 def __eq__(self, other):
419 return self.foo == other.foo
Guido van Rossum581cb932003-02-06 17:52:15 +0000420 x = C([42])
421 y = copy.deepcopy(x)
422 self.assertEqual(y, x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000423 self.assertTrue(y is not x)
424 self.assertTrue(y.foo is not x.foo)
Guido van Rossum581cb932003-02-06 17:52:15 +0000425
Guido van Rossum99d2c252003-06-13 19:28:47 +0000426 def test_deepcopy_reflexive_inst(self):
427 class C:
428 pass
429 x = C()
430 x.foo = x
431 y = copy.deepcopy(x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000432 self.assertTrue(y is not x)
433 self.assertTrue(y.foo is y)
Guido van Rossum99d2c252003-06-13 19:28:47 +0000434
Guido van Rossum581cb932003-02-06 17:52:15 +0000435 # _reconstruct()
436
437 def test_reconstruct_string(self):
438 class C(object):
439 def __reduce__(self):
440 return ""
441 x = C()
442 y = copy.copy(x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000443 self.assertTrue(y is x)
Guido van Rossum581cb932003-02-06 17:52:15 +0000444 y = copy.deepcopy(x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000445 self.assertTrue(y is x)
Guido van Rossum581cb932003-02-06 17:52:15 +0000446
447 def test_reconstruct_nostate(self):
448 class C(object):
449 def __reduce__(self):
450 return (C, ())
451 x = C()
452 x.foo = 42
453 y = copy.copy(x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000454 self.assertTrue(y.__class__ is x.__class__)
Guido van Rossum581cb932003-02-06 17:52:15 +0000455 y = copy.deepcopy(x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000456 self.assertTrue(y.__class__ is x.__class__)
Guido van Rossum581cb932003-02-06 17:52:15 +0000457
458 def test_reconstruct_state(self):
459 class C(object):
460 def __reduce__(self):
461 return (C, (), self.__dict__)
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000462 def __eq__(self, other):
463 return self.__dict__ == other.__dict__
Guido van Rossum581cb932003-02-06 17:52:15 +0000464 x = C()
465 x.foo = [42]
466 y = copy.copy(x)
467 self.assertEqual(y, x)
468 y = copy.deepcopy(x)
469 self.assertEqual(y, x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000470 self.assertTrue(y.foo is not x.foo)
Guido van Rossum581cb932003-02-06 17:52:15 +0000471
472 def test_reconstruct_state_setstate(self):
473 class C(object):
474 def __reduce__(self):
475 return (C, (), self.__dict__)
476 def __setstate__(self, state):
477 self.__dict__.update(state)
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000478 def __eq__(self, other):
479 return self.__dict__ == other.__dict__
Guido van Rossum581cb932003-02-06 17:52:15 +0000480 x = C()
481 x.foo = [42]
482 y = copy.copy(x)
483 self.assertEqual(y, x)
484 y = copy.deepcopy(x)
485 self.assertEqual(y, x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000486 self.assertTrue(y.foo is not x.foo)
Guido van Rossum581cb932003-02-06 17:52:15 +0000487
Guido van Rossum99d2c252003-06-13 19:28:47 +0000488 def test_reconstruct_reflexive(self):
489 class C(object):
490 pass
491 x = C()
492 x.foo = x
493 y = copy.deepcopy(x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000494 self.assertTrue(y is not x)
495 self.assertTrue(y.foo is y)
Guido van Rossum99d2c252003-06-13 19:28:47 +0000496
Guido van Rossum90e05b02003-02-06 18:18:23 +0000497 # Additions for Python 2.3 and pickle protocol 2
498
499 def test_reduce_4tuple(self):
500 class C(list):
501 def __reduce__(self):
502 return (C, (), self.__dict__, iter(self))
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000503 def __eq__(self, other):
504 return (list(self) == list(other) and
505 self.__dict__ == other.__dict__)
Guido van Rossum90e05b02003-02-06 18:18:23 +0000506 x = C([[1, 2], 3])
507 y = copy.copy(x)
508 self.assertEqual(x, y)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000509 self.assertTrue(x is not y)
510 self.assertTrue(x[0] is y[0])
Guido van Rossum90e05b02003-02-06 18:18:23 +0000511 y = copy.deepcopy(x)
512 self.assertEqual(x, y)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000513 self.assertTrue(x is not y)
514 self.assertTrue(x[0] is not y[0])
Guido van Rossum90e05b02003-02-06 18:18:23 +0000515
516 def test_reduce_5tuple(self):
517 class C(dict):
518 def __reduce__(self):
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000519 return (C, (), self.__dict__, None, self.items())
Guido van Rossum47b9ff62006-08-24 00:41:19 +0000520 def __eq__(self, other):
521 return (dict(self) == dict(other) and
522 self.__dict__ == other.__dict__)
Guido van Rossum90e05b02003-02-06 18:18:23 +0000523 x = C([("foo", [1, 2]), ("bar", 3)])
524 y = copy.copy(x)
525 self.assertEqual(x, y)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000526 self.assertTrue(x is not y)
527 self.assertTrue(x["foo"] is y["foo"])
Guido van Rossum90e05b02003-02-06 18:18:23 +0000528 y = copy.deepcopy(x)
529 self.assertEqual(x, y)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000530 self.assertTrue(x is not y)
531 self.assertTrue(x["foo"] is not y["foo"])
Guido van Rossum90e05b02003-02-06 18:18:23 +0000532
Guido van Rossumc7557582003-02-06 19:53:22 +0000533 def test_copy_slots(self):
534 class C(object):
535 __slots__ = ["foo"]
536 x = C()
537 x.foo = [42]
538 y = copy.copy(x)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000539 self.assertTrue(x.foo is y.foo)
Guido van Rossumc7557582003-02-06 19:53:22 +0000540
541 def test_deepcopy_slots(self):
542 class C(object):
543 __slots__ = ["foo"]
544 x = C()
545 x.foo = [42]
546 y = copy.deepcopy(x)
547 self.assertEqual(x.foo, y.foo)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000548 self.assertTrue(x.foo is not y.foo)
Guido van Rossumc7557582003-02-06 19:53:22 +0000549
Antoine Pitrou3941a8f2010-09-04 17:40:21 +0000550 def test_deepcopy_dict_subclass(self):
551 class C(dict):
552 def __init__(self, d=None):
553 if not d:
554 d = {}
555 self._keys = list(d.keys())
556 super().__init__(d)
557 def __setitem__(self, key, item):
558 super().__setitem__(key, item)
559 if key not in self._keys:
560 self._keys.append(key)
561 x = C(d={'foo':0})
562 y = copy.deepcopy(x)
563 self.assertEqual(x, y)
564 self.assertEqual(x._keys, y._keys)
565 self.assertTrue(x is not y)
566 x['bar'] = 1
567 self.assertNotEqual(x, y)
568 self.assertNotEqual(x._keys, y._keys)
569
Guido van Rossumc7557582003-02-06 19:53:22 +0000570 def test_copy_list_subclass(self):
571 class C(list):
572 pass
573 x = C([[1, 2], 3])
574 x.foo = [4, 5]
575 y = copy.copy(x)
576 self.assertEqual(list(x), list(y))
577 self.assertEqual(x.foo, y.foo)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000578 self.assertTrue(x[0] is y[0])
579 self.assertTrue(x.foo is y.foo)
Guido van Rossumc7557582003-02-06 19:53:22 +0000580
581 def test_deepcopy_list_subclass(self):
582 class C(list):
583 pass
584 x = C([[1, 2], 3])
585 x.foo = [4, 5]
586 y = copy.deepcopy(x)
587 self.assertEqual(list(x), list(y))
588 self.assertEqual(x.foo, y.foo)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000589 self.assertTrue(x[0] is not y[0])
590 self.assertTrue(x.foo is not y.foo)
Guido van Rossumc7557582003-02-06 19:53:22 +0000591
Guido van Rossum85233bf2003-02-06 21:25:12 +0000592 def test_copy_tuple_subclass(self):
593 class C(tuple):
594 pass
595 x = C([1, 2, 3])
596 self.assertEqual(tuple(x), (1, 2, 3))
597 y = copy.copy(x)
598 self.assertEqual(tuple(y), (1, 2, 3))
599
600 def test_deepcopy_tuple_subclass(self):
601 class C(tuple):
602 pass
603 x = C([[1, 2], 3])
604 self.assertEqual(tuple(x), ([1, 2], 3))
605 y = copy.deepcopy(x)
606 self.assertEqual(tuple(y), ([1, 2], 3))
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000607 self.assertTrue(x is not y)
608 self.assertTrue(x[0] is not y[0])
Guido van Rossum85233bf2003-02-06 21:25:12 +0000609
Neal Norwitze2fdc612003-06-08 13:19:58 +0000610 def test_getstate_exc(self):
611 class EvilState(object):
612 def __getstate__(self):
Collin Winter3add4d72007-08-29 23:37:32 +0000613 raise ValueError("ain't got no stickin' state")
Neal Norwitze2fdc612003-06-08 13:19:58 +0000614 self.assertRaises(ValueError, copy.copy, EvilState())
615
Guido van Rossum1968ad32006-02-25 22:38:04 +0000616 def test_copy_function(self):
617 self.assertEqual(copy.copy(global_foo), global_foo)
618 def foo(x, y): return x+y
619 self.assertEqual(copy.copy(foo), foo)
620 bar = lambda: None
621 self.assertEqual(copy.copy(bar), bar)
622
623 def test_deepcopy_function(self):
624 self.assertEqual(copy.deepcopy(global_foo), global_foo)
625 def foo(x, y): return x+y
626 self.assertEqual(copy.deepcopy(foo), foo)
627 bar = lambda: None
628 self.assertEqual(copy.deepcopy(bar), bar)
629
Antoine Pitrou6e610062009-05-15 17:04:50 +0000630 def _check_weakref(self, _copy):
631 class C(object):
632 pass
633 obj = C()
634 x = weakref.ref(obj)
635 y = _copy(x)
636 self.assertTrue(y is x)
637 del obj
638 y = _copy(x)
639 self.assertTrue(y is x)
640
641 def test_copy_weakref(self):
642 self._check_weakref(copy.copy)
643
644 def test_deepcopy_weakref(self):
645 self._check_weakref(copy.deepcopy)
646
647 def _check_copy_weakdict(self, _dicttype):
648 class C(object):
649 pass
650 a, b, c, d = [C() for i in range(4)]
651 u = _dicttype()
652 u[a] = b
653 u[c] = d
654 v = copy.copy(u)
655 self.assertFalse(v is u)
656 self.assertEqual(v, u)
657 self.assertEqual(v[a], b)
658 self.assertEqual(v[c], d)
659 self.assertEqual(len(v), 2)
660 del c, d
661 self.assertEqual(len(v), 1)
662 x, y = C(), C()
663 # The underlying containers are decoupled
664 v[x] = y
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000665 self.assertNotIn(x, u)
Antoine Pitrou6e610062009-05-15 17:04:50 +0000666
667 def test_copy_weakkeydict(self):
668 self._check_copy_weakdict(weakref.WeakKeyDictionary)
669
670 def test_copy_weakvaluedict(self):
671 self._check_copy_weakdict(weakref.WeakValueDictionary)
672
673 def test_deepcopy_weakkeydict(self):
674 class C(object):
675 def __init__(self, i):
676 self.i = i
677 a, b, c, d = [C(i) for i in range(4)]
678 u = weakref.WeakKeyDictionary()
679 u[a] = b
680 u[c] = d
681 # Keys aren't copied, values are
682 v = copy.deepcopy(u)
683 self.assertNotEqual(v, u)
684 self.assertEqual(len(v), 2)
685 self.assertFalse(v[a] is b)
686 self.assertFalse(v[c] is d)
687 self.assertEqual(v[a].i, b.i)
688 self.assertEqual(v[c].i, d.i)
689 del c
690 self.assertEqual(len(v), 1)
691
692 def test_deepcopy_weakvaluedict(self):
693 class C(object):
694 def __init__(self, i):
695 self.i = i
696 a, b, c, d = [C(i) for i in range(4)]
697 u = weakref.WeakValueDictionary()
698 u[a] = b
699 u[c] = d
700 # Keys are copied, values aren't
701 v = copy.deepcopy(u)
702 self.assertNotEqual(v, u)
703 self.assertEqual(len(v), 2)
704 (x, y), (z, t) = sorted(v.items(), key=lambda pair: pair[0].i)
705 self.assertFalse(x is a)
706 self.assertEqual(x.i, a.i)
707 self.assertTrue(y is b)
708 self.assertFalse(z is c)
709 self.assertEqual(z.i, c.i)
710 self.assertTrue(t is d)
711 del x, y, z, t
712 del d
713 self.assertEqual(len(v), 1)
714
Antoine Pitrou1fc0d2b2009-11-28 15:58:27 +0000715 def test_deepcopy_bound_method(self):
716 class Foo(object):
717 def m(self):
718 pass
719 f = Foo()
720 f.b = f.m
721 g = copy.deepcopy(f)
722 self.assertEqual(g.m, g.b)
723 self.assertTrue(g.b.__self__ is g)
724 g.b()
725
Antoine Pitrou6e610062009-05-15 17:04:50 +0000726
Guido van Rossum1968ad32006-02-25 22:38:04 +0000727def global_foo(x, y): return x+y
728
Guido van Rossum581cb932003-02-06 17:52:15 +0000729def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000730 support.run_unittest(TestCopy)
Guido van Rossum581cb932003-02-06 17:52:15 +0000731
732if __name__ == "__main__":
733 test_main()