Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_copy.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | 8ece29c8bba2 |
| children |
| rev | line source |
|---|---|
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 """Unit tests for the copy module.""" |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 import copy |
|
47189
f419b2e96a98
Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents:
46865
diff
changeset
|
4 import copy_reg |
|
54099
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
5 import weakref |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 import unittest |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 from test import test_support |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 class TestCopy(unittest.TestCase): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 # Attempt full line coverage of copy.py from top to bottom |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 def test_exceptions(self): |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
15 self.assertTrue(copy.Error is copy.error) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
16 self.assertTrue(issubclass(copy.Error, Exception)) |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 # The copy() method |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 def test_copy_basic(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 x = 42 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 y = copy.copy(x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 self.assertEqual(x, y) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 def test_copy_copy(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 class C(object): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 def __init__(self, foo): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 self.foo = foo |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 def __copy__(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 return C(self.foo) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 x = C(42) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 y = copy.copy(x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 self.assertEqual(y.__class__, x.__class__) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 self.assertEqual(y.foo, x.foo) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 |
|
27754
6b1789ac2f95
Add support for copy_reg.dispatch_table.
Guido van Rossum <guido@python.org>
parents:
27736
diff
changeset
|
36 def test_copy_registry(self): |
|
6b1789ac2f95
Add support for copy_reg.dispatch_table.
Guido van Rossum <guido@python.org>
parents:
27736
diff
changeset
|
37 class C(object): |
|
6b1789ac2f95
Add support for copy_reg.dispatch_table.
Guido van Rossum <guido@python.org>
parents:
27736
diff
changeset
|
38 def __new__(cls, foo): |
|
6b1789ac2f95
Add support for copy_reg.dispatch_table.
Guido van Rossum <guido@python.org>
parents:
27736
diff
changeset
|
39 obj = object.__new__(cls) |
|
6b1789ac2f95
Add support for copy_reg.dispatch_table.
Guido van Rossum <guido@python.org>
parents:
27736
diff
changeset
|
40 obj.foo = foo |
|
6b1789ac2f95
Add support for copy_reg.dispatch_table.
Guido van Rossum <guido@python.org>
parents:
27736
diff
changeset
|
41 return obj |
|
6b1789ac2f95
Add support for copy_reg.dispatch_table.
Guido van Rossum <guido@python.org>
parents:
27736
diff
changeset
|
42 def pickle_C(obj): |
|
6b1789ac2f95
Add support for copy_reg.dispatch_table.
Guido van Rossum <guido@python.org>
parents:
27736
diff
changeset
|
43 return (C, (obj.foo,)) |
|
6b1789ac2f95
Add support for copy_reg.dispatch_table.
Guido van Rossum <guido@python.org>
parents:
27736
diff
changeset
|
44 x = C(42) |
|
6b1789ac2f95
Add support for copy_reg.dispatch_table.
Guido van Rossum <guido@python.org>
parents:
27736
diff
changeset
|
45 self.assertRaises(TypeError, copy.copy, x) |
|
47189
f419b2e96a98
Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents:
46865
diff
changeset
|
46 copy_reg.pickle(C, pickle_C, C) |
|
27754
6b1789ac2f95
Add support for copy_reg.dispatch_table.
Guido van Rossum <guido@python.org>
parents:
27736
diff
changeset
|
47 y = copy.copy(x) |
|
6b1789ac2f95
Add support for copy_reg.dispatch_table.
Guido van Rossum <guido@python.org>
parents:
27736
diff
changeset
|
48 |
|
27959
cc4ad9afa21d
Use __reduce_ex__ in copy.py. The test_*copy_cant() tests are simpler again.
Guido van Rossum <guido@python.org>
parents:
27755
diff
changeset
|
49 def test_copy_reduce_ex(self): |
|
cc4ad9afa21d
Use __reduce_ex__ in copy.py. The test_*copy_cant() tests are simpler again.
Guido van Rossum <guido@python.org>
parents:
27755
diff
changeset
|
50 class C(object): |
|
cc4ad9afa21d
Use __reduce_ex__ in copy.py. The test_*copy_cant() tests are simpler again.
Guido van Rossum <guido@python.org>
parents:
27755
diff
changeset
|
51 def __reduce_ex__(self, proto): |
|
cc4ad9afa21d
Use __reduce_ex__ in copy.py. The test_*copy_cant() tests are simpler again.
Guido van Rossum <guido@python.org>
parents:
27755
diff
changeset
|
52 return "" |
|
cc4ad9afa21d
Use __reduce_ex__ in copy.py. The test_*copy_cant() tests are simpler again.
Guido van Rossum <guido@python.org>
parents:
27755
diff
changeset
|
53 def __reduce__(self): |
|
cc4ad9afa21d
Use __reduce_ex__ in copy.py. The test_*copy_cant() tests are simpler again.
Guido van Rossum <guido@python.org>
parents:
27755
diff
changeset
|
54 raise test_support.TestFailed, "shouldn't call this" |
|
cc4ad9afa21d
Use __reduce_ex__ in copy.py. The test_*copy_cant() tests are simpler again.
Guido van Rossum <guido@python.org>
parents:
27755
diff
changeset
|
55 x = C() |
|
cc4ad9afa21d
Use __reduce_ex__ in copy.py. The test_*copy_cant() tests are simpler again.
Guido van Rossum <guido@python.org>
parents:
27755
diff
changeset
|
56 y = copy.copy(x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
57 self.assertTrue(y is x) |
|
27959
cc4ad9afa21d
Use __reduce_ex__ in copy.py. The test_*copy_cant() tests are simpler again.
Guido van Rossum <guido@python.org>
parents:
27755
diff
changeset
|
58 |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 def test_copy_reduce(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 class C(object): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 def __reduce__(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 return "" |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 x = C() |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 y = copy.copy(x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
65 self.assertTrue(y is x) |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 def test_copy_cant(self): |
|
27959
cc4ad9afa21d
Use __reduce_ex__ in copy.py. The test_*copy_cant() tests are simpler again.
Guido van Rossum <guido@python.org>
parents:
27755
diff
changeset
|
68 class C(object): |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 def __getattribute__(self, name): |
|
27959
cc4ad9afa21d
Use __reduce_ex__ in copy.py. The test_*copy_cant() tests are simpler again.
Guido van Rossum <guido@python.org>
parents:
27755
diff
changeset
|
70 if name.startswith("__reduce"): |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 raise AttributeError, name |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 return object.__getattribute__(self, name) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 x = C() |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 self.assertRaises(copy.Error, copy.copy, x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 # Type-specific _copy_xxx() methods |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 def test_copy_atomic(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 class Classic: |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 pass |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 class NewStyle(object): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 pass |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 def f(): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 pass |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 tests = [None, 42, 2L**100, 3.14, True, False, 1j, |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 "hello", u"hello\u1234", f.func_code, |
|
29458
8bed55203559
Copy builtin functions as atomic. Fixes #746304. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents:
29435
diff
changeset
|
87 NewStyle, xrange(10), Classic, max] |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 for x in tests: |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
89 self.assertTrue(copy.copy(x) is x, repr(x)) |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 def test_copy_list(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 x = [1, 2, 3] |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
93 self.assertEqual(copy.copy(x), x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 def test_copy_tuple(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 x = (1, 2, 3) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 self.assertEqual(copy.copy(x), x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 def test_copy_dict(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 x = {"foo": 1, "bar": 2} |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 self.assertEqual(copy.copy(x), x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 def test_copy_inst_vanilla(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 class C: |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 def __init__(self, foo): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
106 self.foo = foo |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 def __cmp__(self, other): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 return cmp(self.foo, other.foo) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 x = C(42) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 self.assertEqual(copy.copy(x), x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 def test_copy_inst_copy(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 class C: |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 def __init__(self, foo): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 self.foo = foo |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 def __copy__(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 return C(self.foo) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 def __cmp__(self, other): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 return cmp(self.foo, other.foo) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 x = C(42) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 self.assertEqual(copy.copy(x), x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 def test_copy_inst_getinitargs(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 class C: |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 def __init__(self, foo): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 self.foo = foo |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 def __getinitargs__(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 return (self.foo,) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 def __cmp__(self, other): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 return cmp(self.foo, other.foo) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 x = C(42) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 self.assertEqual(copy.copy(x), x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 def test_copy_inst_getstate(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 class C: |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 def __init__(self, foo): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 self.foo = foo |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 def __getstate__(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 return {"foo": self.foo} |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 def __cmp__(self, other): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 return cmp(self.foo, other.foo) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 x = C(42) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 self.assertEqual(copy.copy(x), x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 def test_copy_inst_setstate(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 class C: |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 def __init__(self, foo): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 self.foo = foo |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 def __setstate__(self, state): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 self.foo = state["foo"] |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 def __cmp__(self, other): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 return cmp(self.foo, other.foo) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 x = C(42) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 self.assertEqual(copy.copy(x), x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 def test_copy_inst_getstate_setstate(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 class C: |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 def __init__(self, foo): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 self.foo = foo |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 def __getstate__(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
161 return self.foo |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 def __setstate__(self, state): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 self.foo = state |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
164 def __cmp__(self, other): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
165 return cmp(self.foo, other.foo) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
166 x = C(42) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
167 self.assertEqual(copy.copy(x), x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
168 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
169 # The deepcopy() method |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
170 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 def test_deepcopy_basic(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 x = 42 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 y = copy.deepcopy(x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 self.assertEqual(y, x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 def test_deepcopy_memo(self): |
|
29435
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
177 # Tests of reflexive objects are under type-specific sections below. |
|
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
178 # This tests only repetitions of objects. |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 x = [] |
|
29435
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
180 x = [x, x] |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 y = copy.deepcopy(x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 self.assertEqual(y, x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
183 self.assertTrue(y is not x) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
184 self.assertTrue(y[0] is not x[0]) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
185 self.assertTrue(y[0] is y[1]) |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
186 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
187 def test_deepcopy_issubclass(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
188 # XXX Note: there's no way to test the TypeError coming out of |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 # issubclass() -- this can only happen when an extension |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
190 # module defines a "type" that doesn't formally inherit from |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
191 # type. |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
192 class Meta(type): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
193 pass |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 class C: |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 __metaclass__ = Meta |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 self.assertEqual(copy.deepcopy(C), C) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 def test_deepcopy_deepcopy(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 class C(object): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 def __init__(self, foo): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
201 self.foo = foo |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
202 def __deepcopy__(self, memo=None): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 return C(self.foo) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 x = C(42) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 y = copy.deepcopy(x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
206 self.assertEqual(y.__class__, x.__class__) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
207 self.assertEqual(y.foo, x.foo) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
208 |
|
27754
6b1789ac2f95
Add support for copy_reg.dispatch_table.
Guido van Rossum <guido@python.org>
parents:
27736
diff
changeset
|
209 def test_deepcopy_registry(self): |
|
6b1789ac2f95
Add support for copy_reg.dispatch_table.
Guido van Rossum <guido@python.org>
parents:
27736
diff
changeset
|
210 class C(object): |
|
6b1789ac2f95
Add support for copy_reg.dispatch_table.
Guido van Rossum <guido@python.org>
parents:
27736
diff
changeset
|
211 def __new__(cls, foo): |
|
6b1789ac2f95
Add support for copy_reg.dispatch_table.
Guido van Rossum <guido@python.org>
parents:
27736
diff
changeset
|
212 obj = object.__new__(cls) |
|
6b1789ac2f95
Add support for copy_reg.dispatch_table.
Guido van Rossum <guido@python.org>
parents:
27736
diff
changeset
|
213 obj.foo = foo |
|
6b1789ac2f95
Add support for copy_reg.dispatch_table.
Guido van Rossum <guido@python.org>
parents:
27736
diff
changeset
|
214 return obj |
|
6b1789ac2f95
Add support for copy_reg.dispatch_table.
Guido van Rossum <guido@python.org>
parents:
27736
diff
changeset
|
215 def pickle_C(obj): |
|
6b1789ac2f95
Add support for copy_reg.dispatch_table.
Guido van Rossum <guido@python.org>
parents:
27736
diff
changeset
|
216 return (C, (obj.foo,)) |
|
6b1789ac2f95
Add support for copy_reg.dispatch_table.
Guido van Rossum <guido@python.org>
parents:
27736
diff
changeset
|
217 x = C(42) |
|
6b1789ac2f95
Add support for copy_reg.dispatch_table.
Guido van Rossum <guido@python.org>
parents:
27736
diff
changeset
|
218 self.assertRaises(TypeError, copy.deepcopy, x) |
|
47189
f419b2e96a98
Revert copy_reg -> copyreg rename.
Georg Brandl <georg@python.org>
parents:
46865
diff
changeset
|
219 copy_reg.pickle(C, pickle_C, C) |
|
27754
6b1789ac2f95
Add support for copy_reg.dispatch_table.
Guido van Rossum <guido@python.org>
parents:
27736
diff
changeset
|
220 y = copy.deepcopy(x) |
|
6b1789ac2f95
Add support for copy_reg.dispatch_table.
Guido van Rossum <guido@python.org>
parents:
27736
diff
changeset
|
221 |
|
27959
cc4ad9afa21d
Use __reduce_ex__ in copy.py. The test_*copy_cant() tests are simpler again.
Guido van Rossum <guido@python.org>
parents:
27755
diff
changeset
|
222 def test_deepcopy_reduce_ex(self): |
|
cc4ad9afa21d
Use __reduce_ex__ in copy.py. The test_*copy_cant() tests are simpler again.
Guido van Rossum <guido@python.org>
parents:
27755
diff
changeset
|
223 class C(object): |
|
cc4ad9afa21d
Use __reduce_ex__ in copy.py. The test_*copy_cant() tests are simpler again.
Guido van Rossum <guido@python.org>
parents:
27755
diff
changeset
|
224 def __reduce_ex__(self, proto): |
|
cc4ad9afa21d
Use __reduce_ex__ in copy.py. The test_*copy_cant() tests are simpler again.
Guido van Rossum <guido@python.org>
parents:
27755
diff
changeset
|
225 return "" |
|
cc4ad9afa21d
Use __reduce_ex__ in copy.py. The test_*copy_cant() tests are simpler again.
Guido van Rossum <guido@python.org>
parents:
27755
diff
changeset
|
226 def __reduce__(self): |
|
cc4ad9afa21d
Use __reduce_ex__ in copy.py. The test_*copy_cant() tests are simpler again.
Guido van Rossum <guido@python.org>
parents:
27755
diff
changeset
|
227 raise test_support.TestFailed, "shouldn't call this" |
|
cc4ad9afa21d
Use __reduce_ex__ in copy.py. The test_*copy_cant() tests are simpler again.
Guido van Rossum <guido@python.org>
parents:
27755
diff
changeset
|
228 x = C() |
|
cc4ad9afa21d
Use __reduce_ex__ in copy.py. The test_*copy_cant() tests are simpler again.
Guido van Rossum <guido@python.org>
parents:
27755
diff
changeset
|
229 y = copy.deepcopy(x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
230 self.assertTrue(y is x) |
|
27959
cc4ad9afa21d
Use __reduce_ex__ in copy.py. The test_*copy_cant() tests are simpler again.
Guido van Rossum <guido@python.org>
parents:
27755
diff
changeset
|
231 |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 def test_deepcopy_reduce(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 class C(object): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
234 def __reduce__(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 return "" |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
236 x = C() |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 y = copy.deepcopy(x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
238 self.assertTrue(y is x) |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
239 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
240 def test_deepcopy_cant(self): |
|
27959
cc4ad9afa21d
Use __reduce_ex__ in copy.py. The test_*copy_cant() tests are simpler again.
Guido van Rossum <guido@python.org>
parents:
27755
diff
changeset
|
241 class C(object): |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
242 def __getattribute__(self, name): |
|
27959
cc4ad9afa21d
Use __reduce_ex__ in copy.py. The test_*copy_cant() tests are simpler again.
Guido van Rossum <guido@python.org>
parents:
27755
diff
changeset
|
243 if name.startswith("__reduce"): |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
244 raise AttributeError, name |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
245 return object.__getattribute__(self, name) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
246 x = C() |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
247 self.assertRaises(copy.Error, copy.deepcopy, x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
248 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
249 # Type-specific _deepcopy_xxx() methods |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
250 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
251 def test_deepcopy_atomic(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
252 class Classic: |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
253 pass |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
254 class NewStyle(object): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
255 pass |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 def f(): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 pass |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
258 tests = [None, 42, 2L**100, 3.14, True, False, 1j, |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 "hello", u"hello\u1234", f.func_code, |
|
29458
8bed55203559
Copy builtin functions as atomic. Fixes #746304. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents:
29435
diff
changeset
|
260 NewStyle, xrange(10), Classic, max] |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
261 for x in tests: |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
262 self.assertTrue(copy.deepcopy(x) is x, repr(x)) |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
263 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
264 def test_deepcopy_list(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
265 x = [[1, 2], 3] |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
266 y = copy.deepcopy(x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
267 self.assertEqual(y, x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
268 self.assertTrue(x is not y) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
269 self.assertTrue(x[0] is not y[0]) |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
270 |
|
29435
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
271 def test_deepcopy_reflexive_list(self): |
|
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
272 x = [] |
|
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
273 x.append(x) |
|
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
274 y = copy.deepcopy(x) |
|
30801
c42c40034536
Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents:
29458
diff
changeset
|
275 self.assertRaises(RuntimeError, cmp, y, x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
276 self.assertTrue(y is not x) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
277 self.assertTrue(y[0] is y) |
|
30801
c42c40034536
Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents:
29458
diff
changeset
|
278 self.assertEqual(len(y), 1) |
|
29435
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
279 |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
280 def test_deepcopy_tuple(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
281 x = ([1, 2], 3) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
282 y = copy.deepcopy(x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
283 self.assertEqual(y, x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
284 self.assertTrue(x is not y) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
285 self.assertTrue(x[0] is not y[0]) |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
286 |
|
29435
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
287 def test_deepcopy_reflexive_tuple(self): |
|
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
288 x = ([],) |
|
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
289 x[0].append(x) |
|
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
290 y = copy.deepcopy(x) |
|
30801
c42c40034536
Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents:
29458
diff
changeset
|
291 self.assertRaises(RuntimeError, cmp, y, x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
292 self.assertTrue(y is not x) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
293 self.assertTrue(y[0] is not x[0]) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
294 self.assertTrue(y[0][0] is y) |
|
29435
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
295 |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
296 def test_deepcopy_dict(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
297 x = {"foo": [1, 2], "bar": 3} |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
298 y = copy.deepcopy(x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
299 self.assertEqual(y, x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
300 self.assertTrue(x is not y) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
301 self.assertTrue(x["foo"] is not y["foo"]) |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
302 |
|
29435
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
303 def test_deepcopy_reflexive_dict(self): |
|
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
304 x = {} |
|
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
305 x['foo'] = x |
|
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
306 y = copy.deepcopy(x) |
|
30801
c42c40034536
Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents:
29458
diff
changeset
|
307 self.assertRaises(RuntimeError, cmp, y, x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
308 self.assertTrue(y is not x) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
309 self.assertTrue(y['foo'] is y) |
|
30801
c42c40034536
Deleting cyclic object comparison.
Armin Rigo <arigo@tunes.org>
parents:
29458
diff
changeset
|
310 self.assertEqual(len(y), 1) |
|
29435
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
311 |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
312 def test_deepcopy_keepalive(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
313 memo = {} |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
314 x = 42 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
315 y = copy.deepcopy(x, memo) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
316 self.assertTrue(memo[id(x)] is x) |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
317 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
318 def test_deepcopy_inst_vanilla(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
319 class C: |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
320 def __init__(self, foo): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
321 self.foo = foo |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
322 def __cmp__(self, other): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
323 return cmp(self.foo, other.foo) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
324 x = C([42]) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
325 y = copy.deepcopy(x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
326 self.assertEqual(y, x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
327 self.assertTrue(y.foo is not x.foo) |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
328 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
329 def test_deepcopy_inst_deepcopy(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
330 class C: |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
331 def __init__(self, foo): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
332 self.foo = foo |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
333 def __deepcopy__(self, memo): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
334 return C(copy.deepcopy(self.foo, memo)) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
335 def __cmp__(self, other): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
336 return cmp(self.foo, other.foo) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
337 x = C([42]) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
338 y = copy.deepcopy(x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
339 self.assertEqual(y, x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
340 self.assertTrue(y is not x) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
341 self.assertTrue(y.foo is not x.foo) |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
342 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
343 def test_deepcopy_inst_getinitargs(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
344 class C: |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
345 def __init__(self, foo): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
346 self.foo = foo |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
347 def __getinitargs__(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
348 return (self.foo,) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
349 def __cmp__(self, other): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
350 return cmp(self.foo, other.foo) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
351 x = C([42]) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
352 y = copy.deepcopy(x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
353 self.assertEqual(y, x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
354 self.assertTrue(y is not x) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
355 self.assertTrue(y.foo is not x.foo) |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
356 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
357 def test_deepcopy_inst_getstate(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
358 class C: |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
359 def __init__(self, foo): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
360 self.foo = foo |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
361 def __getstate__(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
362 return {"foo": self.foo} |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
363 def __cmp__(self, other): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
364 return cmp(self.foo, other.foo) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
365 x = C([42]) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
366 y = copy.deepcopy(x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
367 self.assertEqual(y, x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
368 self.assertTrue(y is not x) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
369 self.assertTrue(y.foo is not x.foo) |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
370 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
371 def test_deepcopy_inst_setstate(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
372 class C: |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
373 def __init__(self, foo): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
374 self.foo = foo |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
375 def __setstate__(self, state): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
376 self.foo = state["foo"] |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
377 def __cmp__(self, other): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
378 return cmp(self.foo, other.foo) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
379 x = C([42]) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
380 y = copy.deepcopy(x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
381 self.assertEqual(y, x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
382 self.assertTrue(y is not x) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
383 self.assertTrue(y.foo is not x.foo) |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
384 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
385 def test_deepcopy_inst_getstate_setstate(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
386 class C: |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
387 def __init__(self, foo): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
388 self.foo = foo |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
389 def __getstate__(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
390 return self.foo |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
391 def __setstate__(self, state): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
392 self.foo = state |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
393 def __cmp__(self, other): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
394 return cmp(self.foo, other.foo) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
395 x = C([42]) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
396 y = copy.deepcopy(x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
397 self.assertEqual(y, x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
398 self.assertTrue(y is not x) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
399 self.assertTrue(y.foo is not x.foo) |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
400 |
|
29435
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
401 def test_deepcopy_reflexive_inst(self): |
|
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
402 class C: |
|
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
403 pass |
|
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
404 x = C() |
|
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
405 x.foo = x |
|
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
406 y = copy.deepcopy(x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
407 self.assertTrue(y is not x) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
408 self.assertTrue(y.foo is y) |
|
29435
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
409 |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
410 # _reconstruct() |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
411 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
412 def test_reconstruct_string(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
413 class C(object): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
414 def __reduce__(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
415 return "" |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
416 x = C() |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
417 y = copy.copy(x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
418 self.assertTrue(y is x) |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
419 y = copy.deepcopy(x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
420 self.assertTrue(y is x) |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
421 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
422 def test_reconstruct_nostate(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
423 class C(object): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
424 def __reduce__(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
425 return (C, ()) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
426 x = C() |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
427 x.foo = 42 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
428 y = copy.copy(x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
429 self.assertTrue(y.__class__ is x.__class__) |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
430 y = copy.deepcopy(x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
431 self.assertTrue(y.__class__ is x.__class__) |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
432 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
433 def test_reconstruct_state(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
434 class C(object): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
435 def __reduce__(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
436 return (C, (), self.__dict__) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
437 def __cmp__(self, other): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
438 return cmp(self.__dict__, other.__dict__) |
|
48213
7527da2b05e8
Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47189
diff
changeset
|
439 __hash__ = None # Silence Py3k warning |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
440 x = C() |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
441 x.foo = [42] |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
442 y = copy.copy(x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
443 self.assertEqual(y, x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
444 y = copy.deepcopy(x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
445 self.assertEqual(y, x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
446 self.assertTrue(y.foo is not x.foo) |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
447 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
448 def test_reconstruct_state_setstate(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
449 class C(object): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
450 def __reduce__(self): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
451 return (C, (), self.__dict__) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
452 def __setstate__(self, state): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
453 self.__dict__.update(state) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
454 def __cmp__(self, other): |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
455 return cmp(self.__dict__, other.__dict__) |
|
48213
7527da2b05e8
Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47189
diff
changeset
|
456 __hash__ = None # Silence Py3k warning |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
457 x = C() |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
458 x.foo = [42] |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
459 y = copy.copy(x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
460 self.assertEqual(y, x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
461 y = copy.deepcopy(x) |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
462 self.assertEqual(y, x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
463 self.assertTrue(y.foo is not x.foo) |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
464 |
|
29435
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
465 def test_reconstruct_reflexive(self): |
|
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
466 class C(object): |
|
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
467 pass |
|
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
468 x = C() |
|
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
469 x.foo = x |
|
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
470 y = copy.deepcopy(x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
471 self.assertTrue(y is not x) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
472 self.assertTrue(y.foo is y) |
|
29435
0ba7c101648a
SF patch 707900, fixing bug 702858, by Steven Taschuk.
Guido van Rossum <guido@python.org>
parents:
29382
diff
changeset
|
473 |
|
27727
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
474 # Additions for Python 2.3 and pickle protocol 2 |
|
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
475 |
|
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
476 def test_reduce_4tuple(self): |
|
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
477 class C(list): |
|
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
478 def __reduce__(self): |
|
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
479 return (C, (), self.__dict__, iter(self)) |
|
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
480 def __cmp__(self, other): |
|
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
481 return (cmp(list(self), list(other)) or |
|
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
482 cmp(self.__dict__, other.__dict__)) |
|
48213
7527da2b05e8
Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47189
diff
changeset
|
483 __hash__ = None # Silence Py3k warning |
|
27727
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
484 x = C([[1, 2], 3]) |
|
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
485 y = copy.copy(x) |
|
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
486 self.assertEqual(x, y) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
487 self.assertTrue(x is not y) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
488 self.assertTrue(x[0] is y[0]) |
|
27727
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
489 y = copy.deepcopy(x) |
|
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
490 self.assertEqual(x, y) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
491 self.assertTrue(x is not y) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
492 self.assertTrue(x[0] is not y[0]) |
|
27727
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
493 |
|
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
494 def test_reduce_5tuple(self): |
|
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
495 class C(dict): |
|
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
496 def __reduce__(self): |
|
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
497 return (C, (), self.__dict__, None, self.iteritems()) |
|
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
498 def __cmp__(self, other): |
|
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
499 return (cmp(dict(self), list(dict)) or |
|
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
500 cmp(self.__dict__, other.__dict__)) |
|
48213
7527da2b05e8
Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47189
diff
changeset
|
501 __hash__ = None # Silence Py3k warning |
|
27727
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
502 x = C([("foo", [1, 2]), ("bar", 3)]) |
|
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
503 y = copy.copy(x) |
|
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
504 self.assertEqual(x, y) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
505 self.assertTrue(x is not y) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
506 self.assertTrue(x["foo"] is y["foo"]) |
|
27727
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
507 y = copy.deepcopy(x) |
|
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
508 self.assertEqual(x, y) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
509 self.assertTrue(x is not y) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
510 self.assertTrue(x["foo"] is not y["foo"]) |
|
27727
d502187b2b06
Support __reduce__ returning a 4-tuple or 5-tuple.
Guido van Rossum <guido@python.org>
parents:
27725
diff
changeset
|
511 |
|
27733
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
512 def test_copy_slots(self): |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
513 class C(object): |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
514 __slots__ = ["foo"] |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
515 x = C() |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
516 x.foo = [42] |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
517 y = copy.copy(x) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
518 self.assertTrue(x.foo is y.foo) |
|
27733
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
519 |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
520 def test_deepcopy_slots(self): |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
521 class C(object): |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
522 __slots__ = ["foo"] |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
523 x = C() |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
524 x.foo = [42] |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
525 y = copy.deepcopy(x) |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
526 self.assertEqual(x.foo, y.foo) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
527 self.assertTrue(x.foo is not y.foo) |
|
27733
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
528 |
|
64437
8ece29c8bba2
Merged revisions 84495-84496 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58817
diff
changeset
|
529 def test_deepcopy_dict_subclass(self): |
|
8ece29c8bba2
Merged revisions 84495-84496 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58817
diff
changeset
|
530 class C(dict): |
|
8ece29c8bba2
Merged revisions 84495-84496 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58817
diff
changeset
|
531 def __init__(self, d=None): |
|
8ece29c8bba2
Merged revisions 84495-84496 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58817
diff
changeset
|
532 if not d: |
|
8ece29c8bba2
Merged revisions 84495-84496 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58817
diff
changeset
|
533 d = {} |
|
8ece29c8bba2
Merged revisions 84495-84496 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58817
diff
changeset
|
534 self._keys = list(d.keys()) |
|
8ece29c8bba2
Merged revisions 84495-84496 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58817
diff
changeset
|
535 dict.__init__(self, d) |
|
8ece29c8bba2
Merged revisions 84495-84496 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58817
diff
changeset
|
536 def __setitem__(self, key, item): |
|
8ece29c8bba2
Merged revisions 84495-84496 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58817
diff
changeset
|
537 dict.__setitem__(self, key, item) |
|
8ece29c8bba2
Merged revisions 84495-84496 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58817
diff
changeset
|
538 if key not in self._keys: |
|
8ece29c8bba2
Merged revisions 84495-84496 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58817
diff
changeset
|
539 self._keys.append(key) |
|
8ece29c8bba2
Merged revisions 84495-84496 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58817
diff
changeset
|
540 x = C(d={'foo':0}) |
|
8ece29c8bba2
Merged revisions 84495-84496 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58817
diff
changeset
|
541 y = copy.deepcopy(x) |
|
8ece29c8bba2
Merged revisions 84495-84496 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58817
diff
changeset
|
542 self.assertEqual(x, y) |
|
8ece29c8bba2
Merged revisions 84495-84496 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58817
diff
changeset
|
543 self.assertEqual(x._keys, y._keys) |
|
8ece29c8bba2
Merged revisions 84495-84496 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58817
diff
changeset
|
544 self.assertTrue(x is not y) |
|
8ece29c8bba2
Merged revisions 84495-84496 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58817
diff
changeset
|
545 x['bar'] = 1 |
|
8ece29c8bba2
Merged revisions 84495-84496 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58817
diff
changeset
|
546 self.assertNotEqual(x, y) |
|
8ece29c8bba2
Merged revisions 84495-84496 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58817
diff
changeset
|
547 self.assertNotEqual(x._keys, y._keys) |
|
8ece29c8bba2
Merged revisions 84495-84496 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
58817
diff
changeset
|
548 |
|
27733
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
549 def test_copy_list_subclass(self): |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
550 class C(list): |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
551 pass |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
552 x = C([[1, 2], 3]) |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
553 x.foo = [4, 5] |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
554 y = copy.copy(x) |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
555 self.assertEqual(list(x), list(y)) |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
556 self.assertEqual(x.foo, y.foo) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
557 self.assertTrue(x[0] is y[0]) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
558 self.assertTrue(x.foo is y.foo) |
|
27733
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
559 |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
560 def test_deepcopy_list_subclass(self): |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
561 class C(list): |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
562 pass |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
563 x = C([[1, 2], 3]) |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
564 x.foo = [4, 5] |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
565 y = copy.deepcopy(x) |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
566 self.assertEqual(list(x), list(y)) |
|
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
567 self.assertEqual(x.foo, y.foo) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
568 self.assertTrue(x[0] is not y[0]) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
569 self.assertTrue(x.foo is not y.foo) |
|
27733
e5e242cf46b1
Support all the new stuff supported by the new pickle code:
Guido van Rossum <guido@python.org>
parents:
27727
diff
changeset
|
570 |
|
27736
db416bd18d4f
Fix a bug in the way __getnewargs__ was handled.
Guido van Rossum <guido@python.org>
parents:
27733
diff
changeset
|
571 def test_copy_tuple_subclass(self): |
|
db416bd18d4f
Fix a bug in the way __getnewargs__ was handled.
Guido van Rossum <guido@python.org>
parents:
27733
diff
changeset
|
572 class C(tuple): |
|
db416bd18d4f
Fix a bug in the way __getnewargs__ was handled.
Guido van Rossum <guido@python.org>
parents:
27733
diff
changeset
|
573 pass |
|
db416bd18d4f
Fix a bug in the way __getnewargs__ was handled.
Guido van Rossum <guido@python.org>
parents:
27733
diff
changeset
|
574 x = C([1, 2, 3]) |
|
db416bd18d4f
Fix a bug in the way __getnewargs__ was handled.
Guido van Rossum <guido@python.org>
parents:
27733
diff
changeset
|
575 self.assertEqual(tuple(x), (1, 2, 3)) |
|
db416bd18d4f
Fix a bug in the way __getnewargs__ was handled.
Guido van Rossum <guido@python.org>
parents:
27733
diff
changeset
|
576 y = copy.copy(x) |
|
db416bd18d4f
Fix a bug in the way __getnewargs__ was handled.
Guido van Rossum <guido@python.org>
parents:
27733
diff
changeset
|
577 self.assertEqual(tuple(y), (1, 2, 3)) |
|
db416bd18d4f
Fix a bug in the way __getnewargs__ was handled.
Guido van Rossum <guido@python.org>
parents:
27733
diff
changeset
|
578 |
|
db416bd18d4f
Fix a bug in the way __getnewargs__ was handled.
Guido van Rossum <guido@python.org>
parents:
27733
diff
changeset
|
579 def test_deepcopy_tuple_subclass(self): |
|
db416bd18d4f
Fix a bug in the way __getnewargs__ was handled.
Guido van Rossum <guido@python.org>
parents:
27733
diff
changeset
|
580 class C(tuple): |
|
db416bd18d4f
Fix a bug in the way __getnewargs__ was handled.
Guido van Rossum <guido@python.org>
parents:
27733
diff
changeset
|
581 pass |
|
db416bd18d4f
Fix a bug in the way __getnewargs__ was handled.
Guido van Rossum <guido@python.org>
parents:
27733
diff
changeset
|
582 x = C([[1, 2], 3]) |
|
db416bd18d4f
Fix a bug in the way __getnewargs__ was handled.
Guido van Rossum <guido@python.org>
parents:
27733
diff
changeset
|
583 self.assertEqual(tuple(x), ([1, 2], 3)) |
|
db416bd18d4f
Fix a bug in the way __getnewargs__ was handled.
Guido van Rossum <guido@python.org>
parents:
27733
diff
changeset
|
584 y = copy.deepcopy(x) |
|
db416bd18d4f
Fix a bug in the way __getnewargs__ was handled.
Guido van Rossum <guido@python.org>
parents:
27733
diff
changeset
|
585 self.assertEqual(tuple(y), ([1, 2], 3)) |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
586 self.assertTrue(x is not y) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54099
diff
changeset
|
587 self.assertTrue(x[0] is not y[0]) |
|
27736
db416bd18d4f
Fix a bug in the way __getnewargs__ was handled.
Guido van Rossum <guido@python.org>
parents:
27733
diff
changeset
|
588 |
|
29382
585e303a9491
Fix SF #749831, copy raises SystemError when getstate raises exception
Neal Norwitz <nnorwitz@gmail.com>
parents:
28917
diff
changeset
|
589 def test_getstate_exc(self): |
|
585e303a9491
Fix SF #749831, copy raises SystemError when getstate raises exception
Neal Norwitz <nnorwitz@gmail.com>
parents:
28917
diff
changeset
|
590 class EvilState(object): |
|
585e303a9491
Fix SF #749831, copy raises SystemError when getstate raises exception
Neal Norwitz <nnorwitz@gmail.com>
parents:
28917
diff
changeset
|
591 def __getstate__(self): |
|
585e303a9491
Fix SF #749831, copy raises SystemError when getstate raises exception
Neal Norwitz <nnorwitz@gmail.com>
parents:
28917
diff
changeset
|
592 raise ValueError, "ain't got no stickin' state" |
|
585e303a9491
Fix SF #749831, copy raises SystemError when getstate raises exception
Neal Norwitz <nnorwitz@gmail.com>
parents:
28917
diff
changeset
|
593 self.assertRaises(ValueError, copy.copy, EvilState()) |
|
585e303a9491
Fix SF #749831, copy raises SystemError when getstate raises exception
Neal Norwitz <nnorwitz@gmail.com>
parents:
28917
diff
changeset
|
594 |
| 36656 | 595 def test_copy_function(self): |
| 596 self.assertEqual(copy.copy(global_foo), global_foo) | |
| 597 def foo(x, y): return x+y | |
| 598 self.assertEqual(copy.copy(foo), foo) | |
| 599 bar = lambda: None | |
| 600 self.assertEqual(copy.copy(bar), bar) | |
| 601 | |
| 602 def test_deepcopy_function(self): | |
| 603 self.assertEqual(copy.deepcopy(global_foo), global_foo) | |
| 604 def foo(x, y): return x+y | |
| 605 self.assertEqual(copy.deepcopy(foo), foo) | |
| 606 bar = lambda: None | |
| 607 self.assertEqual(copy.deepcopy(bar), bar) | |
| 608 | |
|
54099
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
609 def _check_weakref(self, _copy): |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
610 class C(object): |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
611 pass |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
612 obj = C() |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
613 x = weakref.ref(obj) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
614 y = _copy(x) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
615 self.assertTrue(y is x) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
616 del obj |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
617 y = _copy(x) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
618 self.assertTrue(y is x) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
619 |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
620 def test_copy_weakref(self): |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
621 self._check_weakref(copy.copy) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
622 |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
623 def test_deepcopy_weakref(self): |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
624 self._check_weakref(copy.deepcopy) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
625 |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
626 def _check_copy_weakdict(self, _dicttype): |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
627 class C(object): |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
628 pass |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
629 a, b, c, d = [C() for i in xrange(4)] |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
630 u = _dicttype() |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
631 u[a] = b |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
632 u[c] = d |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
633 v = copy.copy(u) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
634 self.assertFalse(v is u) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
635 self.assertEqual(v, u) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
636 self.assertEqual(v[a], b) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
637 self.assertEqual(v[c], d) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
638 self.assertEqual(len(v), 2) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
639 del c, d |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
640 self.assertEqual(len(v), 1) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
641 x, y = C(), C() |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
642 # The underlying containers are decoupled |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
643 v[x] = y |
|
58460
17891566a478
use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58198
diff
changeset
|
644 self.assertNotIn(x, u) |
|
54099
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
645 |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
646 def test_copy_weakkeydict(self): |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
647 self._check_copy_weakdict(weakref.WeakKeyDictionary) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
648 |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
649 def test_copy_weakvaluedict(self): |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
650 self._check_copy_weakdict(weakref.WeakValueDictionary) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
651 |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
652 def test_deepcopy_weakkeydict(self): |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
653 class C(object): |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
654 def __init__(self, i): |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
655 self.i = i |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
656 a, b, c, d = [C(i) for i in xrange(4)] |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
657 u = weakref.WeakKeyDictionary() |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
658 u[a] = b |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
659 u[c] = d |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
660 # Keys aren't copied, values are |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
661 v = copy.deepcopy(u) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
662 self.assertNotEqual(v, u) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
663 self.assertEqual(len(v), 2) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
664 self.assertFalse(v[a] is b) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
665 self.assertFalse(v[c] is d) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
666 self.assertEqual(v[a].i, b.i) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
667 self.assertEqual(v[c].i, d.i) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
668 del c |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
669 self.assertEqual(len(v), 1) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
670 |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
671 def test_deepcopy_weakvaluedict(self): |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
672 class C(object): |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
673 def __init__(self, i): |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
674 self.i = i |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
675 a, b, c, d = [C(i) for i in xrange(4)] |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
676 u = weakref.WeakValueDictionary() |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
677 u[a] = b |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
678 u[c] = d |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
679 # Keys are copied, values aren't |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
680 v = copy.deepcopy(u) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
681 self.assertNotEqual(v, u) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
682 self.assertEqual(len(v), 2) |
|
58669
ff040880d1be
#7092: Silence more py3k warnings. Patch by Florent Xicluna.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
58460
diff
changeset
|
683 (x, y), (z, t) = sorted(v.items(), key=lambda pair: pair[0].i) |
|
54099
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
684 self.assertFalse(x is a) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
685 self.assertEqual(x.i, a.i) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
686 self.assertTrue(y is b) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
687 self.assertFalse(z is c) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
688 self.assertEqual(z.i, c.i) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
689 self.assertTrue(t is d) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
690 del x, y, z, t |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
691 del d |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
692 self.assertEqual(len(v), 1) |
|
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
693 |
|
57495
83c702c17e02
Issue #1515: Enable use of deepcopy() with instance methods. Patch by Robert Collins.
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
694 def test_deepcopy_bound_method(self): |
|
83c702c17e02
Issue #1515: Enable use of deepcopy() with instance methods. Patch by Robert Collins.
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
695 class Foo(object): |
|
83c702c17e02
Issue #1515: Enable use of deepcopy() with instance methods. Patch by Robert Collins.
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
696 def m(self): |
|
83c702c17e02
Issue #1515: Enable use of deepcopy() with instance methods. Patch by Robert Collins.
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
697 pass |
|
83c702c17e02
Issue #1515: Enable use of deepcopy() with instance methods. Patch by Robert Collins.
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
698 f = Foo() |
|
83c702c17e02
Issue #1515: Enable use of deepcopy() with instance methods. Patch by Robert Collins.
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
699 f.b = f.m |
|
83c702c17e02
Issue #1515: Enable use of deepcopy() with instance methods. Patch by Robert Collins.
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
700 g = copy.deepcopy(f) |
|
83c702c17e02
Issue #1515: Enable use of deepcopy() with instance methods. Patch by Robert Collins.
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
701 self.assertEqual(g.m, g.b) |
|
83c702c17e02
Issue #1515: Enable use of deepcopy() with instance methods. Patch by Robert Collins.
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
702 self.assertTrue(g.b.im_self is g) |
|
83c702c17e02
Issue #1515: Enable use of deepcopy() with instance methods. Patch by Robert Collins.
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
703 g.b() |
|
83c702c17e02
Issue #1515: Enable use of deepcopy() with instance methods. Patch by Robert Collins.
Antoine Pitrou <solipsis@pitrou.net>
parents:
55002
diff
changeset
|
704 |
|
54099
c40f187af6dd
Issue #2116: Weak references and weak dictionaries now support copy()ing and deepcopy()ing.
Antoine Pitrou <solipsis@pitrou.net>
parents:
48213
diff
changeset
|
705 |
| 36656 | 706 def global_foo(x, y): return x+y |
| 707 | |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
708 def test_main(): |
|
28917
1741af48fd9f
Combine the functionality of test_support.run_unittest()
Walter Dörwald <walter@livinglogic.de>
parents:
27959
diff
changeset
|
709 test_support.run_unittest(TestCopy) |
|
27725
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
710 |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
711 if __name__ == "__main__": |
|
f2f06b6ecdd7
A test suite for the copy module. This should provide full code
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
712 test_main() |
