Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 1 | """Unit tests for the copy module.""" |
| 2 | |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 3 | import copy |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 4 | import copyreg |
Antoine Pitrou | 6e61006 | 2009-05-15 17:04:50 +0000 | [diff] [blame] | 5 | import weakref |
Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 6 | from operator import le, lt, ge, gt, eq, ne |
Antoine Pitrou | 6e61006 | 2009-05-15 17:04:50 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 8 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 9 | from test import support |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 10 | |
Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 11 | order_comparisons = le, lt, ge, gt |
| 12 | equality_comparisons = eq, ne |
| 13 | comparisons = order_comparisons + equality_comparisons |
| 14 | |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 15 | class TestCopy(unittest.TestCase): |
| 16 | |
| 17 | # Attempt full line coverage of copy.py from top to bottom |
| 18 | |
| 19 | def test_exceptions(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 20 | self.assertTrue(copy.Error is copy.error) |
| 21 | self.assertTrue(issubclass(copy.Error, Exception)) |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 22 | |
| 23 | # The copy() method |
| 24 | |
| 25 | def test_copy_basic(self): |
| 26 | x = 42 |
| 27 | y = copy.copy(x) |
| 28 | self.assertEqual(x, y) |
| 29 | |
| 30 | def test_copy_copy(self): |
| 31 | class C(object): |
| 32 | def __init__(self, foo): |
| 33 | self.foo = foo |
| 34 | def __copy__(self): |
| 35 | return C(self.foo) |
| 36 | x = C(42) |
| 37 | y = copy.copy(x) |
| 38 | self.assertEqual(y.__class__, x.__class__) |
| 39 | self.assertEqual(y.foo, x.foo) |
| 40 | |
Guido van Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 41 | def test_copy_registry(self): |
| 42 | class C(object): |
| 43 | def __new__(cls, foo): |
| 44 | obj = object.__new__(cls) |
| 45 | obj.foo = foo |
| 46 | return obj |
| 47 | def pickle_C(obj): |
| 48 | return (C, (obj.foo,)) |
| 49 | x = C(42) |
| 50 | self.assertRaises(TypeError, copy.copy, x) |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 51 | copyreg.pickle(C, pickle_C, C) |
Guido van Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 52 | y = copy.copy(x) |
| 53 | |
Guido van Rossum | e690883 | 2003-02-19 01:19:28 +0000 | [diff] [blame] | 54 | def test_copy_reduce_ex(self): |
| 55 | class C(object): |
| 56 | def __reduce_ex__(self, proto): |
| 57 | return "" |
| 58 | def __reduce__(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 59 | raise support.TestFailed("shouldn't call this") |
Guido van Rossum | e690883 | 2003-02-19 01:19:28 +0000 | [diff] [blame] | 60 | x = C() |
| 61 | y = copy.copy(x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 62 | self.assertTrue(y is x) |
Guido van Rossum | e690883 | 2003-02-19 01:19:28 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 64 | def test_copy_reduce(self): |
| 65 | class C(object): |
| 66 | def __reduce__(self): |
| 67 | return "" |
| 68 | x = C() |
| 69 | y = copy.copy(x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 70 | self.assertTrue(y is x) |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 71 | |
| 72 | def test_copy_cant(self): |
Guido van Rossum | e690883 | 2003-02-19 01:19:28 +0000 | [diff] [blame] | 73 | class C(object): |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 74 | def __getattribute__(self, name): |
Guido van Rossum | e690883 | 2003-02-19 01:19:28 +0000 | [diff] [blame] | 75 | if name.startswith("__reduce"): |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 76 | raise AttributeError(name) |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 77 | return object.__getattribute__(self, name) |
| 78 | x = C() |
| 79 | self.assertRaises(copy.Error, copy.copy, x) |
| 80 | |
| 81 | # Type-specific _copy_xxx() methods |
| 82 | |
| 83 | def test_copy_atomic(self): |
| 84 | class Classic: |
| 85 | pass |
| 86 | class NewStyle(object): |
| 87 | pass |
| 88 | def f(): |
| 89 | pass |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 90 | tests = [None, 42, 2**100, 3.14, True, False, 1j, |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 91 | "hello", "hello\u1234", f.__code__, |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 92 | NewStyle, range(10), Classic, max] |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 93 | for x in tests: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 94 | self.assertTrue(copy.copy(x) is x, repr(x)) |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 95 | |
| 96 | def test_copy_list(self): |
| 97 | x = [1, 2, 3] |
| 98 | self.assertEqual(copy.copy(x), x) |
| 99 | |
| 100 | def test_copy_tuple(self): |
| 101 | x = (1, 2, 3) |
| 102 | self.assertEqual(copy.copy(x), x) |
| 103 | |
| 104 | def test_copy_dict(self): |
| 105 | x = {"foo": 1, "bar": 2} |
| 106 | self.assertEqual(copy.copy(x), x) |
| 107 | |
| 108 | def test_copy_inst_vanilla(self): |
| 109 | class C: |
| 110 | def __init__(self, foo): |
| 111 | self.foo = foo |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 112 | def __eq__(self, other): |
| 113 | return self.foo == other.foo |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 114 | x = C(42) |
| 115 | self.assertEqual(copy.copy(x), x) |
| 116 | |
| 117 | def test_copy_inst_copy(self): |
| 118 | class C: |
| 119 | def __init__(self, foo): |
| 120 | self.foo = foo |
| 121 | def __copy__(self): |
| 122 | return C(self.foo) |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 123 | def __eq__(self, other): |
| 124 | return self.foo == other.foo |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 125 | x = C(42) |
| 126 | self.assertEqual(copy.copy(x), x) |
| 127 | |
| 128 | def test_copy_inst_getinitargs(self): |
| 129 | class C: |
| 130 | def __init__(self, foo): |
| 131 | self.foo = foo |
| 132 | def __getinitargs__(self): |
| 133 | return (self.foo,) |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 134 | def __eq__(self, other): |
| 135 | return self.foo == other.foo |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 136 | x = C(42) |
| 137 | self.assertEqual(copy.copy(x), x) |
| 138 | |
| 139 | def test_copy_inst_getstate(self): |
| 140 | class C: |
| 141 | def __init__(self, foo): |
| 142 | self.foo = foo |
| 143 | def __getstate__(self): |
| 144 | return {"foo": self.foo} |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 145 | def __eq__(self, other): |
| 146 | return self.foo == other.foo |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 147 | x = C(42) |
| 148 | self.assertEqual(copy.copy(x), x) |
| 149 | |
| 150 | def test_copy_inst_setstate(self): |
| 151 | class C: |
| 152 | def __init__(self, foo): |
| 153 | self.foo = foo |
| 154 | def __setstate__(self, state): |
| 155 | self.foo = state["foo"] |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 156 | def __eq__(self, other): |
| 157 | return self.foo == other.foo |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 158 | x = C(42) |
| 159 | self.assertEqual(copy.copy(x), x) |
| 160 | |
| 161 | def test_copy_inst_getstate_setstate(self): |
| 162 | class C: |
| 163 | def __init__(self, foo): |
| 164 | self.foo = foo |
| 165 | def __getstate__(self): |
| 166 | return self.foo |
| 167 | def __setstate__(self, state): |
| 168 | self.foo = state |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 169 | def __eq__(self, other): |
| 170 | return self.foo == other.foo |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 171 | x = C(42) |
| 172 | self.assertEqual(copy.copy(x), x) |
| 173 | |
| 174 | # The deepcopy() method |
| 175 | |
| 176 | def test_deepcopy_basic(self): |
| 177 | x = 42 |
| 178 | y = copy.deepcopy(x) |
| 179 | self.assertEqual(y, x) |
| 180 | |
| 181 | def test_deepcopy_memo(self): |
Guido van Rossum | 99d2c25 | 2003-06-13 19:28:47 +0000 | [diff] [blame] | 182 | # Tests of reflexive objects are under type-specific sections below. |
| 183 | # This tests only repetitions of objects. |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 184 | x = [] |
Guido van Rossum | 99d2c25 | 2003-06-13 19:28:47 +0000 | [diff] [blame] | 185 | x = [x, x] |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 186 | y = copy.deepcopy(x) |
| 187 | self.assertEqual(y, x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 188 | self.assertTrue(y is not x) |
| 189 | self.assertTrue(y[0] is not x[0]) |
| 190 | self.assertTrue(y[0] is y[1]) |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 191 | |
| 192 | def test_deepcopy_issubclass(self): |
| 193 | # XXX Note: there's no way to test the TypeError coming out of |
| 194 | # issubclass() -- this can only happen when an extension |
| 195 | # module defines a "type" that doesn't formally inherit from |
| 196 | # type. |
| 197 | class Meta(type): |
| 198 | pass |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 199 | class C(metaclass=Meta): |
| 200 | pass |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 201 | self.assertEqual(copy.deepcopy(C), C) |
| 202 | |
| 203 | def test_deepcopy_deepcopy(self): |
| 204 | class C(object): |
| 205 | def __init__(self, foo): |
| 206 | self.foo = foo |
| 207 | def __deepcopy__(self, memo=None): |
| 208 | return C(self.foo) |
| 209 | x = C(42) |
| 210 | y = copy.deepcopy(x) |
| 211 | self.assertEqual(y.__class__, x.__class__) |
| 212 | self.assertEqual(y.foo, x.foo) |
| 213 | |
Guido van Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 214 | def test_deepcopy_registry(self): |
| 215 | class C(object): |
| 216 | def __new__(cls, foo): |
| 217 | obj = object.__new__(cls) |
| 218 | obj.foo = foo |
| 219 | return obj |
| 220 | def pickle_C(obj): |
| 221 | return (C, (obj.foo,)) |
| 222 | x = C(42) |
| 223 | self.assertRaises(TypeError, copy.deepcopy, x) |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 224 | copyreg.pickle(C, pickle_C, C) |
Guido van Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 225 | y = copy.deepcopy(x) |
| 226 | |
Guido van Rossum | e690883 | 2003-02-19 01:19:28 +0000 | [diff] [blame] | 227 | def test_deepcopy_reduce_ex(self): |
| 228 | class C(object): |
| 229 | def __reduce_ex__(self, proto): |
| 230 | return "" |
| 231 | def __reduce__(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 232 | raise support.TestFailed("shouldn't call this") |
Guido van Rossum | e690883 | 2003-02-19 01:19:28 +0000 | [diff] [blame] | 233 | x = C() |
| 234 | y = copy.deepcopy(x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 235 | self.assertTrue(y is x) |
Guido van Rossum | e690883 | 2003-02-19 01:19:28 +0000 | [diff] [blame] | 236 | |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 237 | def test_deepcopy_reduce(self): |
| 238 | class C(object): |
| 239 | def __reduce__(self): |
| 240 | return "" |
| 241 | x = C() |
| 242 | y = copy.deepcopy(x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 243 | self.assertTrue(y is x) |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 244 | |
| 245 | def test_deepcopy_cant(self): |
Guido van Rossum | e690883 | 2003-02-19 01:19:28 +0000 | [diff] [blame] | 246 | class C(object): |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 247 | def __getattribute__(self, name): |
Guido van Rossum | e690883 | 2003-02-19 01:19:28 +0000 | [diff] [blame] | 248 | if name.startswith("__reduce"): |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 249 | raise AttributeError(name) |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 250 | return object.__getattribute__(self, name) |
| 251 | x = C() |
| 252 | self.assertRaises(copy.Error, copy.deepcopy, x) |
| 253 | |
| 254 | # Type-specific _deepcopy_xxx() methods |
| 255 | |
| 256 | def test_deepcopy_atomic(self): |
| 257 | class Classic: |
| 258 | pass |
| 259 | class NewStyle(object): |
| 260 | pass |
| 261 | def f(): |
| 262 | pass |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 263 | tests = [None, 42, 2**100, 3.14, True, False, 1j, |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 264 | "hello", "hello\u1234", f.__code__, |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 265 | NewStyle, range(10), Classic, max] |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 266 | for x in tests: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 267 | self.assertTrue(copy.deepcopy(x) is x, repr(x)) |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 268 | |
| 269 | def test_deepcopy_list(self): |
| 270 | x = [[1, 2], 3] |
| 271 | y = copy.deepcopy(x) |
| 272 | self.assertEqual(y, x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 273 | self.assertTrue(x is not y) |
| 274 | self.assertTrue(x[0] is not y[0]) |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 275 | |
Guido van Rossum | 99d2c25 | 2003-06-13 19:28:47 +0000 | [diff] [blame] | 276 | def test_deepcopy_reflexive_list(self): |
| 277 | x = [] |
| 278 | x.append(x) |
| 279 | y = copy.deepcopy(x) |
Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 280 | for op in comparisons: |
| 281 | self.assertRaises(RuntimeError, op, y, x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 282 | self.assertTrue(y is not x) |
| 283 | self.assertTrue(y[0] is y) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 284 | self.assertEqual(len(y), 1) |
Guido van Rossum | 99d2c25 | 2003-06-13 19:28:47 +0000 | [diff] [blame] | 285 | |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 286 | def test_deepcopy_tuple(self): |
| 287 | x = ([1, 2], 3) |
| 288 | y = copy.deepcopy(x) |
| 289 | self.assertEqual(y, x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 290 | self.assertTrue(x is not y) |
| 291 | self.assertTrue(x[0] is not y[0]) |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 292 | |
Guido van Rossum | 99d2c25 | 2003-06-13 19:28:47 +0000 | [diff] [blame] | 293 | def test_deepcopy_reflexive_tuple(self): |
| 294 | x = ([],) |
| 295 | x[0].append(x) |
| 296 | y = copy.deepcopy(x) |
Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 297 | for op in comparisons: |
| 298 | self.assertRaises(RuntimeError, op, y, x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 299 | self.assertTrue(y is not x) |
| 300 | self.assertTrue(y[0] is not x[0]) |
| 301 | self.assertTrue(y[0][0] is y) |
Guido van Rossum | 99d2c25 | 2003-06-13 19:28:47 +0000 | [diff] [blame] | 302 | |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 303 | def test_deepcopy_dict(self): |
| 304 | x = {"foo": [1, 2], "bar": 3} |
| 305 | y = copy.deepcopy(x) |
| 306 | self.assertEqual(y, x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 307 | self.assertTrue(x is not y) |
| 308 | self.assertTrue(x["foo"] is not y["foo"]) |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 309 | |
Guido van Rossum | 99d2c25 | 2003-06-13 19:28:47 +0000 | [diff] [blame] | 310 | def test_deepcopy_reflexive_dict(self): |
| 311 | x = {} |
| 312 | x['foo'] = x |
| 313 | y = copy.deepcopy(x) |
Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 314 | for op in order_comparisons: |
| 315 | self.assertRaises(TypeError, op, y, x) |
| 316 | for op in equality_comparisons: |
| 317 | self.assertRaises(RuntimeError, op, y, x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 318 | self.assertTrue(y is not x) |
| 319 | self.assertTrue(y['foo'] is y) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 320 | self.assertEqual(len(y), 1) |
Guido van Rossum | 99d2c25 | 2003-06-13 19:28:47 +0000 | [diff] [blame] | 321 | |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 322 | def test_deepcopy_keepalive(self): |
| 323 | memo = {} |
Benjamin Peterson | e90ec36 | 2011-06-27 16:22:46 -0500 | [diff] [blame] | 324 | x = [] |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 325 | y = copy.deepcopy(x, memo) |
Benjamin Peterson | e90ec36 | 2011-06-27 16:22:46 -0500 | [diff] [blame] | 326 | self.assertIs(memo[id(memo)][0], x) |
| 327 | |
| 328 | def test_deepcopy_dont_memo_immutable(self): |
| 329 | memo = {} |
| 330 | x = [1, 2, 3, 4] |
| 331 | y = copy.deepcopy(x, memo) |
| 332 | self.assertEqual(y, x) |
| 333 | # There's the entry for the new list, and the keep alive. |
| 334 | self.assertEqual(len(memo), 2) |
| 335 | |
| 336 | memo = {} |
| 337 | x = [(1, 2)] |
| 338 | y = copy.deepcopy(x, memo) |
| 339 | self.assertEqual(y, x) |
| 340 | # Tuples with immutable contents are immutable for deepcopy. |
| 341 | self.assertEqual(len(memo), 2) |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 342 | |
| 343 | def test_deepcopy_inst_vanilla(self): |
| 344 | class C: |
| 345 | def __init__(self, foo): |
| 346 | self.foo = foo |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 347 | def __eq__(self, other): |
| 348 | return self.foo == other.foo |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 349 | x = C([42]) |
| 350 | y = copy.deepcopy(x) |
| 351 | self.assertEqual(y, x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 352 | self.assertTrue(y.foo is not x.foo) |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 353 | |
| 354 | def test_deepcopy_inst_deepcopy(self): |
| 355 | class C: |
| 356 | def __init__(self, foo): |
| 357 | self.foo = foo |
| 358 | def __deepcopy__(self, memo): |
| 359 | return C(copy.deepcopy(self.foo, memo)) |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 360 | def __eq__(self, other): |
| 361 | return self.foo == other.foo |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 362 | x = C([42]) |
| 363 | y = copy.deepcopy(x) |
| 364 | self.assertEqual(y, x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 365 | self.assertTrue(y is not x) |
| 366 | self.assertTrue(y.foo is not x.foo) |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 367 | |
| 368 | def test_deepcopy_inst_getinitargs(self): |
| 369 | class C: |
| 370 | def __init__(self, foo): |
| 371 | self.foo = foo |
| 372 | def __getinitargs__(self): |
| 373 | return (self.foo,) |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 374 | def __eq__(self, other): |
| 375 | return self.foo == other.foo |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 376 | x = C([42]) |
| 377 | y = copy.deepcopy(x) |
| 378 | self.assertEqual(y, x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 379 | self.assertTrue(y is not x) |
| 380 | self.assertTrue(y.foo is not x.foo) |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 381 | |
| 382 | def test_deepcopy_inst_getstate(self): |
| 383 | class C: |
| 384 | def __init__(self, foo): |
| 385 | self.foo = foo |
| 386 | def __getstate__(self): |
| 387 | return {"foo": self.foo} |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 388 | def __eq__(self, other): |
| 389 | return self.foo == other.foo |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 390 | x = C([42]) |
| 391 | y = copy.deepcopy(x) |
| 392 | self.assertEqual(y, x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 393 | self.assertTrue(y is not x) |
| 394 | self.assertTrue(y.foo is not x.foo) |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 395 | |
| 396 | def test_deepcopy_inst_setstate(self): |
| 397 | class C: |
| 398 | def __init__(self, foo): |
| 399 | self.foo = foo |
| 400 | def __setstate__(self, state): |
| 401 | self.foo = state["foo"] |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 402 | def __eq__(self, other): |
| 403 | return self.foo == other.foo |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 404 | x = C([42]) |
| 405 | y = copy.deepcopy(x) |
| 406 | self.assertEqual(y, x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 407 | self.assertTrue(y is not x) |
| 408 | self.assertTrue(y.foo is not x.foo) |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 409 | |
| 410 | def test_deepcopy_inst_getstate_setstate(self): |
| 411 | class C: |
| 412 | def __init__(self, foo): |
| 413 | self.foo = foo |
| 414 | def __getstate__(self): |
| 415 | return self.foo |
| 416 | def __setstate__(self, state): |
| 417 | self.foo = state |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 418 | def __eq__(self, other): |
| 419 | return self.foo == other.foo |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 420 | x = C([42]) |
| 421 | y = copy.deepcopy(x) |
| 422 | self.assertEqual(y, x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 423 | self.assertTrue(y is not x) |
| 424 | self.assertTrue(y.foo is not x.foo) |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 425 | |
Guido van Rossum | 99d2c25 | 2003-06-13 19:28:47 +0000 | [diff] [blame] | 426 | def test_deepcopy_reflexive_inst(self): |
| 427 | class C: |
| 428 | pass |
| 429 | x = C() |
| 430 | x.foo = x |
| 431 | y = copy.deepcopy(x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 432 | self.assertTrue(y is not x) |
| 433 | self.assertTrue(y.foo is y) |
Guido van Rossum | 99d2c25 | 2003-06-13 19:28:47 +0000 | [diff] [blame] | 434 | |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 435 | # _reconstruct() |
| 436 | |
| 437 | def test_reconstruct_string(self): |
| 438 | class C(object): |
| 439 | def __reduce__(self): |
| 440 | return "" |
| 441 | x = C() |
| 442 | y = copy.copy(x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 443 | self.assertTrue(y is x) |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 444 | y = copy.deepcopy(x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 445 | self.assertTrue(y is x) |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 446 | |
| 447 | def test_reconstruct_nostate(self): |
| 448 | class C(object): |
| 449 | def __reduce__(self): |
| 450 | return (C, ()) |
| 451 | x = C() |
| 452 | x.foo = 42 |
| 453 | y = copy.copy(x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 454 | self.assertTrue(y.__class__ is x.__class__) |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 455 | y = copy.deepcopy(x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 456 | self.assertTrue(y.__class__ is x.__class__) |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 457 | |
| 458 | def test_reconstruct_state(self): |
| 459 | class C(object): |
| 460 | def __reduce__(self): |
| 461 | return (C, (), self.__dict__) |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 462 | def __eq__(self, other): |
| 463 | return self.__dict__ == other.__dict__ |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 464 | x = C() |
| 465 | x.foo = [42] |
| 466 | y = copy.copy(x) |
| 467 | self.assertEqual(y, x) |
| 468 | y = copy.deepcopy(x) |
| 469 | self.assertEqual(y, x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 470 | self.assertTrue(y.foo is not x.foo) |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 471 | |
| 472 | def test_reconstruct_state_setstate(self): |
| 473 | class C(object): |
| 474 | def __reduce__(self): |
| 475 | return (C, (), self.__dict__) |
| 476 | def __setstate__(self, state): |
| 477 | self.__dict__.update(state) |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 478 | def __eq__(self, other): |
| 479 | return self.__dict__ == other.__dict__ |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 480 | x = C() |
| 481 | x.foo = [42] |
| 482 | y = copy.copy(x) |
| 483 | self.assertEqual(y, x) |
| 484 | y = copy.deepcopy(x) |
| 485 | self.assertEqual(y, x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 486 | self.assertTrue(y.foo is not x.foo) |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 487 | |
Guido van Rossum | 99d2c25 | 2003-06-13 19:28:47 +0000 | [diff] [blame] | 488 | def test_reconstruct_reflexive(self): |
| 489 | class C(object): |
| 490 | pass |
| 491 | x = C() |
| 492 | x.foo = x |
| 493 | y = copy.deepcopy(x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 494 | self.assertTrue(y is not x) |
| 495 | self.assertTrue(y.foo is y) |
Guido van Rossum | 99d2c25 | 2003-06-13 19:28:47 +0000 | [diff] [blame] | 496 | |
Guido van Rossum | 90e05b0 | 2003-02-06 18:18:23 +0000 | [diff] [blame] | 497 | # Additions for Python 2.3 and pickle protocol 2 |
| 498 | |
| 499 | def test_reduce_4tuple(self): |
| 500 | class C(list): |
| 501 | def __reduce__(self): |
| 502 | return (C, (), self.__dict__, iter(self)) |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 503 | def __eq__(self, other): |
| 504 | return (list(self) == list(other) and |
| 505 | self.__dict__ == other.__dict__) |
Guido van Rossum | 90e05b0 | 2003-02-06 18:18:23 +0000 | [diff] [blame] | 506 | x = C([[1, 2], 3]) |
| 507 | y = copy.copy(x) |
| 508 | self.assertEqual(x, y) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 509 | self.assertTrue(x is not y) |
| 510 | self.assertTrue(x[0] is y[0]) |
Guido van Rossum | 90e05b0 | 2003-02-06 18:18:23 +0000 | [diff] [blame] | 511 | y = copy.deepcopy(x) |
| 512 | self.assertEqual(x, y) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 513 | self.assertTrue(x is not y) |
| 514 | self.assertTrue(x[0] is not y[0]) |
Guido van Rossum | 90e05b0 | 2003-02-06 18:18:23 +0000 | [diff] [blame] | 515 | |
| 516 | def test_reduce_5tuple(self): |
| 517 | class C(dict): |
| 518 | def __reduce__(self): |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 519 | return (C, (), self.__dict__, None, self.items()) |
Guido van Rossum | 47b9ff6 | 2006-08-24 00:41:19 +0000 | [diff] [blame] | 520 | def __eq__(self, other): |
| 521 | return (dict(self) == dict(other) and |
| 522 | self.__dict__ == other.__dict__) |
Guido van Rossum | 90e05b0 | 2003-02-06 18:18:23 +0000 | [diff] [blame] | 523 | x = C([("foo", [1, 2]), ("bar", 3)]) |
| 524 | y = copy.copy(x) |
| 525 | self.assertEqual(x, y) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 526 | self.assertTrue(x is not y) |
| 527 | self.assertTrue(x["foo"] is y["foo"]) |
Guido van Rossum | 90e05b0 | 2003-02-06 18:18:23 +0000 | [diff] [blame] | 528 | y = copy.deepcopy(x) |
| 529 | self.assertEqual(x, y) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 530 | self.assertTrue(x is not y) |
| 531 | self.assertTrue(x["foo"] is not y["foo"]) |
Guido van Rossum | 90e05b0 | 2003-02-06 18:18:23 +0000 | [diff] [blame] | 532 | |
Guido van Rossum | c755758 | 2003-02-06 19:53:22 +0000 | [diff] [blame] | 533 | def test_copy_slots(self): |
| 534 | class C(object): |
| 535 | __slots__ = ["foo"] |
| 536 | x = C() |
| 537 | x.foo = [42] |
| 538 | y = copy.copy(x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 539 | self.assertTrue(x.foo is y.foo) |
Guido van Rossum | c755758 | 2003-02-06 19:53:22 +0000 | [diff] [blame] | 540 | |
| 541 | def test_deepcopy_slots(self): |
| 542 | class C(object): |
| 543 | __slots__ = ["foo"] |
| 544 | x = C() |
| 545 | x.foo = [42] |
| 546 | y = copy.deepcopy(x) |
| 547 | self.assertEqual(x.foo, y.foo) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 548 | self.assertTrue(x.foo is not y.foo) |
Guido van Rossum | c755758 | 2003-02-06 19:53:22 +0000 | [diff] [blame] | 549 | |
Antoine Pitrou | 3941a8f | 2010-09-04 17:40:21 +0000 | [diff] [blame] | 550 | def test_deepcopy_dict_subclass(self): |
| 551 | class C(dict): |
| 552 | def __init__(self, d=None): |
| 553 | if not d: |
| 554 | d = {} |
| 555 | self._keys = list(d.keys()) |
| 556 | super().__init__(d) |
| 557 | def __setitem__(self, key, item): |
| 558 | super().__setitem__(key, item) |
| 559 | if key not in self._keys: |
| 560 | self._keys.append(key) |
| 561 | x = C(d={'foo':0}) |
| 562 | y = copy.deepcopy(x) |
| 563 | self.assertEqual(x, y) |
| 564 | self.assertEqual(x._keys, y._keys) |
| 565 | self.assertTrue(x is not y) |
| 566 | x['bar'] = 1 |
| 567 | self.assertNotEqual(x, y) |
| 568 | self.assertNotEqual(x._keys, y._keys) |
| 569 | |
Guido van Rossum | c755758 | 2003-02-06 19:53:22 +0000 | [diff] [blame] | 570 | def test_copy_list_subclass(self): |
| 571 | class C(list): |
| 572 | pass |
| 573 | x = C([[1, 2], 3]) |
| 574 | x.foo = [4, 5] |
| 575 | y = copy.copy(x) |
| 576 | self.assertEqual(list(x), list(y)) |
| 577 | self.assertEqual(x.foo, y.foo) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 578 | self.assertTrue(x[0] is y[0]) |
| 579 | self.assertTrue(x.foo is y.foo) |
Guido van Rossum | c755758 | 2003-02-06 19:53:22 +0000 | [diff] [blame] | 580 | |
| 581 | def test_deepcopy_list_subclass(self): |
| 582 | class C(list): |
| 583 | pass |
| 584 | x = C([[1, 2], 3]) |
| 585 | x.foo = [4, 5] |
| 586 | y = copy.deepcopy(x) |
| 587 | self.assertEqual(list(x), list(y)) |
| 588 | self.assertEqual(x.foo, y.foo) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 589 | self.assertTrue(x[0] is not y[0]) |
| 590 | self.assertTrue(x.foo is not y.foo) |
Guido van Rossum | c755758 | 2003-02-06 19:53:22 +0000 | [diff] [blame] | 591 | |
Guido van Rossum | 85233bf | 2003-02-06 21:25:12 +0000 | [diff] [blame] | 592 | def test_copy_tuple_subclass(self): |
| 593 | class C(tuple): |
| 594 | pass |
| 595 | x = C([1, 2, 3]) |
| 596 | self.assertEqual(tuple(x), (1, 2, 3)) |
| 597 | y = copy.copy(x) |
| 598 | self.assertEqual(tuple(y), (1, 2, 3)) |
| 599 | |
| 600 | def test_deepcopy_tuple_subclass(self): |
| 601 | class C(tuple): |
| 602 | pass |
| 603 | x = C([[1, 2], 3]) |
| 604 | self.assertEqual(tuple(x), ([1, 2], 3)) |
| 605 | y = copy.deepcopy(x) |
| 606 | self.assertEqual(tuple(y), ([1, 2], 3)) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 607 | self.assertTrue(x is not y) |
| 608 | self.assertTrue(x[0] is not y[0]) |
Guido van Rossum | 85233bf | 2003-02-06 21:25:12 +0000 | [diff] [blame] | 609 | |
Neal Norwitz | e2fdc61 | 2003-06-08 13:19:58 +0000 | [diff] [blame] | 610 | def test_getstate_exc(self): |
| 611 | class EvilState(object): |
| 612 | def __getstate__(self): |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 613 | raise ValueError("ain't got no stickin' state") |
Neal Norwitz | e2fdc61 | 2003-06-08 13:19:58 +0000 | [diff] [blame] | 614 | self.assertRaises(ValueError, copy.copy, EvilState()) |
| 615 | |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 616 | def test_copy_function(self): |
| 617 | self.assertEqual(copy.copy(global_foo), global_foo) |
| 618 | def foo(x, y): return x+y |
| 619 | self.assertEqual(copy.copy(foo), foo) |
| 620 | bar = lambda: None |
| 621 | self.assertEqual(copy.copy(bar), bar) |
| 622 | |
| 623 | def test_deepcopy_function(self): |
| 624 | self.assertEqual(copy.deepcopy(global_foo), global_foo) |
| 625 | def foo(x, y): return x+y |
| 626 | self.assertEqual(copy.deepcopy(foo), foo) |
| 627 | bar = lambda: None |
| 628 | self.assertEqual(copy.deepcopy(bar), bar) |
| 629 | |
Antoine Pitrou | 6e61006 | 2009-05-15 17:04:50 +0000 | [diff] [blame] | 630 | def _check_weakref(self, _copy): |
| 631 | class C(object): |
| 632 | pass |
| 633 | obj = C() |
| 634 | x = weakref.ref(obj) |
| 635 | y = _copy(x) |
| 636 | self.assertTrue(y is x) |
| 637 | del obj |
| 638 | y = _copy(x) |
| 639 | self.assertTrue(y is x) |
| 640 | |
| 641 | def test_copy_weakref(self): |
| 642 | self._check_weakref(copy.copy) |
| 643 | |
| 644 | def test_deepcopy_weakref(self): |
| 645 | self._check_weakref(copy.deepcopy) |
| 646 | |
| 647 | def _check_copy_weakdict(self, _dicttype): |
| 648 | class C(object): |
| 649 | pass |
| 650 | a, b, c, d = [C() for i in range(4)] |
| 651 | u = _dicttype() |
| 652 | u[a] = b |
| 653 | u[c] = d |
| 654 | v = copy.copy(u) |
| 655 | self.assertFalse(v is u) |
| 656 | self.assertEqual(v, u) |
| 657 | self.assertEqual(v[a], b) |
| 658 | self.assertEqual(v[c], d) |
| 659 | self.assertEqual(len(v), 2) |
| 660 | del c, d |
| 661 | self.assertEqual(len(v), 1) |
| 662 | x, y = C(), C() |
| 663 | # The underlying containers are decoupled |
| 664 | v[x] = y |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 665 | self.assertNotIn(x, u) |
Antoine Pitrou | 6e61006 | 2009-05-15 17:04:50 +0000 | [diff] [blame] | 666 | |
| 667 | def test_copy_weakkeydict(self): |
| 668 | self._check_copy_weakdict(weakref.WeakKeyDictionary) |
| 669 | |
| 670 | def test_copy_weakvaluedict(self): |
| 671 | self._check_copy_weakdict(weakref.WeakValueDictionary) |
| 672 | |
| 673 | def test_deepcopy_weakkeydict(self): |
| 674 | class C(object): |
| 675 | def __init__(self, i): |
| 676 | self.i = i |
| 677 | a, b, c, d = [C(i) for i in range(4)] |
| 678 | u = weakref.WeakKeyDictionary() |
| 679 | u[a] = b |
| 680 | u[c] = d |
| 681 | # Keys aren't copied, values are |
| 682 | v = copy.deepcopy(u) |
| 683 | self.assertNotEqual(v, u) |
| 684 | self.assertEqual(len(v), 2) |
| 685 | self.assertFalse(v[a] is b) |
| 686 | self.assertFalse(v[c] is d) |
| 687 | self.assertEqual(v[a].i, b.i) |
| 688 | self.assertEqual(v[c].i, d.i) |
| 689 | del c |
| 690 | self.assertEqual(len(v), 1) |
| 691 | |
| 692 | def test_deepcopy_weakvaluedict(self): |
| 693 | class C(object): |
| 694 | def __init__(self, i): |
| 695 | self.i = i |
| 696 | a, b, c, d = [C(i) for i in range(4)] |
| 697 | u = weakref.WeakValueDictionary() |
| 698 | u[a] = b |
| 699 | u[c] = d |
| 700 | # Keys are copied, values aren't |
| 701 | v = copy.deepcopy(u) |
| 702 | self.assertNotEqual(v, u) |
| 703 | self.assertEqual(len(v), 2) |
| 704 | (x, y), (z, t) = sorted(v.items(), key=lambda pair: pair[0].i) |
| 705 | self.assertFalse(x is a) |
| 706 | self.assertEqual(x.i, a.i) |
| 707 | self.assertTrue(y is b) |
| 708 | self.assertFalse(z is c) |
| 709 | self.assertEqual(z.i, c.i) |
| 710 | self.assertTrue(t is d) |
| 711 | del x, y, z, t |
| 712 | del d |
| 713 | self.assertEqual(len(v), 1) |
| 714 | |
Antoine Pitrou | 1fc0d2b | 2009-11-28 15:58:27 +0000 | [diff] [blame] | 715 | def test_deepcopy_bound_method(self): |
| 716 | class Foo(object): |
| 717 | def m(self): |
| 718 | pass |
| 719 | f = Foo() |
| 720 | f.b = f.m |
| 721 | g = copy.deepcopy(f) |
| 722 | self.assertEqual(g.m, g.b) |
| 723 | self.assertTrue(g.b.__self__ is g) |
| 724 | g.b() |
| 725 | |
Antoine Pitrou | 6e61006 | 2009-05-15 17:04:50 +0000 | [diff] [blame] | 726 | |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 727 | def global_foo(x, y): return x+y |
| 728 | |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 729 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 730 | support.run_unittest(TestCopy) |
Guido van Rossum | 581cb93 | 2003-02-06 17:52:15 +0000 | [diff] [blame] | 731 | |
| 732 | if __name__ == "__main__": |
| 733 | test_main() |