Mercurial > cpython-lru_cache-weakref
annotate Lib/copy.py @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | a1362a063122 |
| children | 6f9a2c874018 |
| rev | line source |
|---|---|
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
1 """\ |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
2 Generic (shallow and deep) copying operations |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
3 ============================================= |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
4 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
5 Interface summary: |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
6 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
7 import copy |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
8 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
9 x = copy.copy(y) # make a shallow copy of y |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
10 x = copy.deepcopy(y) # make a deep copy of y |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
11 |
|
2540
d7ae5fb1d255
rename copy.Error to copy.error
Guido van Rossum <guido@python.org>
parents:
2303
diff
changeset
|
12 For module specific errors, copy.error is raised. |
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
13 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
14 The difference between shallow and deep copying is only relevant for |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
15 compound objects (objects that contain other objects, like lists or |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
16 class instances). |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
17 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
18 - A shallow copy constructs a new compound object and then (to the |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
19 extent possible) inserts *the same objects* into in that the |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
20 original contains. |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
21 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
22 - A deep copy constructs a new compound object and then, recursively, |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
23 inserts *copies* into it of the objects found in the original. |
|
2101
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 |
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
25 Two problems often exist with deep copy operations that don't exist |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
26 with shallow copy operations: |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
27 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
28 (a) recursive objects (compound objects that, directly or indirectly, |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
29 contain a reference to themselves) may cause a recursive loop |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
30 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
31 (b) because deep copy copies *everything* it may copy too much, e.g. |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
32 administrative data structures that should be shared even between |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
33 copies |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
34 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
35 Python's deep copy operation avoids these problems by: |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
36 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
37 (a) keeping a table of objects already copied during the current |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
38 copying pass |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
39 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
40 (b) letting user-defined classes override the copying operation or the |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
41 set of components copied |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
42 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
43 This version does not copy types like module, class, function, method, |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
44 nor stack trace, stack frame, nor file, socket, window, nor array, nor |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
45 any similar types. |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
46 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
47 Classes can use the same interfaces to control copying that they use |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
48 to control pickling: they can define methods called __getinitargs__(), |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
49 __getstate__() and __setstate__(). See the __doc__ string of module |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
50 "pickle" for information on these methods. |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
51 """ |
|
2101
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 import types |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 |
|
2540
d7ae5fb1d255
rename copy.Error to copy.error
Guido van Rossum <guido@python.org>
parents:
2303
diff
changeset
|
55 error = 'copy.error' |
|
d7ae5fb1d255
rename copy.Error to copy.error
Guido van Rossum <guido@python.org>
parents:
2303
diff
changeset
|
56 Error = error # backward compatibility |
|
2101
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 def copy(x): |
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
59 """Shallow copy operation on arbitrary Python objects. |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
60 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
61 See the module's __doc__ string for more info. |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
62 """ |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
63 |
|
2101
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 try: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 copierfunction = _copy_dispatch[type(x)] |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 except KeyError: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 try: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 copier = x.__copy__ |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 except AttributeError: |
|
2540
d7ae5fb1d255
rename copy.Error to copy.error
Guido van Rossum <guido@python.org>
parents:
2303
diff
changeset
|
70 raise error, \ |
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
71 "un(shallow)copyable object of type %s" % type(x) |
|
2101
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 y = copier() |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 else: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 y = copierfunction(x) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 return y |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 _copy_dispatch = d = {} |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 def _copy_atomic(x): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 return x |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 d[types.NoneType] = _copy_atomic |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 d[types.IntType] = _copy_atomic |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 d[types.LongType] = _copy_atomic |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 d[types.FloatType] = _copy_atomic |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 d[types.StringType] = _copy_atomic |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 d[types.CodeType] = _copy_atomic |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 d[types.TypeType] = _copy_atomic |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 d[types.XRangeType] = _copy_atomic |
|
2540
d7ae5fb1d255
rename copy.Error to copy.error
Guido van Rossum <guido@python.org>
parents:
2303
diff
changeset
|
89 d[types.ClassType] = _copy_atomic |
|
2101
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 def _copy_list(x): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 return x[:] |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
93 d[types.ListType] = _copy_list |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 def _copy_tuple(x): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 return x[:] |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 d[types.TupleType] = _copy_tuple |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 def _copy_dict(x): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 y = {} |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 for key in x.keys(): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 y[key] = x[key] |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 return y |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 d[types.DictionaryType] = _copy_dict |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
106 def _copy_inst(x): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 if hasattr(x, '__copy__'): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 return x.__copy__() |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 if hasattr(x, '__getinitargs__'): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 args = x.__getinitargs__() |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 else: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 args = () |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 y = apply(x.__class__, args) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 if hasattr(x, '__getstate__'): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 state = x.__getstate__() |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 else: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 state = x.__dict__ |
|
2591
2a3e48972261
fix bogus hasttr usage
Guido van Rossum <guido@python.org>
parents:
2540
diff
changeset
|
118 if hasattr(y, '__setstate__'): |
|
2101
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 y.__setstate__(state) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 else: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 for key in state.keys(): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 setattr(y, key, state[key]) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 return y |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 d[types.InstanceType] = _copy_inst |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 del d |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 def deepcopy(x, memo = None): |
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
129 """Deep copy operation on arbitrary Python objects. |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
130 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
131 See the module's __doc__ string for more info. |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
132 """ |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
133 |
|
2101
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 if memo is None: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 memo = {} |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 d = id(x) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 if memo.has_key(d): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 return memo[d] |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 try: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 copierfunction = _deepcopy_dispatch[type(x)] |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 except KeyError: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
142 try: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 copier = x.__deepcopy__ |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 except AttributeError: |
|
2540
d7ae5fb1d255
rename copy.Error to copy.error
Guido van Rossum <guido@python.org>
parents:
2303
diff
changeset
|
145 raise error, \ |
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
146 "un-deep-copyable object of type %s" % type(x) |
|
2101
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 y = copier(memo) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 else: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 y = copierfunction(x, memo) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 memo[d] = y |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 return y |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 _deepcopy_dispatch = d = {} |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 def _deepcopy_atomic(x, memo): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 return x |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 d[types.NoneType] = _deepcopy_atomic |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 d[types.IntType] = _deepcopy_atomic |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 d[types.LongType] = _deepcopy_atomic |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 d[types.FloatType] = _deepcopy_atomic |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
161 d[types.StringType] = _deepcopy_atomic |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 d[types.CodeType] = _deepcopy_atomic |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 d[types.TypeType] = _deepcopy_atomic |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
164 d[types.XRangeType] = _deepcopy_atomic |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
165 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
166 def _deepcopy_list(x, memo): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
167 y = [] |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
168 memo[id(x)] = y |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
169 for a in x: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
170 y.append(deepcopy(a, memo)) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 return y |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 d[types.ListType] = _deepcopy_list |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 def _deepcopy_tuple(x, memo): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 y = [] |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 for a in x: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
177 y.append(deepcopy(a, memo)) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
178 d = id(x) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 try: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 return memo[d] |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 except KeyError: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 pass |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
183 for i in range(len(x)): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
184 if x[i] is not y[i]: |
|
3685
a1362a063122
Memoize _deepcopy_tuple() -- maybe this helps Tommy's problem.
Guido van Rossum <guido@python.org>
parents:
2591
diff
changeset
|
185 y = tuple(y) |
|
a1362a063122
Memoize _deepcopy_tuple() -- maybe this helps Tommy's problem.
Guido van Rossum <guido@python.org>
parents:
2591
diff
changeset
|
186 break |
|
a1362a063122
Memoize _deepcopy_tuple() -- maybe this helps Tommy's problem.
Guido van Rossum <guido@python.org>
parents:
2591
diff
changeset
|
187 else: |
|
a1362a063122
Memoize _deepcopy_tuple() -- maybe this helps Tommy's problem.
Guido van Rossum <guido@python.org>
parents:
2591
diff
changeset
|
188 y = x |
|
a1362a063122
Memoize _deepcopy_tuple() -- maybe this helps Tommy's problem.
Guido van Rossum <guido@python.org>
parents:
2591
diff
changeset
|
189 memo[d] = y |
|
a1362a063122
Memoize _deepcopy_tuple() -- maybe this helps Tommy's problem.
Guido van Rossum <guido@python.org>
parents:
2591
diff
changeset
|
190 return y |
|
2101
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
191 d[types.TupleType] = _deepcopy_tuple |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
192 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
193 def _deepcopy_dict(x, memo): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 y = {} |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 memo[id(x)] = y |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 for key in x.keys(): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 y[deepcopy(key, memo)] = deepcopy(x[key], memo) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 return y |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 d[types.DictionaryType] = _deepcopy_dict |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
201 def _deepcopy_inst(x, memo): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
202 if hasattr(x, '__deepcopy__'): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 return x.__deepcopy__() |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 if hasattr(x, '__getinitargs__'): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 args = x.__getinitargs__() |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
206 args = deepcopy(args, memo) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
207 else: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
208 args = () |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 y = apply(x.__class__, args) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
210 memo[id(x)] = y |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
211 if hasattr(x, '__getstate__'): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
212 state = x.__getstate__() |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
213 else: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
214 state = x.__dict__ |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
215 state = deepcopy(state, memo) |
|
2591
2a3e48972261
fix bogus hasttr usage
Guido van Rossum <guido@python.org>
parents:
2540
diff
changeset
|
216 if hasattr(y, '__setstate__'): |
|
2101
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
217 y.__setstate__(state) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
218 else: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
219 for key in state.keys(): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
220 setattr(y, key, state[key]) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
221 return y |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
222 d[types.InstanceType] = _deepcopy_inst |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
223 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
224 del d |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
226 del types |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
227 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 def _test(): |
|
2540
d7ae5fb1d255
rename copy.Error to copy.error
Guido van Rossum <guido@python.org>
parents:
2303
diff
changeset
|
229 l = [None, 1, 2L, 3.14, 'xyzzy', (1, 2L), [3.14, 'abc'], |
|
d7ae5fb1d255
rename copy.Error to copy.error
Guido van Rossum <guido@python.org>
parents:
2303
diff
changeset
|
230 {'abc': 'ABC'}, (), [], {}] |
|
2101
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
231 l1 = copy(l) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 print l1==l |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 l1 = map(copy, l) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
234 print l1==l |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 l1 = deepcopy(l) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
236 print l1==l |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 class C: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
238 def __init__(self, arg=None): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
239 self.a = 1 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
240 self.arg = arg |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
241 self.fp = open('copy.py') |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
242 self.fp.close() |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
243 def __getstate__(self): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
244 return {'a': self.a, 'arg': self.arg} |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
245 def __setstate__(self, state): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
246 for key in state.keys(): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
247 setattr(self, key, state[key]) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
248 def __deepcopy__(self, memo = None): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
249 new = self.__class__(deepcopy(self.arg, memo)) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
250 new.a = self.a |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
251 return new |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
252 c = C('argument sketch') |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
253 l.append(c) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
254 l2 = copy(l) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
255 print l == l2 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 print l |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 print l2 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
258 l2 = deepcopy(l) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 print l == l2 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
260 print l |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
261 print l2 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
262 l.append({l[1]: l, 'xyz': l[2]}) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
263 l3 = copy(l) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
264 import repr |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
265 print map(repr.repr, l) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
266 print map(repr.repr, l1) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
267 print map(repr.repr, l2) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
268 print map(repr.repr, l3) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
269 l3 = deepcopy(l) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
270 import repr |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
271 print map(repr.repr, l) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
272 print map(repr.repr, l1) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
273 print map(repr.repr, l2) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
274 print map(repr.repr, l3) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
275 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
276 if __name__ == '__main__': |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
277 _test() |
