ยปCore Development>Code coverage>Lib/test/test_copy.py

Python code coverage for Lib/test/test_copy.py

#countcontent
1n/a"""Unit tests for the copy module."""
2n/a
3n/aimport copy
4n/aimport copyreg
5n/aimport weakref
6n/aimport abc
7n/afrom operator import le, lt, ge, gt, eq, ne
8n/a
9n/aimport unittest
10n/a
11n/aorder_comparisons = le, lt, ge, gt
12n/aequality_comparisons = eq, ne
13n/acomparisons = order_comparisons + equality_comparisons
14n/a
15n/aclass TestCopy(unittest.TestCase):
16n/a
17n/a # Attempt full line coverage of copy.py from top to bottom
18n/a
19n/a def test_exceptions(self):
20n/a self.assertIs(copy.Error, copy.error)
21n/a self.assertTrue(issubclass(copy.Error, Exception))
22n/a
23n/a # The copy() method
24n/a
25n/a def test_copy_basic(self):
26n/a x = 42
27n/a y = copy.copy(x)
28n/a self.assertEqual(x, y)
29n/a
30n/a def test_copy_copy(self):
31n/a class C(object):
32n/a def __init__(self, foo):
33n/a self.foo = foo
34n/a def __copy__(self):
35n/a return C(self.foo)
36n/a x = C(42)
37n/a y = copy.copy(x)
38n/a self.assertEqual(y.__class__, x.__class__)
39n/a self.assertEqual(y.foo, x.foo)
40n/a
41n/a def test_copy_registry(self):
42n/a class C(object):
43n/a def __new__(cls, foo):
44n/a obj = object.__new__(cls)
45n/a obj.foo = foo
46n/a return obj
47n/a def pickle_C(obj):
48n/a return (C, (obj.foo,))
49n/a x = C(42)
50n/a self.assertRaises(TypeError, copy.copy, x)
51n/a copyreg.pickle(C, pickle_C, C)
52n/a y = copy.copy(x)
53n/a
54n/a def test_copy_reduce_ex(self):
55n/a class C(object):
56n/a def __reduce_ex__(self, proto):
57n/a c.append(1)
58n/a return ""
59n/a def __reduce__(self):
60n/a self.fail("shouldn't call this")
61n/a c = []
62n/a x = C()
63n/a y = copy.copy(x)
64n/a self.assertIs(y, x)
65n/a self.assertEqual(c, [1])
66n/a
67n/a def test_copy_reduce(self):
68n/a class C(object):
69n/a def __reduce__(self):
70n/a c.append(1)
71n/a return ""
72n/a c = []
73n/a x = C()
74n/a y = copy.copy(x)
75n/a self.assertIs(y, x)
76n/a self.assertEqual(c, [1])
77n/a
78n/a def test_copy_cant(self):
79n/a class C(object):
80n/a def __getattribute__(self, name):
81n/a if name.startswith("__reduce"):
82n/a raise AttributeError(name)
83n/a return object.__getattribute__(self, name)
84n/a x = C()
85n/a self.assertRaises(copy.Error, copy.copy, x)
86n/a
87n/a # Type-specific _copy_xxx() methods
88n/a
89n/a def test_copy_atomic(self):
90n/a class Classic:
91n/a pass
92n/a class NewStyle(object):
93n/a pass
94n/a def f():
95n/a pass
96n/a class WithMetaclass(metaclass=abc.ABCMeta):
97n/a pass
98n/a tests = [None, ..., NotImplemented,
99n/a 42, 2**100, 3.14, True, False, 1j,
100n/a "hello", "hello\u1234", f.__code__,
101n/a b"world", bytes(range(256)), range(10), slice(1, 10, 2),
102n/a NewStyle, Classic, max, WithMetaclass]
103n/a for x in tests:
104n/a self.assertIs(copy.copy(x), x)
105n/a
106n/a def test_copy_list(self):
107n/a x = [1, 2, 3]
108n/a y = copy.copy(x)
109n/a self.assertEqual(y, x)
110n/a self.assertIsNot(y, x)
111n/a x = []
112n/a y = copy.copy(x)
113n/a self.assertEqual(y, x)
114n/a self.assertIsNot(y, x)
115n/a
116n/a def test_copy_tuple(self):
117n/a x = (1, 2, 3)
118n/a self.assertIs(copy.copy(x), x)
119n/a x = ()
120n/a self.assertIs(copy.copy(x), x)
121n/a x = (1, 2, 3, [])
122n/a self.assertIs(copy.copy(x), x)
123n/a
124n/a def test_copy_dict(self):
125n/a x = {"foo": 1, "bar": 2}
126n/a y = copy.copy(x)
127n/a self.assertEqual(y, x)
128n/a self.assertIsNot(y, x)
129n/a x = {}
130n/a y = copy.copy(x)
131n/a self.assertEqual(y, x)
132n/a self.assertIsNot(y, x)
133n/a
134n/a def test_copy_set(self):
135n/a x = {1, 2, 3}
136n/a y = copy.copy(x)
137n/a self.assertEqual(y, x)
138n/a self.assertIsNot(y, x)
139n/a x = set()
140n/a y = copy.copy(x)
141n/a self.assertEqual(y, x)
142n/a self.assertIsNot(y, x)
143n/a
144n/a def test_copy_frozenset(self):
145n/a x = frozenset({1, 2, 3})
146n/a self.assertIs(copy.copy(x), x)
147n/a x = frozenset()
148n/a self.assertIs(copy.copy(x), x)
149n/a
150n/a def test_copy_bytearray(self):
151n/a x = bytearray(b'abc')
152n/a y = copy.copy(x)
153n/a self.assertEqual(y, x)
154n/a self.assertIsNot(y, x)
155n/a x = bytearray()
156n/a y = copy.copy(x)
157n/a self.assertEqual(y, x)
158n/a self.assertIsNot(y, x)
159n/a
160n/a def test_copy_inst_vanilla(self):
161n/a class C:
162n/a def __init__(self, foo):
163n/a self.foo = foo
164n/a def __eq__(self, other):
165n/a return self.foo == other.foo
166n/a x = C(42)
167n/a self.assertEqual(copy.copy(x), x)
168n/a
169n/a def test_copy_inst_copy(self):
170n/a class C:
171n/a def __init__(self, foo):
172n/a self.foo = foo
173n/a def __copy__(self):
174n/a return C(self.foo)
175n/a def __eq__(self, other):
176n/a return self.foo == other.foo
177n/a x = C(42)
178n/a self.assertEqual(copy.copy(x), x)
179n/a
180n/a def test_copy_inst_getinitargs(self):
181n/a class C:
182n/a def __init__(self, foo):
183n/a self.foo = foo
184n/a def __getinitargs__(self):
185n/a return (self.foo,)
186n/a def __eq__(self, other):
187n/a return self.foo == other.foo
188n/a x = C(42)
189n/a self.assertEqual(copy.copy(x), x)
190n/a
191n/a def test_copy_inst_getnewargs(self):
192n/a class C(int):
193n/a def __new__(cls, foo):
194n/a self = int.__new__(cls)
195n/a self.foo = foo
196n/a return self
197n/a def __getnewargs__(self):
198n/a return self.foo,
199n/a def __eq__(self, other):
200n/a return self.foo == other.foo
201n/a x = C(42)
202n/a y = copy.copy(x)
203n/a self.assertIsInstance(y, C)
204n/a self.assertEqual(y, x)
205n/a self.assertIsNot(y, x)
206n/a self.assertEqual(y.foo, x.foo)
207n/a
208n/a def test_copy_inst_getnewargs_ex(self):
209n/a class C(int):
210n/a def __new__(cls, *, foo):
211n/a self = int.__new__(cls)
212n/a self.foo = foo
213n/a return self
214n/a def __getnewargs_ex__(self):
215n/a return (), {'foo': self.foo}
216n/a def __eq__(self, other):
217n/a return self.foo == other.foo
218n/a x = C(foo=42)
219n/a y = copy.copy(x)
220n/a self.assertIsInstance(y, C)
221n/a self.assertEqual(y, x)
222n/a self.assertIsNot(y, x)
223n/a self.assertEqual(y.foo, x.foo)
224n/a
225n/a def test_copy_inst_getstate(self):
226n/a class C:
227n/a def __init__(self, foo):
228n/a self.foo = foo
229n/a def __getstate__(self):
230n/a return {"foo": self.foo}
231n/a def __eq__(self, other):
232n/a return self.foo == other.foo
233n/a x = C(42)
234n/a self.assertEqual(copy.copy(x), x)
235n/a
236n/a def test_copy_inst_setstate(self):
237n/a class C:
238n/a def __init__(self, foo):
239n/a self.foo = foo
240n/a def __setstate__(self, state):
241n/a self.foo = state["foo"]
242n/a def __eq__(self, other):
243n/a return self.foo == other.foo
244n/a x = C(42)
245n/a self.assertEqual(copy.copy(x), x)
246n/a
247n/a def test_copy_inst_getstate_setstate(self):
248n/a class C:
249n/a def __init__(self, foo):
250n/a self.foo = foo
251n/a def __getstate__(self):
252n/a return self.foo
253n/a def __setstate__(self, state):
254n/a self.foo = state
255n/a def __eq__(self, other):
256n/a return self.foo == other.foo
257n/a x = C(42)
258n/a self.assertEqual(copy.copy(x), x)
259n/a # State with boolean value is false (issue #25718)
260n/a x = C(0.0)
261n/a self.assertEqual(copy.copy(x), x)
262n/a
263n/a # The deepcopy() method
264n/a
265n/a def test_deepcopy_basic(self):
266n/a x = 42
267n/a y = copy.deepcopy(x)
268n/a self.assertEqual(y, x)
269n/a
270n/a def test_deepcopy_memo(self):
271n/a # Tests of reflexive objects are under type-specific sections below.
272n/a # This tests only repetitions of objects.
273n/a x = []
274n/a x = [x, x]
275n/a y = copy.deepcopy(x)
276n/a self.assertEqual(y, x)
277n/a self.assertIsNot(y, x)
278n/a self.assertIsNot(y[0], x[0])
279n/a self.assertIs(y[0], y[1])
280n/a
281n/a def test_deepcopy_issubclass(self):
282n/a # XXX Note: there's no way to test the TypeError coming out of
283n/a # issubclass() -- this can only happen when an extension
284n/a # module defines a "type" that doesn't formally inherit from
285n/a # type.
286n/a class Meta(type):
287n/a pass
288n/a class C(metaclass=Meta):
289n/a pass
290n/a self.assertEqual(copy.deepcopy(C), C)
291n/a
292n/a def test_deepcopy_deepcopy(self):
293n/a class C(object):
294n/a def __init__(self, foo):
295n/a self.foo = foo
296n/a def __deepcopy__(self, memo=None):
297n/a return C(self.foo)
298n/a x = C(42)
299n/a y = copy.deepcopy(x)
300n/a self.assertEqual(y.__class__, x.__class__)
301n/a self.assertEqual(y.foo, x.foo)
302n/a
303n/a def test_deepcopy_registry(self):
304n/a class C(object):
305n/a def __new__(cls, foo):
306n/a obj = object.__new__(cls)
307n/a obj.foo = foo
308n/a return obj
309n/a def pickle_C(obj):
310n/a return (C, (obj.foo,))
311n/a x = C(42)
312n/a self.assertRaises(TypeError, copy.deepcopy, x)
313n/a copyreg.pickle(C, pickle_C, C)
314n/a y = copy.deepcopy(x)
315n/a
316n/a def test_deepcopy_reduce_ex(self):
317n/a class C(object):
318n/a def __reduce_ex__(self, proto):
319n/a c.append(1)
320n/a return ""
321n/a def __reduce__(self):
322n/a self.fail("shouldn't call this")
323n/a c = []
324n/a x = C()
325n/a y = copy.deepcopy(x)
326n/a self.assertIs(y, x)
327n/a self.assertEqual(c, [1])
328n/a
329n/a def test_deepcopy_reduce(self):
330n/a class C(object):
331n/a def __reduce__(self):
332n/a c.append(1)
333n/a return ""
334n/a c = []
335n/a x = C()
336n/a y = copy.deepcopy(x)
337n/a self.assertIs(y, x)
338n/a self.assertEqual(c, [1])
339n/a
340n/a def test_deepcopy_cant(self):
341n/a class C(object):
342n/a def __getattribute__(self, name):
343n/a if name.startswith("__reduce"):
344n/a raise AttributeError(name)
345n/a return object.__getattribute__(self, name)
346n/a x = C()
347n/a self.assertRaises(copy.Error, copy.deepcopy, x)
348n/a
349n/a # Type-specific _deepcopy_xxx() methods
350n/a
351n/a def test_deepcopy_atomic(self):
352n/a class Classic:
353n/a pass
354n/a class NewStyle(object):
355n/a pass
356n/a def f():
357n/a pass
358n/a tests = [None, 42, 2**100, 3.14, True, False, 1j,
359n/a "hello", "hello\u1234", f.__code__,
360n/a NewStyle, Classic, max]
361n/a for x in tests:
362n/a self.assertIs(copy.deepcopy(x), x)
363n/a
364n/a def test_deepcopy_list(self):
365n/a x = [[1, 2], 3]
366n/a y = copy.deepcopy(x)
367n/a self.assertEqual(y, x)
368n/a self.assertIsNot(x, y)
369n/a self.assertIsNot(x[0], y[0])
370n/a
371n/a def test_deepcopy_reflexive_list(self):
372n/a x = []
373n/a x.append(x)
374n/a y = copy.deepcopy(x)
375n/a for op in comparisons:
376n/a self.assertRaises(RecursionError, op, y, x)
377n/a self.assertIsNot(y, x)
378n/a self.assertIs(y[0], y)
379n/a self.assertEqual(len(y), 1)
380n/a
381n/a def test_deepcopy_empty_tuple(self):
382n/a x = ()
383n/a y = copy.deepcopy(x)
384n/a self.assertIs(x, y)
385n/a
386n/a def test_deepcopy_tuple(self):
387n/a x = ([1, 2], 3)
388n/a y = copy.deepcopy(x)
389n/a self.assertEqual(y, x)
390n/a self.assertIsNot(x, y)
391n/a self.assertIsNot(x[0], y[0])
392n/a
393n/a def test_deepcopy_tuple_of_immutables(self):
394n/a x = ((1, 2), 3)
395n/a y = copy.deepcopy(x)
396n/a self.assertIs(x, y)
397n/a
398n/a def test_deepcopy_reflexive_tuple(self):
399n/a x = ([],)
400n/a x[0].append(x)
401n/a y = copy.deepcopy(x)
402n/a for op in comparisons:
403n/a self.assertRaises(RecursionError, op, y, x)
404n/a self.assertIsNot(y, x)
405n/a self.assertIsNot(y[0], x[0])
406n/a self.assertIs(y[0][0], y)
407n/a
408n/a def test_deepcopy_dict(self):
409n/a x = {"foo": [1, 2], "bar": 3}
410n/a y = copy.deepcopy(x)
411n/a self.assertEqual(y, x)
412n/a self.assertIsNot(x, y)
413n/a self.assertIsNot(x["foo"], y["foo"])
414n/a
415n/a def test_deepcopy_reflexive_dict(self):
416n/a x = {}
417n/a x['foo'] = x
418n/a y = copy.deepcopy(x)
419n/a for op in order_comparisons:
420n/a self.assertRaises(TypeError, op, y, x)
421n/a for op in equality_comparisons:
422n/a self.assertRaises(RecursionError, op, y, x)
423n/a self.assertIsNot(y, x)
424n/a self.assertIs(y['foo'], y)
425n/a self.assertEqual(len(y), 1)
426n/a
427n/a def test_deepcopy_keepalive(self):
428n/a memo = {}
429n/a x = []
430n/a y = copy.deepcopy(x, memo)
431n/a self.assertIs(memo[id(memo)][0], x)
432n/a
433n/a def test_deepcopy_dont_memo_immutable(self):
434n/a memo = {}
435n/a x = [1, 2, 3, 4]
436n/a y = copy.deepcopy(x, memo)
437n/a self.assertEqual(y, x)
438n/a # There's the entry for the new list, and the keep alive.
439n/a self.assertEqual(len(memo), 2)
440n/a
441n/a memo = {}
442n/a x = [(1, 2)]
443n/a y = copy.deepcopy(x, memo)
444n/a self.assertEqual(y, x)
445n/a # Tuples with immutable contents are immutable for deepcopy.
446n/a self.assertEqual(len(memo), 2)
447n/a
448n/a def test_deepcopy_inst_vanilla(self):
449n/a class C:
450n/a def __init__(self, foo):
451n/a self.foo = foo
452n/a def __eq__(self, other):
453n/a return self.foo == other.foo
454n/a x = C([42])
455n/a y = copy.deepcopy(x)
456n/a self.assertEqual(y, x)
457n/a self.assertIsNot(y.foo, x.foo)
458n/a
459n/a def test_deepcopy_inst_deepcopy(self):
460n/a class C:
461n/a def __init__(self, foo):
462n/a self.foo = foo
463n/a def __deepcopy__(self, memo):
464n/a return C(copy.deepcopy(self.foo, memo))
465n/a def __eq__(self, other):
466n/a return self.foo == other.foo
467n/a x = C([42])
468n/a y = copy.deepcopy(x)
469n/a self.assertEqual(y, x)
470n/a self.assertIsNot(y, x)
471n/a self.assertIsNot(y.foo, x.foo)
472n/a
473n/a def test_deepcopy_inst_getinitargs(self):
474n/a class C:
475n/a def __init__(self, foo):
476n/a self.foo = foo
477n/a def __getinitargs__(self):
478n/a return (self.foo,)
479n/a def __eq__(self, other):
480n/a return self.foo == other.foo
481n/a x = C([42])
482n/a y = copy.deepcopy(x)
483n/a self.assertEqual(y, x)
484n/a self.assertIsNot(y, x)
485n/a self.assertIsNot(y.foo, x.foo)
486n/a
487n/a def test_deepcopy_inst_getnewargs(self):
488n/a class C(int):
489n/a def __new__(cls, foo):
490n/a self = int.__new__(cls)
491n/a self.foo = foo
492n/a return self
493n/a def __getnewargs__(self):
494n/a return self.foo,
495n/a def __eq__(self, other):
496n/a return self.foo == other.foo
497n/a x = C([42])
498n/a y = copy.deepcopy(x)
499n/a self.assertIsInstance(y, C)
500n/a self.assertEqual(y, x)
501n/a self.assertIsNot(y, x)
502n/a self.assertEqual(y.foo, x.foo)
503n/a self.assertIsNot(y.foo, x.foo)
504n/a
505n/a def test_deepcopy_inst_getnewargs_ex(self):
506n/a class C(int):
507n/a def __new__(cls, *, foo):
508n/a self = int.__new__(cls)
509n/a self.foo = foo
510n/a return self
511n/a def __getnewargs_ex__(self):
512n/a return (), {'foo': self.foo}
513n/a def __eq__(self, other):
514n/a return self.foo == other.foo
515n/a x = C(foo=[42])
516n/a y = copy.deepcopy(x)
517n/a self.assertIsInstance(y, C)
518n/a self.assertEqual(y, x)
519n/a self.assertIsNot(y, x)
520n/a self.assertEqual(y.foo, x.foo)
521n/a self.assertIsNot(y.foo, x.foo)
522n/a
523n/a def test_deepcopy_inst_getstate(self):
524n/a class C:
525n/a def __init__(self, foo):
526n/a self.foo = foo
527n/a def __getstate__(self):
528n/a return {"foo": self.foo}
529n/a def __eq__(self, other):
530n/a return self.foo == other.foo
531n/a x = C([42])
532n/a y = copy.deepcopy(x)
533n/a self.assertEqual(y, x)
534n/a self.assertIsNot(y, x)
535n/a self.assertIsNot(y.foo, x.foo)
536n/a
537n/a def test_deepcopy_inst_setstate(self):
538n/a class C:
539n/a def __init__(self, foo):
540n/a self.foo = foo
541n/a def __setstate__(self, state):
542n/a self.foo = state["foo"]
543n/a def __eq__(self, other):
544n/a return self.foo == other.foo
545n/a x = C([42])
546n/a y = copy.deepcopy(x)
547n/a self.assertEqual(y, x)
548n/a self.assertIsNot(y, x)
549n/a self.assertIsNot(y.foo, x.foo)
550n/a
551n/a def test_deepcopy_inst_getstate_setstate(self):
552n/a class C:
553n/a def __init__(self, foo):
554n/a self.foo = foo
555n/a def __getstate__(self):
556n/a return self.foo
557n/a def __setstate__(self, state):
558n/a self.foo = state
559n/a def __eq__(self, other):
560n/a return self.foo == other.foo
561n/a x = C([42])
562n/a y = copy.deepcopy(x)
563n/a self.assertEqual(y, x)
564n/a self.assertIsNot(y, x)
565n/a self.assertIsNot(y.foo, x.foo)
566n/a # State with boolean value is false (issue #25718)
567n/a x = C([])
568n/a y = copy.deepcopy(x)
569n/a self.assertEqual(y, x)
570n/a self.assertIsNot(y, x)
571n/a self.assertIsNot(y.foo, x.foo)
572n/a
573n/a def test_deepcopy_reflexive_inst(self):
574n/a class C:
575n/a pass
576n/a x = C()
577n/a x.foo = x
578n/a y = copy.deepcopy(x)
579n/a self.assertIsNot(y, x)
580n/a self.assertIs(y.foo, y)
581n/a
582n/a def test_deepcopy_range(self):
583n/a class I(int):
584n/a pass
585n/a x = range(I(10))
586n/a y = copy.deepcopy(x)
587n/a self.assertIsNot(y, x)
588n/a self.assertEqual(y, x)
589n/a self.assertIsNot(y.stop, x.stop)
590n/a self.assertEqual(y.stop, x.stop)
591n/a self.assertIsInstance(y.stop, I)
592n/a
593n/a # _reconstruct()
594n/a
595n/a def test_reconstruct_string(self):
596n/a class C(object):
597n/a def __reduce__(self):
598n/a return ""
599n/a x = C()
600n/a y = copy.copy(x)
601n/a self.assertIs(y, x)
602n/a y = copy.deepcopy(x)
603n/a self.assertIs(y, x)
604n/a
605n/a def test_reconstruct_nostate(self):
606n/a class C(object):
607n/a def __reduce__(self):
608n/a return (C, ())
609n/a x = C()
610n/a x.foo = 42
611n/a y = copy.copy(x)
612n/a self.assertIs(y.__class__, x.__class__)
613n/a y = copy.deepcopy(x)
614n/a self.assertIs(y.__class__, x.__class__)
615n/a
616n/a def test_reconstruct_state(self):
617n/a class C(object):
618n/a def __reduce__(self):
619n/a return (C, (), self.__dict__)
620n/a def __eq__(self, other):
621n/a return self.__dict__ == other.__dict__
622n/a x = C()
623n/a x.foo = [42]
624n/a y = copy.copy(x)
625n/a self.assertEqual(y, x)
626n/a y = copy.deepcopy(x)
627n/a self.assertEqual(y, x)
628n/a self.assertIsNot(y.foo, x.foo)
629n/a
630n/a def test_reconstruct_state_setstate(self):
631n/a class C(object):
632n/a def __reduce__(self):
633n/a return (C, (), self.__dict__)
634n/a def __setstate__(self, state):
635n/a self.__dict__.update(state)
636n/a def __eq__(self, other):
637n/a return self.__dict__ == other.__dict__
638n/a x = C()
639n/a x.foo = [42]
640n/a y = copy.copy(x)
641n/a self.assertEqual(y, x)
642n/a y = copy.deepcopy(x)
643n/a self.assertEqual(y, x)
644n/a self.assertIsNot(y.foo, x.foo)
645n/a
646n/a def test_reconstruct_reflexive(self):
647n/a class C(object):
648n/a pass
649n/a x = C()
650n/a x.foo = x
651n/a y = copy.deepcopy(x)
652n/a self.assertIsNot(y, x)
653n/a self.assertIs(y.foo, y)
654n/a
655n/a # Additions for Python 2.3 and pickle protocol 2
656n/a
657n/a def test_reduce_4tuple(self):
658n/a class C(list):
659n/a def __reduce__(self):
660n/a return (C, (), self.__dict__, iter(self))
661n/a def __eq__(self, other):
662n/a return (list(self) == list(other) and
663n/a self.__dict__ == other.__dict__)
664n/a x = C([[1, 2], 3])
665n/a y = copy.copy(x)
666n/a self.assertEqual(x, y)
667n/a self.assertIsNot(x, y)
668n/a self.assertIs(x[0], y[0])
669n/a y = copy.deepcopy(x)
670n/a self.assertEqual(x, y)
671n/a self.assertIsNot(x, y)
672n/a self.assertIsNot(x[0], y[0])
673n/a
674n/a def test_reduce_5tuple(self):
675n/a class C(dict):
676n/a def __reduce__(self):
677n/a return (C, (), self.__dict__, None, self.items())
678n/a def __eq__(self, other):
679n/a return (dict(self) == dict(other) and
680n/a self.__dict__ == other.__dict__)
681n/a x = C([("foo", [1, 2]), ("bar", 3)])
682n/a y = copy.copy(x)
683n/a self.assertEqual(x, y)
684n/a self.assertIsNot(x, y)
685n/a self.assertIs(x["foo"], y["foo"])
686n/a y = copy.deepcopy(x)
687n/a self.assertEqual(x, y)
688n/a self.assertIsNot(x, y)
689n/a self.assertIsNot(x["foo"], y["foo"])
690n/a
691n/a def test_copy_slots(self):
692n/a class C(object):
693n/a __slots__ = ["foo"]
694n/a x = C()
695n/a x.foo = [42]
696n/a y = copy.copy(x)
697n/a self.assertIs(x.foo, y.foo)
698n/a
699n/a def test_deepcopy_slots(self):
700n/a class C(object):
701n/a __slots__ = ["foo"]
702n/a x = C()
703n/a x.foo = [42]
704n/a y = copy.deepcopy(x)
705n/a self.assertEqual(x.foo, y.foo)
706n/a self.assertIsNot(x.foo, y.foo)
707n/a
708n/a def test_deepcopy_dict_subclass(self):
709n/a class C(dict):
710n/a def __init__(self, d=None):
711n/a if not d:
712n/a d = {}
713n/a self._keys = list(d.keys())
714n/a super().__init__(d)
715n/a def __setitem__(self, key, item):
716n/a super().__setitem__(key, item)
717n/a if key not in self._keys:
718n/a self._keys.append(key)
719n/a x = C(d={'foo':0})
720n/a y = copy.deepcopy(x)
721n/a self.assertEqual(x, y)
722n/a self.assertEqual(x._keys, y._keys)
723n/a self.assertIsNot(x, y)
724n/a x['bar'] = 1
725n/a self.assertNotEqual(x, y)
726n/a self.assertNotEqual(x._keys, y._keys)
727n/a
728n/a def test_copy_list_subclass(self):
729n/a class C(list):
730n/a pass
731n/a x = C([[1, 2], 3])
732n/a x.foo = [4, 5]
733n/a y = copy.copy(x)
734n/a self.assertEqual(list(x), list(y))
735n/a self.assertEqual(x.foo, y.foo)
736n/a self.assertIs(x[0], y[0])
737n/a self.assertIs(x.foo, y.foo)
738n/a
739n/a def test_deepcopy_list_subclass(self):
740n/a class C(list):
741n/a pass
742n/a x = C([[1, 2], 3])
743n/a x.foo = [4, 5]
744n/a y = copy.deepcopy(x)
745n/a self.assertEqual(list(x), list(y))
746n/a self.assertEqual(x.foo, y.foo)
747n/a self.assertIsNot(x[0], y[0])
748n/a self.assertIsNot(x.foo, y.foo)
749n/a
750n/a def test_copy_tuple_subclass(self):
751n/a class C(tuple):
752n/a pass
753n/a x = C([1, 2, 3])
754n/a self.assertEqual(tuple(x), (1, 2, 3))
755n/a y = copy.copy(x)
756n/a self.assertEqual(tuple(y), (1, 2, 3))
757n/a
758n/a def test_deepcopy_tuple_subclass(self):
759n/a class C(tuple):
760n/a pass
761n/a x = C([[1, 2], 3])
762n/a self.assertEqual(tuple(x), ([1, 2], 3))
763n/a y = copy.deepcopy(x)
764n/a self.assertEqual(tuple(y), ([1, 2], 3))
765n/a self.assertIsNot(x, y)
766n/a self.assertIsNot(x[0], y[0])
767n/a
768n/a def test_getstate_exc(self):
769n/a class EvilState(object):
770n/a def __getstate__(self):
771n/a raise ValueError("ain't got no stickin' state")
772n/a self.assertRaises(ValueError, copy.copy, EvilState())
773n/a
774n/a def test_copy_function(self):
775n/a self.assertEqual(copy.copy(global_foo), global_foo)
776n/a def foo(x, y): return x+y
777n/a self.assertEqual(copy.copy(foo), foo)
778n/a bar = lambda: None
779n/a self.assertEqual(copy.copy(bar), bar)
780n/a
781n/a def test_deepcopy_function(self):
782n/a self.assertEqual(copy.deepcopy(global_foo), global_foo)
783n/a def foo(x, y): return x+y
784n/a self.assertEqual(copy.deepcopy(foo), foo)
785n/a bar = lambda: None
786n/a self.assertEqual(copy.deepcopy(bar), bar)
787n/a
788n/a def _check_weakref(self, _copy):
789n/a class C(object):
790n/a pass
791n/a obj = C()
792n/a x = weakref.ref(obj)
793n/a y = _copy(x)
794n/a self.assertIs(y, x)
795n/a del obj
796n/a y = _copy(x)
797n/a self.assertIs(y, x)
798n/a
799n/a def test_copy_weakref(self):
800n/a self._check_weakref(copy.copy)
801n/a
802n/a def test_deepcopy_weakref(self):
803n/a self._check_weakref(copy.deepcopy)
804n/a
805n/a def _check_copy_weakdict(self, _dicttype):
806n/a class C(object):
807n/a pass
808n/a a, b, c, d = [C() for i in range(4)]
809n/a u = _dicttype()
810n/a u[a] = b
811n/a u[c] = d
812n/a v = copy.copy(u)
813n/a self.assertIsNot(v, u)
814n/a self.assertEqual(v, u)
815n/a self.assertEqual(v[a], b)
816n/a self.assertEqual(v[c], d)
817n/a self.assertEqual(len(v), 2)
818n/a del c, d
819n/a self.assertEqual(len(v), 1)
820n/a x, y = C(), C()
821n/a # The underlying containers are decoupled
822n/a v[x] = y
823n/a self.assertNotIn(x, u)
824n/a
825n/a def test_copy_weakkeydict(self):
826n/a self._check_copy_weakdict(weakref.WeakKeyDictionary)
827n/a
828n/a def test_copy_weakvaluedict(self):
829n/a self._check_copy_weakdict(weakref.WeakValueDictionary)
830n/a
831n/a def test_deepcopy_weakkeydict(self):
832n/a class C(object):
833n/a def __init__(self, i):
834n/a self.i = i
835n/a a, b, c, d = [C(i) for i in range(4)]
836n/a u = weakref.WeakKeyDictionary()
837n/a u[a] = b
838n/a u[c] = d
839n/a # Keys aren't copied, values are
840n/a v = copy.deepcopy(u)
841n/a self.assertNotEqual(v, u)
842n/a self.assertEqual(len(v), 2)
843n/a self.assertIsNot(v[a], b)
844n/a self.assertIsNot(v[c], d)
845n/a self.assertEqual(v[a].i, b.i)
846n/a self.assertEqual(v[c].i, d.i)
847n/a del c
848n/a self.assertEqual(len(v), 1)
849n/a
850n/a def test_deepcopy_weakvaluedict(self):
851n/a class C(object):
852n/a def __init__(self, i):
853n/a self.i = i
854n/a a, b, c, d = [C(i) for i in range(4)]
855n/a u = weakref.WeakValueDictionary()
856n/a u[a] = b
857n/a u[c] = d
858n/a # Keys are copied, values aren't
859n/a v = copy.deepcopy(u)
860n/a self.assertNotEqual(v, u)
861n/a self.assertEqual(len(v), 2)
862n/a (x, y), (z, t) = sorted(v.items(), key=lambda pair: pair[0].i)
863n/a self.assertIsNot(x, a)
864n/a self.assertEqual(x.i, a.i)
865n/a self.assertIs(y, b)
866n/a self.assertIsNot(z, c)
867n/a self.assertEqual(z.i, c.i)
868n/a self.assertIs(t, d)
869n/a del x, y, z, t
870n/a del d
871n/a self.assertEqual(len(v), 1)
872n/a
873n/a def test_deepcopy_bound_method(self):
874n/a class Foo(object):
875n/a def m(self):
876n/a pass
877n/a f = Foo()
878n/a f.b = f.m
879n/a g = copy.deepcopy(f)
880n/a self.assertEqual(g.m, g.b)
881n/a self.assertIs(g.b.__self__, g)
882n/a g.b()
883n/a
884n/a
885n/adef global_foo(x, y): return x+y
886n/a
887n/aif __name__ == "__main__":
888n/a unittest.main()