1 | n/a | """Unit tests for the copy module.""" |
---|
2 | n/a | |
---|
3 | n/a | import copy |
---|
4 | n/a | import copyreg |
---|
5 | n/a | import weakref |
---|
6 | n/a | import abc |
---|
7 | n/a | from operator import le, lt, ge, gt, eq, ne |
---|
8 | n/a | |
---|
9 | n/a | import unittest |
---|
10 | n/a | |
---|
11 | n/a | order_comparisons = le, lt, ge, gt |
---|
12 | n/a | equality_comparisons = eq, ne |
---|
13 | n/a | comparisons = order_comparisons + equality_comparisons |
---|
14 | n/a | |
---|
15 | n/a | class TestCopy(unittest.TestCase): |
---|
16 | n/a | |
---|
17 | n/a | # Attempt full line coverage of copy.py from top to bottom |
---|
18 | n/a | |
---|
19 | n/a | def test_exceptions(self): |
---|
20 | n/a | self.assertIs(copy.Error, copy.error) |
---|
21 | n/a | self.assertTrue(issubclass(copy.Error, Exception)) |
---|
22 | n/a | |
---|
23 | n/a | # The copy() method |
---|
24 | n/a | |
---|
25 | n/a | def test_copy_basic(self): |
---|
26 | n/a | x = 42 |
---|
27 | n/a | y = copy.copy(x) |
---|
28 | n/a | self.assertEqual(x, y) |
---|
29 | n/a | |
---|
30 | n/a | def test_copy_copy(self): |
---|
31 | n/a | class C(object): |
---|
32 | n/a | def __init__(self, foo): |
---|
33 | n/a | self.foo = foo |
---|
34 | n/a | def __copy__(self): |
---|
35 | n/a | return C(self.foo) |
---|
36 | n/a | x = C(42) |
---|
37 | n/a | y = copy.copy(x) |
---|
38 | n/a | self.assertEqual(y.__class__, x.__class__) |
---|
39 | n/a | self.assertEqual(y.foo, x.foo) |
---|
40 | n/a | |
---|
41 | n/a | def test_copy_registry(self): |
---|
42 | n/a | class C(object): |
---|
43 | n/a | def __new__(cls, foo): |
---|
44 | n/a | obj = object.__new__(cls) |
---|
45 | n/a | obj.foo = foo |
---|
46 | n/a | return obj |
---|
47 | n/a | def pickle_C(obj): |
---|
48 | n/a | return (C, (obj.foo,)) |
---|
49 | n/a | x = C(42) |
---|
50 | n/a | self.assertRaises(TypeError, copy.copy, x) |
---|
51 | n/a | copyreg.pickle(C, pickle_C, C) |
---|
52 | n/a | y = copy.copy(x) |
---|
53 | n/a | |
---|
54 | n/a | def test_copy_reduce_ex(self): |
---|
55 | n/a | class C(object): |
---|
56 | n/a | def __reduce_ex__(self, proto): |
---|
57 | n/a | c.append(1) |
---|
58 | n/a | return "" |
---|
59 | n/a | def __reduce__(self): |
---|
60 | n/a | self.fail("shouldn't call this") |
---|
61 | n/a | c = [] |
---|
62 | n/a | x = C() |
---|
63 | n/a | y = copy.copy(x) |
---|
64 | n/a | self.assertIs(y, x) |
---|
65 | n/a | self.assertEqual(c, [1]) |
---|
66 | n/a | |
---|
67 | n/a | def test_copy_reduce(self): |
---|
68 | n/a | class C(object): |
---|
69 | n/a | def __reduce__(self): |
---|
70 | n/a | c.append(1) |
---|
71 | n/a | return "" |
---|
72 | n/a | c = [] |
---|
73 | n/a | x = C() |
---|
74 | n/a | y = copy.copy(x) |
---|
75 | n/a | self.assertIs(y, x) |
---|
76 | n/a | self.assertEqual(c, [1]) |
---|
77 | n/a | |
---|
78 | n/a | def test_copy_cant(self): |
---|
79 | n/a | class C(object): |
---|
80 | n/a | def __getattribute__(self, name): |
---|
81 | n/a | if name.startswith("__reduce"): |
---|
82 | n/a | raise AttributeError(name) |
---|
83 | n/a | return object.__getattribute__(self, name) |
---|
84 | n/a | x = C() |
---|
85 | n/a | self.assertRaises(copy.Error, copy.copy, x) |
---|
86 | n/a | |
---|
87 | n/a | # Type-specific _copy_xxx() methods |
---|
88 | n/a | |
---|
89 | n/a | def test_copy_atomic(self): |
---|
90 | n/a | class Classic: |
---|
91 | n/a | pass |
---|
92 | n/a | class NewStyle(object): |
---|
93 | n/a | pass |
---|
94 | n/a | def f(): |
---|
95 | n/a | pass |
---|
96 | n/a | class WithMetaclass(metaclass=abc.ABCMeta): |
---|
97 | n/a | pass |
---|
98 | n/a | tests = [None, ..., NotImplemented, |
---|
99 | n/a | 42, 2**100, 3.14, True, False, 1j, |
---|
100 | n/a | "hello", "hello\u1234", f.__code__, |
---|
101 | n/a | b"world", bytes(range(256)), range(10), slice(1, 10, 2), |
---|
102 | n/a | NewStyle, Classic, max, WithMetaclass] |
---|
103 | n/a | for x in tests: |
---|
104 | n/a | self.assertIs(copy.copy(x), x) |
---|
105 | n/a | |
---|
106 | n/a | def test_copy_list(self): |
---|
107 | n/a | x = [1, 2, 3] |
---|
108 | n/a | y = copy.copy(x) |
---|
109 | n/a | self.assertEqual(y, x) |
---|
110 | n/a | self.assertIsNot(y, x) |
---|
111 | n/a | x = [] |
---|
112 | n/a | y = copy.copy(x) |
---|
113 | n/a | self.assertEqual(y, x) |
---|
114 | n/a | self.assertIsNot(y, x) |
---|
115 | n/a | |
---|
116 | n/a | def test_copy_tuple(self): |
---|
117 | n/a | x = (1, 2, 3) |
---|
118 | n/a | self.assertIs(copy.copy(x), x) |
---|
119 | n/a | x = () |
---|
120 | n/a | self.assertIs(copy.copy(x), x) |
---|
121 | n/a | x = (1, 2, 3, []) |
---|
122 | n/a | self.assertIs(copy.copy(x), x) |
---|
123 | n/a | |
---|
124 | n/a | def test_copy_dict(self): |
---|
125 | n/a | x = {"foo": 1, "bar": 2} |
---|
126 | n/a | y = copy.copy(x) |
---|
127 | n/a | self.assertEqual(y, x) |
---|
128 | n/a | self.assertIsNot(y, x) |
---|
129 | n/a | x = {} |
---|
130 | n/a | y = copy.copy(x) |
---|
131 | n/a | self.assertEqual(y, x) |
---|
132 | n/a | self.assertIsNot(y, x) |
---|
133 | n/a | |
---|
134 | n/a | def test_copy_set(self): |
---|
135 | n/a | x = {1, 2, 3} |
---|
136 | n/a | y = copy.copy(x) |
---|
137 | n/a | self.assertEqual(y, x) |
---|
138 | n/a | self.assertIsNot(y, x) |
---|
139 | n/a | x = set() |
---|
140 | n/a | y = copy.copy(x) |
---|
141 | n/a | self.assertEqual(y, x) |
---|
142 | n/a | self.assertIsNot(y, x) |
---|
143 | n/a | |
---|
144 | n/a | def test_copy_frozenset(self): |
---|
145 | n/a | x = frozenset({1, 2, 3}) |
---|
146 | n/a | self.assertIs(copy.copy(x), x) |
---|
147 | n/a | x = frozenset() |
---|
148 | n/a | self.assertIs(copy.copy(x), x) |
---|
149 | n/a | |
---|
150 | n/a | def test_copy_bytearray(self): |
---|
151 | n/a | x = bytearray(b'abc') |
---|
152 | n/a | y = copy.copy(x) |
---|
153 | n/a | self.assertEqual(y, x) |
---|
154 | n/a | self.assertIsNot(y, x) |
---|
155 | n/a | x = bytearray() |
---|
156 | n/a | y = copy.copy(x) |
---|
157 | n/a | self.assertEqual(y, x) |
---|
158 | n/a | self.assertIsNot(y, x) |
---|
159 | n/a | |
---|
160 | n/a | def test_copy_inst_vanilla(self): |
---|
161 | n/a | class C: |
---|
162 | n/a | def __init__(self, foo): |
---|
163 | n/a | self.foo = foo |
---|
164 | n/a | def __eq__(self, other): |
---|
165 | n/a | return self.foo == other.foo |
---|
166 | n/a | x = C(42) |
---|
167 | n/a | self.assertEqual(copy.copy(x), x) |
---|
168 | n/a | |
---|
169 | n/a | def test_copy_inst_copy(self): |
---|
170 | n/a | class C: |
---|
171 | n/a | def __init__(self, foo): |
---|
172 | n/a | self.foo = foo |
---|
173 | n/a | def __copy__(self): |
---|
174 | n/a | return C(self.foo) |
---|
175 | n/a | def __eq__(self, other): |
---|
176 | n/a | return self.foo == other.foo |
---|
177 | n/a | x = C(42) |
---|
178 | n/a | self.assertEqual(copy.copy(x), x) |
---|
179 | n/a | |
---|
180 | n/a | def test_copy_inst_getinitargs(self): |
---|
181 | n/a | class C: |
---|
182 | n/a | def __init__(self, foo): |
---|
183 | n/a | self.foo = foo |
---|
184 | n/a | def __getinitargs__(self): |
---|
185 | n/a | return (self.foo,) |
---|
186 | n/a | def __eq__(self, other): |
---|
187 | n/a | return self.foo == other.foo |
---|
188 | n/a | x = C(42) |
---|
189 | n/a | self.assertEqual(copy.copy(x), x) |
---|
190 | n/a | |
---|
191 | n/a | def test_copy_inst_getnewargs(self): |
---|
192 | n/a | class C(int): |
---|
193 | n/a | def __new__(cls, foo): |
---|
194 | n/a | self = int.__new__(cls) |
---|
195 | n/a | self.foo = foo |
---|
196 | n/a | return self |
---|
197 | n/a | def __getnewargs__(self): |
---|
198 | n/a | return self.foo, |
---|
199 | n/a | def __eq__(self, other): |
---|
200 | n/a | return self.foo == other.foo |
---|
201 | n/a | x = C(42) |
---|
202 | n/a | y = copy.copy(x) |
---|
203 | n/a | self.assertIsInstance(y, C) |
---|
204 | n/a | self.assertEqual(y, x) |
---|
205 | n/a | self.assertIsNot(y, x) |
---|
206 | n/a | self.assertEqual(y.foo, x.foo) |
---|
207 | n/a | |
---|
208 | n/a | def test_copy_inst_getnewargs_ex(self): |
---|
209 | n/a | class C(int): |
---|
210 | n/a | def __new__(cls, *, foo): |
---|
211 | n/a | self = int.__new__(cls) |
---|
212 | n/a | self.foo = foo |
---|
213 | n/a | return self |
---|
214 | n/a | def __getnewargs_ex__(self): |
---|
215 | n/a | return (), {'foo': self.foo} |
---|
216 | n/a | def __eq__(self, other): |
---|
217 | n/a | return self.foo == other.foo |
---|
218 | n/a | x = C(foo=42) |
---|
219 | n/a | y = copy.copy(x) |
---|
220 | n/a | self.assertIsInstance(y, C) |
---|
221 | n/a | self.assertEqual(y, x) |
---|
222 | n/a | self.assertIsNot(y, x) |
---|
223 | n/a | self.assertEqual(y.foo, x.foo) |
---|
224 | n/a | |
---|
225 | n/a | def test_copy_inst_getstate(self): |
---|
226 | n/a | class C: |
---|
227 | n/a | def __init__(self, foo): |
---|
228 | n/a | self.foo = foo |
---|
229 | n/a | def __getstate__(self): |
---|
230 | n/a | return {"foo": self.foo} |
---|
231 | n/a | def __eq__(self, other): |
---|
232 | n/a | return self.foo == other.foo |
---|
233 | n/a | x = C(42) |
---|
234 | n/a | self.assertEqual(copy.copy(x), x) |
---|
235 | n/a | |
---|
236 | n/a | def test_copy_inst_setstate(self): |
---|
237 | n/a | class C: |
---|
238 | n/a | def __init__(self, foo): |
---|
239 | n/a | self.foo = foo |
---|
240 | n/a | def __setstate__(self, state): |
---|
241 | n/a | self.foo = state["foo"] |
---|
242 | n/a | def __eq__(self, other): |
---|
243 | n/a | return self.foo == other.foo |
---|
244 | n/a | x = C(42) |
---|
245 | n/a | self.assertEqual(copy.copy(x), x) |
---|
246 | n/a | |
---|
247 | n/a | def test_copy_inst_getstate_setstate(self): |
---|
248 | n/a | class C: |
---|
249 | n/a | def __init__(self, foo): |
---|
250 | n/a | self.foo = foo |
---|
251 | n/a | def __getstate__(self): |
---|
252 | n/a | return self.foo |
---|
253 | n/a | def __setstate__(self, state): |
---|
254 | n/a | self.foo = state |
---|
255 | n/a | def __eq__(self, other): |
---|
256 | n/a | return self.foo == other.foo |
---|
257 | n/a | x = C(42) |
---|
258 | n/a | self.assertEqual(copy.copy(x), x) |
---|
259 | n/a | # State with boolean value is false (issue #25718) |
---|
260 | n/a | x = C(0.0) |
---|
261 | n/a | self.assertEqual(copy.copy(x), x) |
---|
262 | n/a | |
---|
263 | n/a | # The deepcopy() method |
---|
264 | n/a | |
---|
265 | n/a | def test_deepcopy_basic(self): |
---|
266 | n/a | x = 42 |
---|
267 | n/a | y = copy.deepcopy(x) |
---|
268 | n/a | self.assertEqual(y, x) |
---|
269 | n/a | |
---|
270 | n/a | def test_deepcopy_memo(self): |
---|
271 | n/a | # Tests of reflexive objects are under type-specific sections below. |
---|
272 | n/a | # This tests only repetitions of objects. |
---|
273 | n/a | x = [] |
---|
274 | n/a | x = [x, x] |
---|
275 | n/a | y = copy.deepcopy(x) |
---|
276 | n/a | self.assertEqual(y, x) |
---|
277 | n/a | self.assertIsNot(y, x) |
---|
278 | n/a | self.assertIsNot(y[0], x[0]) |
---|
279 | n/a | self.assertIs(y[0], y[1]) |
---|
280 | n/a | |
---|
281 | n/a | def test_deepcopy_issubclass(self): |
---|
282 | n/a | # XXX Note: there's no way to test the TypeError coming out of |
---|
283 | n/a | # issubclass() -- this can only happen when an extension |
---|
284 | n/a | # module defines a "type" that doesn't formally inherit from |
---|
285 | n/a | # type. |
---|
286 | n/a | class Meta(type): |
---|
287 | n/a | pass |
---|
288 | n/a | class C(metaclass=Meta): |
---|
289 | n/a | pass |
---|
290 | n/a | self.assertEqual(copy.deepcopy(C), C) |
---|
291 | n/a | |
---|
292 | n/a | def test_deepcopy_deepcopy(self): |
---|
293 | n/a | class C(object): |
---|
294 | n/a | def __init__(self, foo): |
---|
295 | n/a | self.foo = foo |
---|
296 | n/a | def __deepcopy__(self, memo=None): |
---|
297 | n/a | return C(self.foo) |
---|
298 | n/a | x = C(42) |
---|
299 | n/a | y = copy.deepcopy(x) |
---|
300 | n/a | self.assertEqual(y.__class__, x.__class__) |
---|
301 | n/a | self.assertEqual(y.foo, x.foo) |
---|
302 | n/a | |
---|
303 | n/a | def test_deepcopy_registry(self): |
---|
304 | n/a | class C(object): |
---|
305 | n/a | def __new__(cls, foo): |
---|
306 | n/a | obj = object.__new__(cls) |
---|
307 | n/a | obj.foo = foo |
---|
308 | n/a | return obj |
---|
309 | n/a | def pickle_C(obj): |
---|
310 | n/a | return (C, (obj.foo,)) |
---|
311 | n/a | x = C(42) |
---|
312 | n/a | self.assertRaises(TypeError, copy.deepcopy, x) |
---|
313 | n/a | copyreg.pickle(C, pickle_C, C) |
---|
314 | n/a | y = copy.deepcopy(x) |
---|
315 | n/a | |
---|
316 | n/a | def test_deepcopy_reduce_ex(self): |
---|
317 | n/a | class C(object): |
---|
318 | n/a | def __reduce_ex__(self, proto): |
---|
319 | n/a | c.append(1) |
---|
320 | n/a | return "" |
---|
321 | n/a | def __reduce__(self): |
---|
322 | n/a | self.fail("shouldn't call this") |
---|
323 | n/a | c = [] |
---|
324 | n/a | x = C() |
---|
325 | n/a | y = copy.deepcopy(x) |
---|
326 | n/a | self.assertIs(y, x) |
---|
327 | n/a | self.assertEqual(c, [1]) |
---|
328 | n/a | |
---|
329 | n/a | def test_deepcopy_reduce(self): |
---|
330 | n/a | class C(object): |
---|
331 | n/a | def __reduce__(self): |
---|
332 | n/a | c.append(1) |
---|
333 | n/a | return "" |
---|
334 | n/a | c = [] |
---|
335 | n/a | x = C() |
---|
336 | n/a | y = copy.deepcopy(x) |
---|
337 | n/a | self.assertIs(y, x) |
---|
338 | n/a | self.assertEqual(c, [1]) |
---|
339 | n/a | |
---|
340 | n/a | def test_deepcopy_cant(self): |
---|
341 | n/a | class C(object): |
---|
342 | n/a | def __getattribute__(self, name): |
---|
343 | n/a | if name.startswith("__reduce"): |
---|
344 | n/a | raise AttributeError(name) |
---|
345 | n/a | return object.__getattribute__(self, name) |
---|
346 | n/a | x = C() |
---|
347 | n/a | self.assertRaises(copy.Error, copy.deepcopy, x) |
---|
348 | n/a | |
---|
349 | n/a | # Type-specific _deepcopy_xxx() methods |
---|
350 | n/a | |
---|
351 | n/a | def test_deepcopy_atomic(self): |
---|
352 | n/a | class Classic: |
---|
353 | n/a | pass |
---|
354 | n/a | class NewStyle(object): |
---|
355 | n/a | pass |
---|
356 | n/a | def f(): |
---|
357 | n/a | pass |
---|
358 | n/a | tests = [None, 42, 2**100, 3.14, True, False, 1j, |
---|
359 | n/a | "hello", "hello\u1234", f.__code__, |
---|
360 | n/a | NewStyle, Classic, max] |
---|
361 | n/a | for x in tests: |
---|
362 | n/a | self.assertIs(copy.deepcopy(x), x) |
---|
363 | n/a | |
---|
364 | n/a | def test_deepcopy_list(self): |
---|
365 | n/a | x = [[1, 2], 3] |
---|
366 | n/a | y = copy.deepcopy(x) |
---|
367 | n/a | self.assertEqual(y, x) |
---|
368 | n/a | self.assertIsNot(x, y) |
---|
369 | n/a | self.assertIsNot(x[0], y[0]) |
---|
370 | n/a | |
---|
371 | n/a | def test_deepcopy_reflexive_list(self): |
---|
372 | n/a | x = [] |
---|
373 | n/a | x.append(x) |
---|
374 | n/a | y = copy.deepcopy(x) |
---|
375 | n/a | for op in comparisons: |
---|
376 | n/a | self.assertRaises(RecursionError, op, y, x) |
---|
377 | n/a | self.assertIsNot(y, x) |
---|
378 | n/a | self.assertIs(y[0], y) |
---|
379 | n/a | self.assertEqual(len(y), 1) |
---|
380 | n/a | |
---|
381 | n/a | def test_deepcopy_empty_tuple(self): |
---|
382 | n/a | x = () |
---|
383 | n/a | y = copy.deepcopy(x) |
---|
384 | n/a | self.assertIs(x, y) |
---|
385 | n/a | |
---|
386 | n/a | def test_deepcopy_tuple(self): |
---|
387 | n/a | x = ([1, 2], 3) |
---|
388 | n/a | y = copy.deepcopy(x) |
---|
389 | n/a | self.assertEqual(y, x) |
---|
390 | n/a | self.assertIsNot(x, y) |
---|
391 | n/a | self.assertIsNot(x[0], y[0]) |
---|
392 | n/a | |
---|
393 | n/a | def test_deepcopy_tuple_of_immutables(self): |
---|
394 | n/a | x = ((1, 2), 3) |
---|
395 | n/a | y = copy.deepcopy(x) |
---|
396 | n/a | self.assertIs(x, y) |
---|
397 | n/a | |
---|
398 | n/a | def test_deepcopy_reflexive_tuple(self): |
---|
399 | n/a | x = ([],) |
---|
400 | n/a | x[0].append(x) |
---|
401 | n/a | y = copy.deepcopy(x) |
---|
402 | n/a | for op in comparisons: |
---|
403 | n/a | self.assertRaises(RecursionError, op, y, x) |
---|
404 | n/a | self.assertIsNot(y, x) |
---|
405 | n/a | self.assertIsNot(y[0], x[0]) |
---|
406 | n/a | self.assertIs(y[0][0], y) |
---|
407 | n/a | |
---|
408 | n/a | def test_deepcopy_dict(self): |
---|
409 | n/a | x = {"foo": [1, 2], "bar": 3} |
---|
410 | n/a | y = copy.deepcopy(x) |
---|
411 | n/a | self.assertEqual(y, x) |
---|
412 | n/a | self.assertIsNot(x, y) |
---|
413 | n/a | self.assertIsNot(x["foo"], y["foo"]) |
---|
414 | n/a | |
---|
415 | n/a | def test_deepcopy_reflexive_dict(self): |
---|
416 | n/a | x = {} |
---|
417 | n/a | x['foo'] = x |
---|
418 | n/a | y = copy.deepcopy(x) |
---|
419 | n/a | for op in order_comparisons: |
---|
420 | n/a | self.assertRaises(TypeError, op, y, x) |
---|
421 | n/a | for op in equality_comparisons: |
---|
422 | n/a | self.assertRaises(RecursionError, op, y, x) |
---|
423 | n/a | self.assertIsNot(y, x) |
---|
424 | n/a | self.assertIs(y['foo'], y) |
---|
425 | n/a | self.assertEqual(len(y), 1) |
---|
426 | n/a | |
---|
427 | n/a | def test_deepcopy_keepalive(self): |
---|
428 | n/a | memo = {} |
---|
429 | n/a | x = [] |
---|
430 | n/a | y = copy.deepcopy(x, memo) |
---|
431 | n/a | self.assertIs(memo[id(memo)][0], x) |
---|
432 | n/a | |
---|
433 | n/a | def test_deepcopy_dont_memo_immutable(self): |
---|
434 | n/a | memo = {} |
---|
435 | n/a | x = [1, 2, 3, 4] |
---|
436 | n/a | y = copy.deepcopy(x, memo) |
---|
437 | n/a | self.assertEqual(y, x) |
---|
438 | n/a | # There's the entry for the new list, and the keep alive. |
---|
439 | n/a | self.assertEqual(len(memo), 2) |
---|
440 | n/a | |
---|
441 | n/a | memo = {} |
---|
442 | n/a | x = [(1, 2)] |
---|
443 | n/a | y = copy.deepcopy(x, memo) |
---|
444 | n/a | self.assertEqual(y, x) |
---|
445 | n/a | # Tuples with immutable contents are immutable for deepcopy. |
---|
446 | n/a | self.assertEqual(len(memo), 2) |
---|
447 | n/a | |
---|
448 | n/a | def test_deepcopy_inst_vanilla(self): |
---|
449 | n/a | class C: |
---|
450 | n/a | def __init__(self, foo): |
---|
451 | n/a | self.foo = foo |
---|
452 | n/a | def __eq__(self, other): |
---|
453 | n/a | return self.foo == other.foo |
---|
454 | n/a | x = C([42]) |
---|
455 | n/a | y = copy.deepcopy(x) |
---|
456 | n/a | self.assertEqual(y, x) |
---|
457 | n/a | self.assertIsNot(y.foo, x.foo) |
---|
458 | n/a | |
---|
459 | n/a | def test_deepcopy_inst_deepcopy(self): |
---|
460 | n/a | class C: |
---|
461 | n/a | def __init__(self, foo): |
---|
462 | n/a | self.foo = foo |
---|
463 | n/a | def __deepcopy__(self, memo): |
---|
464 | n/a | return C(copy.deepcopy(self.foo, memo)) |
---|
465 | n/a | def __eq__(self, other): |
---|
466 | n/a | return self.foo == other.foo |
---|
467 | n/a | x = C([42]) |
---|
468 | n/a | y = copy.deepcopy(x) |
---|
469 | n/a | self.assertEqual(y, x) |
---|
470 | n/a | self.assertIsNot(y, x) |
---|
471 | n/a | self.assertIsNot(y.foo, x.foo) |
---|
472 | n/a | |
---|
473 | n/a | def test_deepcopy_inst_getinitargs(self): |
---|
474 | n/a | class C: |
---|
475 | n/a | def __init__(self, foo): |
---|
476 | n/a | self.foo = foo |
---|
477 | n/a | def __getinitargs__(self): |
---|
478 | n/a | return (self.foo,) |
---|
479 | n/a | def __eq__(self, other): |
---|
480 | n/a | return self.foo == other.foo |
---|
481 | n/a | x = C([42]) |
---|
482 | n/a | y = copy.deepcopy(x) |
---|
483 | n/a | self.assertEqual(y, x) |
---|
484 | n/a | self.assertIsNot(y, x) |
---|
485 | n/a | self.assertIsNot(y.foo, x.foo) |
---|
486 | n/a | |
---|
487 | n/a | def test_deepcopy_inst_getnewargs(self): |
---|
488 | n/a | class C(int): |
---|
489 | n/a | def __new__(cls, foo): |
---|
490 | n/a | self = int.__new__(cls) |
---|
491 | n/a | self.foo = foo |
---|
492 | n/a | return self |
---|
493 | n/a | def __getnewargs__(self): |
---|
494 | n/a | return self.foo, |
---|
495 | n/a | def __eq__(self, other): |
---|
496 | n/a | return self.foo == other.foo |
---|
497 | n/a | x = C([42]) |
---|
498 | n/a | y = copy.deepcopy(x) |
---|
499 | n/a | self.assertIsInstance(y, C) |
---|
500 | n/a | self.assertEqual(y, x) |
---|
501 | n/a | self.assertIsNot(y, x) |
---|
502 | n/a | self.assertEqual(y.foo, x.foo) |
---|
503 | n/a | self.assertIsNot(y.foo, x.foo) |
---|
504 | n/a | |
---|
505 | n/a | def test_deepcopy_inst_getnewargs_ex(self): |
---|
506 | n/a | class C(int): |
---|
507 | n/a | def __new__(cls, *, foo): |
---|
508 | n/a | self = int.__new__(cls) |
---|
509 | n/a | self.foo = foo |
---|
510 | n/a | return self |
---|
511 | n/a | def __getnewargs_ex__(self): |
---|
512 | n/a | return (), {'foo': self.foo} |
---|
513 | n/a | def __eq__(self, other): |
---|
514 | n/a | return self.foo == other.foo |
---|
515 | n/a | x = C(foo=[42]) |
---|
516 | n/a | y = copy.deepcopy(x) |
---|
517 | n/a | self.assertIsInstance(y, C) |
---|
518 | n/a | self.assertEqual(y, x) |
---|
519 | n/a | self.assertIsNot(y, x) |
---|
520 | n/a | self.assertEqual(y.foo, x.foo) |
---|
521 | n/a | self.assertIsNot(y.foo, x.foo) |
---|
522 | n/a | |
---|
523 | n/a | def test_deepcopy_inst_getstate(self): |
---|
524 | n/a | class C: |
---|
525 | n/a | def __init__(self, foo): |
---|
526 | n/a | self.foo = foo |
---|
527 | n/a | def __getstate__(self): |
---|
528 | n/a | return {"foo": self.foo} |
---|
529 | n/a | def __eq__(self, other): |
---|
530 | n/a | return self.foo == other.foo |
---|
531 | n/a | x = C([42]) |
---|
532 | n/a | y = copy.deepcopy(x) |
---|
533 | n/a | self.assertEqual(y, x) |
---|
534 | n/a | self.assertIsNot(y, x) |
---|
535 | n/a | self.assertIsNot(y.foo, x.foo) |
---|
536 | n/a | |
---|
537 | n/a | def test_deepcopy_inst_setstate(self): |
---|
538 | n/a | class C: |
---|
539 | n/a | def __init__(self, foo): |
---|
540 | n/a | self.foo = foo |
---|
541 | n/a | def __setstate__(self, state): |
---|
542 | n/a | self.foo = state["foo"] |
---|
543 | n/a | def __eq__(self, other): |
---|
544 | n/a | return self.foo == other.foo |
---|
545 | n/a | x = C([42]) |
---|
546 | n/a | y = copy.deepcopy(x) |
---|
547 | n/a | self.assertEqual(y, x) |
---|
548 | n/a | self.assertIsNot(y, x) |
---|
549 | n/a | self.assertIsNot(y.foo, x.foo) |
---|
550 | n/a | |
---|
551 | n/a | def test_deepcopy_inst_getstate_setstate(self): |
---|
552 | n/a | class C: |
---|
553 | n/a | def __init__(self, foo): |
---|
554 | n/a | self.foo = foo |
---|
555 | n/a | def __getstate__(self): |
---|
556 | n/a | return self.foo |
---|
557 | n/a | def __setstate__(self, state): |
---|
558 | n/a | self.foo = state |
---|
559 | n/a | def __eq__(self, other): |
---|
560 | n/a | return self.foo == other.foo |
---|
561 | n/a | x = C([42]) |
---|
562 | n/a | y = copy.deepcopy(x) |
---|
563 | n/a | self.assertEqual(y, x) |
---|
564 | n/a | self.assertIsNot(y, x) |
---|
565 | n/a | self.assertIsNot(y.foo, x.foo) |
---|
566 | n/a | # State with boolean value is false (issue #25718) |
---|
567 | n/a | x = C([]) |
---|
568 | n/a | y = copy.deepcopy(x) |
---|
569 | n/a | self.assertEqual(y, x) |
---|
570 | n/a | self.assertIsNot(y, x) |
---|
571 | n/a | self.assertIsNot(y.foo, x.foo) |
---|
572 | n/a | |
---|
573 | n/a | def test_deepcopy_reflexive_inst(self): |
---|
574 | n/a | class C: |
---|
575 | n/a | pass |
---|
576 | n/a | x = C() |
---|
577 | n/a | x.foo = x |
---|
578 | n/a | y = copy.deepcopy(x) |
---|
579 | n/a | self.assertIsNot(y, x) |
---|
580 | n/a | self.assertIs(y.foo, y) |
---|
581 | n/a | |
---|
582 | n/a | def test_deepcopy_range(self): |
---|
583 | n/a | class I(int): |
---|
584 | n/a | pass |
---|
585 | n/a | x = range(I(10)) |
---|
586 | n/a | y = copy.deepcopy(x) |
---|
587 | n/a | self.assertIsNot(y, x) |
---|
588 | n/a | self.assertEqual(y, x) |
---|
589 | n/a | self.assertIsNot(y.stop, x.stop) |
---|
590 | n/a | self.assertEqual(y.stop, x.stop) |
---|
591 | n/a | self.assertIsInstance(y.stop, I) |
---|
592 | n/a | |
---|
593 | n/a | # _reconstruct() |
---|
594 | n/a | |
---|
595 | n/a | def test_reconstruct_string(self): |
---|
596 | n/a | class C(object): |
---|
597 | n/a | def __reduce__(self): |
---|
598 | n/a | return "" |
---|
599 | n/a | x = C() |
---|
600 | n/a | y = copy.copy(x) |
---|
601 | n/a | self.assertIs(y, x) |
---|
602 | n/a | y = copy.deepcopy(x) |
---|
603 | n/a | self.assertIs(y, x) |
---|
604 | n/a | |
---|
605 | n/a | def test_reconstruct_nostate(self): |
---|
606 | n/a | class C(object): |
---|
607 | n/a | def __reduce__(self): |
---|
608 | n/a | return (C, ()) |
---|
609 | n/a | x = C() |
---|
610 | n/a | x.foo = 42 |
---|
611 | n/a | y = copy.copy(x) |
---|
612 | n/a | self.assertIs(y.__class__, x.__class__) |
---|
613 | n/a | y = copy.deepcopy(x) |
---|
614 | n/a | self.assertIs(y.__class__, x.__class__) |
---|
615 | n/a | |
---|
616 | n/a | def test_reconstruct_state(self): |
---|
617 | n/a | class C(object): |
---|
618 | n/a | def __reduce__(self): |
---|
619 | n/a | return (C, (), self.__dict__) |
---|
620 | n/a | def __eq__(self, other): |
---|
621 | n/a | return self.__dict__ == other.__dict__ |
---|
622 | n/a | x = C() |
---|
623 | n/a | x.foo = [42] |
---|
624 | n/a | y = copy.copy(x) |
---|
625 | n/a | self.assertEqual(y, x) |
---|
626 | n/a | y = copy.deepcopy(x) |
---|
627 | n/a | self.assertEqual(y, x) |
---|
628 | n/a | self.assertIsNot(y.foo, x.foo) |
---|
629 | n/a | |
---|
630 | n/a | def test_reconstruct_state_setstate(self): |
---|
631 | n/a | class C(object): |
---|
632 | n/a | def __reduce__(self): |
---|
633 | n/a | return (C, (), self.__dict__) |
---|
634 | n/a | def __setstate__(self, state): |
---|
635 | n/a | self.__dict__.update(state) |
---|
636 | n/a | def __eq__(self, other): |
---|
637 | n/a | return self.__dict__ == other.__dict__ |
---|
638 | n/a | x = C() |
---|
639 | n/a | x.foo = [42] |
---|
640 | n/a | y = copy.copy(x) |
---|
641 | n/a | self.assertEqual(y, x) |
---|
642 | n/a | y = copy.deepcopy(x) |
---|
643 | n/a | self.assertEqual(y, x) |
---|
644 | n/a | self.assertIsNot(y.foo, x.foo) |
---|
645 | n/a | |
---|
646 | n/a | def test_reconstruct_reflexive(self): |
---|
647 | n/a | class C(object): |
---|
648 | n/a | pass |
---|
649 | n/a | x = C() |
---|
650 | n/a | x.foo = x |
---|
651 | n/a | y = copy.deepcopy(x) |
---|
652 | n/a | self.assertIsNot(y, x) |
---|
653 | n/a | self.assertIs(y.foo, y) |
---|
654 | n/a | |
---|
655 | n/a | # Additions for Python 2.3 and pickle protocol 2 |
---|
656 | n/a | |
---|
657 | n/a | def test_reduce_4tuple(self): |
---|
658 | n/a | class C(list): |
---|
659 | n/a | def __reduce__(self): |
---|
660 | n/a | return (C, (), self.__dict__, iter(self)) |
---|
661 | n/a | def __eq__(self, other): |
---|
662 | n/a | return (list(self) == list(other) and |
---|
663 | n/a | self.__dict__ == other.__dict__) |
---|
664 | n/a | x = C([[1, 2], 3]) |
---|
665 | n/a | y = copy.copy(x) |
---|
666 | n/a | self.assertEqual(x, y) |
---|
667 | n/a | self.assertIsNot(x, y) |
---|
668 | n/a | self.assertIs(x[0], y[0]) |
---|
669 | n/a | y = copy.deepcopy(x) |
---|
670 | n/a | self.assertEqual(x, y) |
---|
671 | n/a | self.assertIsNot(x, y) |
---|
672 | n/a | self.assertIsNot(x[0], y[0]) |
---|
673 | n/a | |
---|
674 | n/a | def test_reduce_5tuple(self): |
---|
675 | n/a | class C(dict): |
---|
676 | n/a | def __reduce__(self): |
---|
677 | n/a | return (C, (), self.__dict__, None, self.items()) |
---|
678 | n/a | def __eq__(self, other): |
---|
679 | n/a | return (dict(self) == dict(other) and |
---|
680 | n/a | self.__dict__ == other.__dict__) |
---|
681 | n/a | x = C([("foo", [1, 2]), ("bar", 3)]) |
---|
682 | n/a | y = copy.copy(x) |
---|
683 | n/a | self.assertEqual(x, y) |
---|
684 | n/a | self.assertIsNot(x, y) |
---|
685 | n/a | self.assertIs(x["foo"], y["foo"]) |
---|
686 | n/a | y = copy.deepcopy(x) |
---|
687 | n/a | self.assertEqual(x, y) |
---|
688 | n/a | self.assertIsNot(x, y) |
---|
689 | n/a | self.assertIsNot(x["foo"], y["foo"]) |
---|
690 | n/a | |
---|
691 | n/a | def test_copy_slots(self): |
---|
692 | n/a | class C(object): |
---|
693 | n/a | __slots__ = ["foo"] |
---|
694 | n/a | x = C() |
---|
695 | n/a | x.foo = [42] |
---|
696 | n/a | y = copy.copy(x) |
---|
697 | n/a | self.assertIs(x.foo, y.foo) |
---|
698 | n/a | |
---|
699 | n/a | def test_deepcopy_slots(self): |
---|
700 | n/a | class C(object): |
---|
701 | n/a | __slots__ = ["foo"] |
---|
702 | n/a | x = C() |
---|
703 | n/a | x.foo = [42] |
---|
704 | n/a | y = copy.deepcopy(x) |
---|
705 | n/a | self.assertEqual(x.foo, y.foo) |
---|
706 | n/a | self.assertIsNot(x.foo, y.foo) |
---|
707 | n/a | |
---|
708 | n/a | def test_deepcopy_dict_subclass(self): |
---|
709 | n/a | class C(dict): |
---|
710 | n/a | def __init__(self, d=None): |
---|
711 | n/a | if not d: |
---|
712 | n/a | d = {} |
---|
713 | n/a | self._keys = list(d.keys()) |
---|
714 | n/a | super().__init__(d) |
---|
715 | n/a | def __setitem__(self, key, item): |
---|
716 | n/a | super().__setitem__(key, item) |
---|
717 | n/a | if key not in self._keys: |
---|
718 | n/a | self._keys.append(key) |
---|
719 | n/a | x = C(d={'foo':0}) |
---|
720 | n/a | y = copy.deepcopy(x) |
---|
721 | n/a | self.assertEqual(x, y) |
---|
722 | n/a | self.assertEqual(x._keys, y._keys) |
---|
723 | n/a | self.assertIsNot(x, y) |
---|
724 | n/a | x['bar'] = 1 |
---|
725 | n/a | self.assertNotEqual(x, y) |
---|
726 | n/a | self.assertNotEqual(x._keys, y._keys) |
---|
727 | n/a | |
---|
728 | n/a | def test_copy_list_subclass(self): |
---|
729 | n/a | class C(list): |
---|
730 | n/a | pass |
---|
731 | n/a | x = C([[1, 2], 3]) |
---|
732 | n/a | x.foo = [4, 5] |
---|
733 | n/a | y = copy.copy(x) |
---|
734 | n/a | self.assertEqual(list(x), list(y)) |
---|
735 | n/a | self.assertEqual(x.foo, y.foo) |
---|
736 | n/a | self.assertIs(x[0], y[0]) |
---|
737 | n/a | self.assertIs(x.foo, y.foo) |
---|
738 | n/a | |
---|
739 | n/a | def test_deepcopy_list_subclass(self): |
---|
740 | n/a | class C(list): |
---|
741 | n/a | pass |
---|
742 | n/a | x = C([[1, 2], 3]) |
---|
743 | n/a | x.foo = [4, 5] |
---|
744 | n/a | y = copy.deepcopy(x) |
---|
745 | n/a | self.assertEqual(list(x), list(y)) |
---|
746 | n/a | self.assertEqual(x.foo, y.foo) |
---|
747 | n/a | self.assertIsNot(x[0], y[0]) |
---|
748 | n/a | self.assertIsNot(x.foo, y.foo) |
---|
749 | n/a | |
---|
750 | n/a | def test_copy_tuple_subclass(self): |
---|
751 | n/a | class C(tuple): |
---|
752 | n/a | pass |
---|
753 | n/a | x = C([1, 2, 3]) |
---|
754 | n/a | self.assertEqual(tuple(x), (1, 2, 3)) |
---|
755 | n/a | y = copy.copy(x) |
---|
756 | n/a | self.assertEqual(tuple(y), (1, 2, 3)) |
---|
757 | n/a | |
---|
758 | n/a | def test_deepcopy_tuple_subclass(self): |
---|
759 | n/a | class C(tuple): |
---|
760 | n/a | pass |
---|
761 | n/a | x = C([[1, 2], 3]) |
---|
762 | n/a | self.assertEqual(tuple(x), ([1, 2], 3)) |
---|
763 | n/a | y = copy.deepcopy(x) |
---|
764 | n/a | self.assertEqual(tuple(y), ([1, 2], 3)) |
---|
765 | n/a | self.assertIsNot(x, y) |
---|
766 | n/a | self.assertIsNot(x[0], y[0]) |
---|
767 | n/a | |
---|
768 | n/a | def test_getstate_exc(self): |
---|
769 | n/a | class EvilState(object): |
---|
770 | n/a | def __getstate__(self): |
---|
771 | n/a | raise ValueError("ain't got no stickin' state") |
---|
772 | n/a | self.assertRaises(ValueError, copy.copy, EvilState()) |
---|
773 | n/a | |
---|
774 | n/a | def test_copy_function(self): |
---|
775 | n/a | self.assertEqual(copy.copy(global_foo), global_foo) |
---|
776 | n/a | def foo(x, y): return x+y |
---|
777 | n/a | self.assertEqual(copy.copy(foo), foo) |
---|
778 | n/a | bar = lambda: None |
---|
779 | n/a | self.assertEqual(copy.copy(bar), bar) |
---|
780 | n/a | |
---|
781 | n/a | def test_deepcopy_function(self): |
---|
782 | n/a | self.assertEqual(copy.deepcopy(global_foo), global_foo) |
---|
783 | n/a | def foo(x, y): return x+y |
---|
784 | n/a | self.assertEqual(copy.deepcopy(foo), foo) |
---|
785 | n/a | bar = lambda: None |
---|
786 | n/a | self.assertEqual(copy.deepcopy(bar), bar) |
---|
787 | n/a | |
---|
788 | n/a | def _check_weakref(self, _copy): |
---|
789 | n/a | class C(object): |
---|
790 | n/a | pass |
---|
791 | n/a | obj = C() |
---|
792 | n/a | x = weakref.ref(obj) |
---|
793 | n/a | y = _copy(x) |
---|
794 | n/a | self.assertIs(y, x) |
---|
795 | n/a | del obj |
---|
796 | n/a | y = _copy(x) |
---|
797 | n/a | self.assertIs(y, x) |
---|
798 | n/a | |
---|
799 | n/a | def test_copy_weakref(self): |
---|
800 | n/a | self._check_weakref(copy.copy) |
---|
801 | n/a | |
---|
802 | n/a | def test_deepcopy_weakref(self): |
---|
803 | n/a | self._check_weakref(copy.deepcopy) |
---|
804 | n/a | |
---|
805 | n/a | def _check_copy_weakdict(self, _dicttype): |
---|
806 | n/a | class C(object): |
---|
807 | n/a | pass |
---|
808 | n/a | a, b, c, d = [C() for i in range(4)] |
---|
809 | n/a | u = _dicttype() |
---|
810 | n/a | u[a] = b |
---|
811 | n/a | u[c] = d |
---|
812 | n/a | v = copy.copy(u) |
---|
813 | n/a | self.assertIsNot(v, u) |
---|
814 | n/a | self.assertEqual(v, u) |
---|
815 | n/a | self.assertEqual(v[a], b) |
---|
816 | n/a | self.assertEqual(v[c], d) |
---|
817 | n/a | self.assertEqual(len(v), 2) |
---|
818 | n/a | del c, d |
---|
819 | n/a | self.assertEqual(len(v), 1) |
---|
820 | n/a | x, y = C(), C() |
---|
821 | n/a | # The underlying containers are decoupled |
---|
822 | n/a | v[x] = y |
---|
823 | n/a | self.assertNotIn(x, u) |
---|
824 | n/a | |
---|
825 | n/a | def test_copy_weakkeydict(self): |
---|
826 | n/a | self._check_copy_weakdict(weakref.WeakKeyDictionary) |
---|
827 | n/a | |
---|
828 | n/a | def test_copy_weakvaluedict(self): |
---|
829 | n/a | self._check_copy_weakdict(weakref.WeakValueDictionary) |
---|
830 | n/a | |
---|
831 | n/a | def test_deepcopy_weakkeydict(self): |
---|
832 | n/a | class C(object): |
---|
833 | n/a | def __init__(self, i): |
---|
834 | n/a | self.i = i |
---|
835 | n/a | a, b, c, d = [C(i) for i in range(4)] |
---|
836 | n/a | u = weakref.WeakKeyDictionary() |
---|
837 | n/a | u[a] = b |
---|
838 | n/a | u[c] = d |
---|
839 | n/a | # Keys aren't copied, values are |
---|
840 | n/a | v = copy.deepcopy(u) |
---|
841 | n/a | self.assertNotEqual(v, u) |
---|
842 | n/a | self.assertEqual(len(v), 2) |
---|
843 | n/a | self.assertIsNot(v[a], b) |
---|
844 | n/a | self.assertIsNot(v[c], d) |
---|
845 | n/a | self.assertEqual(v[a].i, b.i) |
---|
846 | n/a | self.assertEqual(v[c].i, d.i) |
---|
847 | n/a | del c |
---|
848 | n/a | self.assertEqual(len(v), 1) |
---|
849 | n/a | |
---|
850 | n/a | def test_deepcopy_weakvaluedict(self): |
---|
851 | n/a | class C(object): |
---|
852 | n/a | def __init__(self, i): |
---|
853 | n/a | self.i = i |
---|
854 | n/a | a, b, c, d = [C(i) for i in range(4)] |
---|
855 | n/a | u = weakref.WeakValueDictionary() |
---|
856 | n/a | u[a] = b |
---|
857 | n/a | u[c] = d |
---|
858 | n/a | # Keys are copied, values aren't |
---|
859 | n/a | v = copy.deepcopy(u) |
---|
860 | n/a | self.assertNotEqual(v, u) |
---|
861 | n/a | self.assertEqual(len(v), 2) |
---|
862 | n/a | (x, y), (z, t) = sorted(v.items(), key=lambda pair: pair[0].i) |
---|
863 | n/a | self.assertIsNot(x, a) |
---|
864 | n/a | self.assertEqual(x.i, a.i) |
---|
865 | n/a | self.assertIs(y, b) |
---|
866 | n/a | self.assertIsNot(z, c) |
---|
867 | n/a | self.assertEqual(z.i, c.i) |
---|
868 | n/a | self.assertIs(t, d) |
---|
869 | n/a | del x, y, z, t |
---|
870 | n/a | del d |
---|
871 | n/a | self.assertEqual(len(v), 1) |
---|
872 | n/a | |
---|
873 | n/a | def test_deepcopy_bound_method(self): |
---|
874 | n/a | class Foo(object): |
---|
875 | n/a | def m(self): |
---|
876 | n/a | pass |
---|
877 | n/a | f = Foo() |
---|
878 | n/a | f.b = f.m |
---|
879 | n/a | g = copy.deepcopy(f) |
---|
880 | n/a | self.assertEqual(g.m, g.b) |
---|
881 | n/a | self.assertIs(g.b.__self__, g) |
---|
882 | n/a | g.b() |
---|
883 | n/a | |
---|
884 | n/a | |
---|
885 | n/a | def global_foo(x, y): return x+y |
---|
886 | n/a | |
---|
887 | n/a | if __name__ == "__main__": |
---|
888 | n/a | unittest.main() |
---|