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