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