move sections
[python/dscho.git] / Lib / test / test_copy.py
blobe3503c4343327cf2174bf297c2bfb9b84ec24b39
1 """Unit tests for the copy module."""
3 import copy
4 import copy_reg
5 import weakref
7 import unittest
8 from test import test_support
10 class TestCopy(unittest.TestCase):
12 # Attempt full line coverage of copy.py from top to bottom
14 def test_exceptions(self):
15 self.assertTrue(copy.Error is copy.error)
16 self.assertTrue(issubclass(copy.Error, Exception))
18 # The copy() method
20 def test_copy_basic(self):
21 x = 42
22 y = copy.copy(x)
23 self.assertEqual(x, y)
25 def test_copy_copy(self):
26 class C(object):
27 def __init__(self, foo):
28 self.foo = foo
29 def __copy__(self):
30 return C(self.foo)
31 x = C(42)
32 y = copy.copy(x)
33 self.assertEqual(y.__class__, x.__class__)
34 self.assertEqual(y.foo, x.foo)
36 def test_copy_registry(self):
37 class C(object):
38 def __new__(cls, foo):
39 obj = object.__new__(cls)
40 obj.foo = foo
41 return obj
42 def pickle_C(obj):
43 return (C, (obj.foo,))
44 x = C(42)
45 self.assertRaises(TypeError, copy.copy, x)
46 copy_reg.pickle(C, pickle_C, C)
47 y = copy.copy(x)
49 def test_copy_reduce_ex(self):
50 class C(object):
51 def __reduce_ex__(self, proto):
52 return ""
53 def __reduce__(self):
54 raise test_support.TestFailed, "shouldn't call this"
55 x = C()
56 y = copy.copy(x)
57 self.assertTrue(y is x)
59 def test_copy_reduce(self):
60 class C(object):
61 def __reduce__(self):
62 return ""
63 x = C()
64 y = copy.copy(x)
65 self.assertTrue(y is x)
67 def test_copy_cant(self):
68 class C(object):
69 def __getattribute__(self, name):
70 if name.startswith("__reduce"):
71 raise AttributeError, name
72 return object.__getattribute__(self, name)
73 x = C()
74 self.assertRaises(copy.Error, copy.copy, x)
76 # Type-specific _copy_xxx() methods
78 def test_copy_atomic(self):
79 class Classic:
80 pass
81 class NewStyle(object):
82 pass
83 def f():
84 pass
85 tests = [None, 42, 2L**100, 3.14, True, False, 1j,
86 "hello", u"hello\u1234", f.func_code,
87 NewStyle, xrange(10), Classic, max]
88 for x in tests:
89 self.assertTrue(copy.copy(x) is x, repr(x))
91 def test_copy_list(self):
92 x = [1, 2, 3]
93 self.assertEqual(copy.copy(x), x)
95 def test_copy_tuple(self):
96 x = (1, 2, 3)
97 self.assertEqual(copy.copy(x), x)
99 def test_copy_dict(self):
100 x = {"foo": 1, "bar": 2}
101 self.assertEqual(copy.copy(x), x)
103 def test_copy_inst_vanilla(self):
104 class C:
105 def __init__(self, foo):
106 self.foo = foo
107 def __cmp__(self, other):
108 return cmp(self.foo, other.foo)
109 x = C(42)
110 self.assertEqual(copy.copy(x), x)
112 def test_copy_inst_copy(self):
113 class C:
114 def __init__(self, foo):
115 self.foo = foo
116 def __copy__(self):
117 return C(self.foo)
118 def __cmp__(self, other):
119 return cmp(self.foo, other.foo)
120 x = C(42)
121 self.assertEqual(copy.copy(x), x)
123 def test_copy_inst_getinitargs(self):
124 class C:
125 def __init__(self, foo):
126 self.foo = foo
127 def __getinitargs__(self):
128 return (self.foo,)
129 def __cmp__(self, other):
130 return cmp(self.foo, other.foo)
131 x = C(42)
132 self.assertEqual(copy.copy(x), x)
134 def test_copy_inst_getstate(self):
135 class C:
136 def __init__(self, foo):
137 self.foo = foo
138 def __getstate__(self):
139 return {"foo": self.foo}
140 def __cmp__(self, other):
141 return cmp(self.foo, other.foo)
142 x = C(42)
143 self.assertEqual(copy.copy(x), x)
145 def test_copy_inst_setstate(self):
146 class C:
147 def __init__(self, foo):
148 self.foo = foo
149 def __setstate__(self, state):
150 self.foo = state["foo"]
151 def __cmp__(self, other):
152 return cmp(self.foo, other.foo)
153 x = C(42)
154 self.assertEqual(copy.copy(x), x)
156 def test_copy_inst_getstate_setstate(self):
157 class C:
158 def __init__(self, foo):
159 self.foo = foo
160 def __getstate__(self):
161 return self.foo
162 def __setstate__(self, state):
163 self.foo = state
164 def __cmp__(self, other):
165 return cmp(self.foo, other.foo)
166 x = C(42)
167 self.assertEqual(copy.copy(x), x)
169 # The deepcopy() method
171 def test_deepcopy_basic(self):
172 x = 42
173 y = copy.deepcopy(x)
174 self.assertEqual(y, x)
176 def test_deepcopy_memo(self):
177 # Tests of reflexive objects are under type-specific sections below.
178 # This tests only repetitions of objects.
179 x = []
180 x = [x, x]
181 y = copy.deepcopy(x)
182 self.assertEqual(y, x)
183 self.assertTrue(y is not x)
184 self.assertTrue(y[0] is not x[0])
185 self.assertTrue(y[0] is y[1])
187 def test_deepcopy_issubclass(self):
188 # XXX Note: there's no way to test the TypeError coming out of
189 # issubclass() -- this can only happen when an extension
190 # module defines a "type" that doesn't formally inherit from
191 # type.
192 class Meta(type):
193 pass
194 class C:
195 __metaclass__ = Meta
196 self.assertEqual(copy.deepcopy(C), C)
198 def test_deepcopy_deepcopy(self):
199 class C(object):
200 def __init__(self, foo):
201 self.foo = foo
202 def __deepcopy__(self, memo=None):
203 return C(self.foo)
204 x = C(42)
205 y = copy.deepcopy(x)
206 self.assertEqual(y.__class__, x.__class__)
207 self.assertEqual(y.foo, x.foo)
209 def test_deepcopy_registry(self):
210 class C(object):
211 def __new__(cls, foo):
212 obj = object.__new__(cls)
213 obj.foo = foo
214 return obj
215 def pickle_C(obj):
216 return (C, (obj.foo,))
217 x = C(42)
218 self.assertRaises(TypeError, copy.deepcopy, x)
219 copy_reg.pickle(C, pickle_C, C)
220 y = copy.deepcopy(x)
222 def test_deepcopy_reduce_ex(self):
223 class C(object):
224 def __reduce_ex__(self, proto):
225 return ""
226 def __reduce__(self):
227 raise test_support.TestFailed, "shouldn't call this"
228 x = C()
229 y = copy.deepcopy(x)
230 self.assertTrue(y is x)
232 def test_deepcopy_reduce(self):
233 class C(object):
234 def __reduce__(self):
235 return ""
236 x = C()
237 y = copy.deepcopy(x)
238 self.assertTrue(y is x)
240 def test_deepcopy_cant(self):
241 class C(object):
242 def __getattribute__(self, name):
243 if name.startswith("__reduce"):
244 raise AttributeError, name
245 return object.__getattribute__(self, name)
246 x = C()
247 self.assertRaises(copy.Error, copy.deepcopy, x)
249 # Type-specific _deepcopy_xxx() methods
251 def test_deepcopy_atomic(self):
252 class Classic:
253 pass
254 class NewStyle(object):
255 pass
256 def f():
257 pass
258 tests = [None, 42, 2L**100, 3.14, True, False, 1j,
259 "hello", u"hello\u1234", f.func_code,
260 NewStyle, xrange(10), Classic, max]
261 for x in tests:
262 self.assertTrue(copy.deepcopy(x) is x, repr(x))
264 def test_deepcopy_list(self):
265 x = [[1, 2], 3]
266 y = copy.deepcopy(x)
267 self.assertEqual(y, x)
268 self.assertTrue(x is not y)
269 self.assertTrue(x[0] is not y[0])
271 def test_deepcopy_reflexive_list(self):
272 x = []
273 x.append(x)
274 y = copy.deepcopy(x)
275 self.assertRaises(RuntimeError, cmp, y, x)
276 self.assertTrue(y is not x)
277 self.assertTrue(y[0] is y)
278 self.assertEqual(len(y), 1)
280 def test_deepcopy_tuple(self):
281 x = ([1, 2], 3)
282 y = copy.deepcopy(x)
283 self.assertEqual(y, x)
284 self.assertTrue(x is not y)
285 self.assertTrue(x[0] is not y[0])
287 def test_deepcopy_reflexive_tuple(self):
288 x = ([],)
289 x[0].append(x)
290 y = copy.deepcopy(x)
291 self.assertRaises(RuntimeError, cmp, y, x)
292 self.assertTrue(y is not x)
293 self.assertTrue(y[0] is not x[0])
294 self.assertTrue(y[0][0] is y)
296 def test_deepcopy_dict(self):
297 x = {"foo": [1, 2], "bar": 3}
298 y = copy.deepcopy(x)
299 self.assertEqual(y, x)
300 self.assertTrue(x is not y)
301 self.assertTrue(x["foo"] is not y["foo"])
303 def test_deepcopy_reflexive_dict(self):
304 x = {}
305 x['foo'] = x
306 y = copy.deepcopy(x)
307 self.assertRaises(RuntimeError, cmp, y, x)
308 self.assertTrue(y is not x)
309 self.assertTrue(y['foo'] is y)
310 self.assertEqual(len(y), 1)
312 def test_deepcopy_keepalive(self):
313 memo = {}
314 x = 42
315 y = copy.deepcopy(x, memo)
316 self.assertTrue(memo[id(x)] is x)
318 def test_deepcopy_inst_vanilla(self):
319 class C:
320 def __init__(self, foo):
321 self.foo = foo
322 def __cmp__(self, other):
323 return cmp(self.foo, other.foo)
324 x = C([42])
325 y = copy.deepcopy(x)
326 self.assertEqual(y, x)
327 self.assertTrue(y.foo is not x.foo)
329 def test_deepcopy_inst_deepcopy(self):
330 class C:
331 def __init__(self, foo):
332 self.foo = foo
333 def __deepcopy__(self, memo):
334 return C(copy.deepcopy(self.foo, memo))
335 def __cmp__(self, other):
336 return cmp(self.foo, other.foo)
337 x = C([42])
338 y = copy.deepcopy(x)
339 self.assertEqual(y, x)
340 self.assertTrue(y is not x)
341 self.assertTrue(y.foo is not x.foo)
343 def test_deepcopy_inst_getinitargs(self):
344 class C:
345 def __init__(self, foo):
346 self.foo = foo
347 def __getinitargs__(self):
348 return (self.foo,)
349 def __cmp__(self, other):
350 return cmp(self.foo, other.foo)
351 x = C([42])
352 y = copy.deepcopy(x)
353 self.assertEqual(y, x)
354 self.assertTrue(y is not x)
355 self.assertTrue(y.foo is not x.foo)
357 def test_deepcopy_inst_getstate(self):
358 class C:
359 def __init__(self, foo):
360 self.foo = foo
361 def __getstate__(self):
362 return {"foo": self.foo}
363 def __cmp__(self, other):
364 return cmp(self.foo, other.foo)
365 x = C([42])
366 y = copy.deepcopy(x)
367 self.assertEqual(y, x)
368 self.assertTrue(y is not x)
369 self.assertTrue(y.foo is not x.foo)
371 def test_deepcopy_inst_setstate(self):
372 class C:
373 def __init__(self, foo):
374 self.foo = foo
375 def __setstate__(self, state):
376 self.foo = state["foo"]
377 def __cmp__(self, other):
378 return cmp(self.foo, other.foo)
379 x = C([42])
380 y = copy.deepcopy(x)
381 self.assertEqual(y, x)
382 self.assertTrue(y is not x)
383 self.assertTrue(y.foo is not x.foo)
385 def test_deepcopy_inst_getstate_setstate(self):
386 class C:
387 def __init__(self, foo):
388 self.foo = foo
389 def __getstate__(self):
390 return self.foo
391 def __setstate__(self, state):
392 self.foo = state
393 def __cmp__(self, other):
394 return cmp(self.foo, other.foo)
395 x = C([42])
396 y = copy.deepcopy(x)
397 self.assertEqual(y, x)
398 self.assertTrue(y is not x)
399 self.assertTrue(y.foo is not x.foo)
401 def test_deepcopy_reflexive_inst(self):
402 class C:
403 pass
404 x = C()
405 x.foo = x
406 y = copy.deepcopy(x)
407 self.assertTrue(y is not x)
408 self.assertTrue(y.foo is y)
410 # _reconstruct()
412 def test_reconstruct_string(self):
413 class C(object):
414 def __reduce__(self):
415 return ""
416 x = C()
417 y = copy.copy(x)
418 self.assertTrue(y is x)
419 y = copy.deepcopy(x)
420 self.assertTrue(y is x)
422 def test_reconstruct_nostate(self):
423 class C(object):
424 def __reduce__(self):
425 return (C, ())
426 x = C()
427 x.foo = 42
428 y = copy.copy(x)
429 self.assertTrue(y.__class__ is x.__class__)
430 y = copy.deepcopy(x)
431 self.assertTrue(y.__class__ is x.__class__)
433 def test_reconstruct_state(self):
434 class C(object):
435 def __reduce__(self):
436 return (C, (), self.__dict__)
437 def __cmp__(self, other):
438 return cmp(self.__dict__, other.__dict__)
439 __hash__ = None # Silence Py3k warning
440 x = C()
441 x.foo = [42]
442 y = copy.copy(x)
443 self.assertEqual(y, x)
444 y = copy.deepcopy(x)
445 self.assertEqual(y, x)
446 self.assertTrue(y.foo is not x.foo)
448 def test_reconstruct_state_setstate(self):
449 class C(object):
450 def __reduce__(self):
451 return (C, (), self.__dict__)
452 def __setstate__(self, state):
453 self.__dict__.update(state)
454 def __cmp__(self, other):
455 return cmp(self.__dict__, other.__dict__)
456 __hash__ = None # Silence Py3k warning
457 x = C()
458 x.foo = [42]
459 y = copy.copy(x)
460 self.assertEqual(y, x)
461 y = copy.deepcopy(x)
462 self.assertEqual(y, x)
463 self.assertTrue(y.foo is not x.foo)
465 def test_reconstruct_reflexive(self):
466 class C(object):
467 pass
468 x = C()
469 x.foo = x
470 y = copy.deepcopy(x)
471 self.assertTrue(y is not x)
472 self.assertTrue(y.foo is y)
474 # Additions for Python 2.3 and pickle protocol 2
476 def test_reduce_4tuple(self):
477 class C(list):
478 def __reduce__(self):
479 return (C, (), self.__dict__, iter(self))
480 def __cmp__(self, other):
481 return (cmp(list(self), list(other)) or
482 cmp(self.__dict__, other.__dict__))
483 __hash__ = None # Silence Py3k warning
484 x = C([[1, 2], 3])
485 y = copy.copy(x)
486 self.assertEqual(x, y)
487 self.assertTrue(x is not y)
488 self.assertTrue(x[0] is y[0])
489 y = copy.deepcopy(x)
490 self.assertEqual(x, y)
491 self.assertTrue(x is not y)
492 self.assertTrue(x[0] is not y[0])
494 def test_reduce_5tuple(self):
495 class C(dict):
496 def __reduce__(self):
497 return (C, (), self.__dict__, None, self.iteritems())
498 def __cmp__(self, other):
499 return (cmp(dict(self), list(dict)) or
500 cmp(self.__dict__, other.__dict__))
501 __hash__ = None # Silence Py3k warning
502 x = C([("foo", [1, 2]), ("bar", 3)])
503 y = copy.copy(x)
504 self.assertEqual(x, y)
505 self.assertTrue(x is not y)
506 self.assertTrue(x["foo"] is y["foo"])
507 y = copy.deepcopy(x)
508 self.assertEqual(x, y)
509 self.assertTrue(x is not y)
510 self.assertTrue(x["foo"] is not y["foo"])
512 def test_copy_slots(self):
513 class C(object):
514 __slots__ = ["foo"]
515 x = C()
516 x.foo = [42]
517 y = copy.copy(x)
518 self.assertTrue(x.foo is y.foo)
520 def test_deepcopy_slots(self):
521 class C(object):
522 __slots__ = ["foo"]
523 x = C()
524 x.foo = [42]
525 y = copy.deepcopy(x)
526 self.assertEqual(x.foo, y.foo)
527 self.assertTrue(x.foo is not y.foo)
529 def test_copy_list_subclass(self):
530 class C(list):
531 pass
532 x = C([[1, 2], 3])
533 x.foo = [4, 5]
534 y = copy.copy(x)
535 self.assertEqual(list(x), list(y))
536 self.assertEqual(x.foo, y.foo)
537 self.assertTrue(x[0] is y[0])
538 self.assertTrue(x.foo is y.foo)
540 def test_deepcopy_list_subclass(self):
541 class C(list):
542 pass
543 x = C([[1, 2], 3])
544 x.foo = [4, 5]
545 y = copy.deepcopy(x)
546 self.assertEqual(list(x), list(y))
547 self.assertEqual(x.foo, y.foo)
548 self.assertTrue(x[0] is not y[0])
549 self.assertTrue(x.foo is not y.foo)
551 def test_copy_tuple_subclass(self):
552 class C(tuple):
553 pass
554 x = C([1, 2, 3])
555 self.assertEqual(tuple(x), (1, 2, 3))
556 y = copy.copy(x)
557 self.assertEqual(tuple(y), (1, 2, 3))
559 def test_deepcopy_tuple_subclass(self):
560 class C(tuple):
561 pass
562 x = C([[1, 2], 3])
563 self.assertEqual(tuple(x), ([1, 2], 3))
564 y = copy.deepcopy(x)
565 self.assertEqual(tuple(y), ([1, 2], 3))
566 self.assertTrue(x is not y)
567 self.assertTrue(x[0] is not y[0])
569 def test_getstate_exc(self):
570 class EvilState(object):
571 def __getstate__(self):
572 raise ValueError, "ain't got no stickin' state"
573 self.assertRaises(ValueError, copy.copy, EvilState())
575 def test_copy_function(self):
576 self.assertEqual(copy.copy(global_foo), global_foo)
577 def foo(x, y): return x+y
578 self.assertEqual(copy.copy(foo), foo)
579 bar = lambda: None
580 self.assertEqual(copy.copy(bar), bar)
582 def test_deepcopy_function(self):
583 self.assertEqual(copy.deepcopy(global_foo), global_foo)
584 def foo(x, y): return x+y
585 self.assertEqual(copy.deepcopy(foo), foo)
586 bar = lambda: None
587 self.assertEqual(copy.deepcopy(bar), bar)
589 def _check_weakref(self, _copy):
590 class C(object):
591 pass
592 obj = C()
593 x = weakref.ref(obj)
594 y = _copy(x)
595 self.assertTrue(y is x)
596 del obj
597 y = _copy(x)
598 self.assertTrue(y is x)
600 def test_copy_weakref(self):
601 self._check_weakref(copy.copy)
603 def test_deepcopy_weakref(self):
604 self._check_weakref(copy.deepcopy)
606 def _check_copy_weakdict(self, _dicttype):
607 class C(object):
608 pass
609 a, b, c, d = [C() for i in xrange(4)]
610 u = _dicttype()
611 u[a] = b
612 u[c] = d
613 v = copy.copy(u)
614 self.assertFalse(v is u)
615 self.assertEqual(v, u)
616 self.assertEqual(v[a], b)
617 self.assertEqual(v[c], d)
618 self.assertEqual(len(v), 2)
619 del c, d
620 self.assertEqual(len(v), 1)
621 x, y = C(), C()
622 # The underlying containers are decoupled
623 v[x] = y
624 self.assertNotIn(x, u)
626 def test_copy_weakkeydict(self):
627 self._check_copy_weakdict(weakref.WeakKeyDictionary)
629 def test_copy_weakvaluedict(self):
630 self._check_copy_weakdict(weakref.WeakValueDictionary)
632 def test_deepcopy_weakkeydict(self):
633 class C(object):
634 def __init__(self, i):
635 self.i = i
636 a, b, c, d = [C(i) for i in xrange(4)]
637 u = weakref.WeakKeyDictionary()
638 u[a] = b
639 u[c] = d
640 # Keys aren't copied, values are
641 v = copy.deepcopy(u)
642 self.assertNotEqual(v, u)
643 self.assertEqual(len(v), 2)
644 self.assertFalse(v[a] is b)
645 self.assertFalse(v[c] is d)
646 self.assertEqual(v[a].i, b.i)
647 self.assertEqual(v[c].i, d.i)
648 del c
649 self.assertEqual(len(v), 1)
651 def test_deepcopy_weakvaluedict(self):
652 class C(object):
653 def __init__(self, i):
654 self.i = i
655 a, b, c, d = [C(i) for i in xrange(4)]
656 u = weakref.WeakValueDictionary()
657 u[a] = b
658 u[c] = d
659 # Keys are copied, values aren't
660 v = copy.deepcopy(u)
661 self.assertNotEqual(v, u)
662 self.assertEqual(len(v), 2)
663 (x, y), (z, t) = sorted(v.items(), key=lambda pair: pair[0].i)
664 self.assertFalse(x is a)
665 self.assertEqual(x.i, a.i)
666 self.assertTrue(y is b)
667 self.assertFalse(z is c)
668 self.assertEqual(z.i, c.i)
669 self.assertTrue(t is d)
670 del x, y, z, t
671 del d
672 self.assertEqual(len(v), 1)
674 def test_deepcopy_bound_method(self):
675 class Foo(object):
676 def m(self):
677 pass
678 f = Foo()
679 f.b = f.m
680 g = copy.deepcopy(f)
681 self.assertEqual(g.m, g.b)
682 self.assertTrue(g.b.im_self is g)
683 g.b()
686 def global_foo(x, y): return x+y
688 def test_main():
689 test_support.run_unittest(TestCopy)
691 if __name__ == "__main__":
692 test_main()