~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to lib-python/modified-2.4.1/test/test_copy.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

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