Mercurial > cpython-lru_cache-weakref
annotate Lib/copy.py @ 5817:48b94de85bf3 legacy-trunk
Extensive changes by AMK.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 17 Jul 1997 16:29:42 +0000 |
| parents | 6f9a2c874018 |
| children | 07b7d4320b14 |
| 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 |
|
5659
6f9a2c874018
Remove '(' in column 0 of doc strings.
Guido van Rossum <guido@python.org>
parents:
3685
diff
changeset
|
28 a) recursive objects (compound objects that, directly or indirectly, |
|
2303
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 |
|
5659
6f9a2c874018
Remove '(' in column 0 of doc strings.
Guido van Rossum <guido@python.org>
parents:
3685
diff
changeset
|
31 b) because deep copy copies *everything* it may copy too much, e.g. |
|
2303
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 |
|
5659
6f9a2c874018
Remove '(' in column 0 of doc strings.
Guido van Rossum <guido@python.org>
parents:
3685
diff
changeset
|
37 a) keeping a table of objects already copied during the current |
|
6f9a2c874018
Remove '(' in column 0 of doc strings.
Guido van Rossum <guido@python.org>
parents:
3685
diff
changeset
|
38 copying pass |
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
39 |
|
5659
6f9a2c874018
Remove '(' in column 0 of doc strings.
Guido van Rossum <guido@python.org>
parents:
3685
diff
changeset
|
40 b) letting user-defined classes override the copying operation or the |
|
2303
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): |
|
5659
6f9a2c874018
Remove '(' in column 0 of doc strings.
Guido van Rossum <guido@python.org>
parents:
3685
diff
changeset
|
100 return x.copy() |
|
2101
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 d[types.DictionaryType] = _copy_dict |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 def _copy_inst(x): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 if hasattr(x, '__copy__'): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 return x.__copy__() |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
106 if hasattr(x, '__getinitargs__'): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 args = x.__getinitargs__() |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 else: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 args = () |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 y = apply(x.__class__, args) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
111 if hasattr(x, '__getstate__'): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 state = x.__getstate__() |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 else: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 state = x.__dict__ |
|
2591
2a3e48972261
fix bogus hasttr usage
Guido van Rossum <guido@python.org>
parents:
2540
diff
changeset
|
115 if hasattr(y, '__setstate__'): |
|
2101
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 y.__setstate__(state) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 else: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 for key in state.keys(): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 setattr(y, key, state[key]) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 return y |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 d[types.InstanceType] = _copy_inst |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
122 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 del d |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
124 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
125 def deepcopy(x, memo = None): |
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
126 """Deep copy operation on arbitrary Python objects. |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
127 |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
128 See the module's __doc__ string for more info. |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
129 """ |
|
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
130 |
|
2101
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 if memo is None: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 memo = {} |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 d = id(x) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 if memo.has_key(d): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 return memo[d] |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
136 try: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 copierfunction = _deepcopy_dispatch[type(x)] |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 except KeyError: |
|
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 copier = x.__deepcopy__ |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 except AttributeError: |
|
2540
d7ae5fb1d255
rename copy.Error to copy.error
Guido van Rossum <guido@python.org>
parents:
2303
diff
changeset
|
142 raise error, \ |
|
2303
505fc478ff08
added __doc__ strings etc.
Guido van Rossum <guido@python.org>
parents:
2101
diff
changeset
|
143 "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
|
144 y = copier(memo) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 else: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 y = copierfunction(x, memo) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 memo[d] = y |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 return y |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 _deepcopy_dispatch = d = {} |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 def _deepcopy_atomic(x, memo): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 return x |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 d[types.NoneType] = _deepcopy_atomic |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 d[types.IntType] = _deepcopy_atomic |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 d[types.LongType] = _deepcopy_atomic |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
157 d[types.FloatType] = _deepcopy_atomic |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 d[types.StringType] = _deepcopy_atomic |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 d[types.CodeType] = _deepcopy_atomic |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 d[types.TypeType] = _deepcopy_atomic |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
161 d[types.XRangeType] = _deepcopy_atomic |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 def _deepcopy_list(x, memo): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
164 y = [] |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
165 memo[id(x)] = y |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
166 for a in x: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
167 y.append(deepcopy(a, memo)) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
168 return y |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
169 d[types.ListType] = _deepcopy_list |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
170 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
171 def _deepcopy_tuple(x, memo): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 y = [] |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 for a in x: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 y.append(deepcopy(a, memo)) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 d = id(x) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 try: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
177 return memo[d] |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
178 except KeyError: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 pass |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 for i in range(len(x)): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 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
|
182 y = tuple(y) |
|
a1362a063122
Memoize _deepcopy_tuple() -- maybe this helps Tommy's problem.
Guido van Rossum <guido@python.org>
parents:
2591
diff
changeset
|
183 break |
|
a1362a063122
Memoize _deepcopy_tuple() -- maybe this helps Tommy's problem.
Guido van Rossum <guido@python.org>
parents:
2591
diff
changeset
|
184 else: |
|
a1362a063122
Memoize _deepcopy_tuple() -- maybe this helps Tommy's problem.
Guido van Rossum <guido@python.org>
parents:
2591
diff
changeset
|
185 y = x |
|
a1362a063122
Memoize _deepcopy_tuple() -- maybe this helps Tommy's problem.
Guido van Rossum <guido@python.org>
parents:
2591
diff
changeset
|
186 memo[d] = y |
|
a1362a063122
Memoize _deepcopy_tuple() -- maybe this helps Tommy's problem.
Guido van Rossum <guido@python.org>
parents:
2591
diff
changeset
|
187 return y |
|
2101
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
188 d[types.TupleType] = _deepcopy_tuple |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
190 def _deepcopy_dict(x, memo): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
191 y = {} |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
192 memo[id(x)] = y |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
193 for key in x.keys(): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 y[deepcopy(key, memo)] = deepcopy(x[key], memo) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 return y |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 d[types.DictionaryType] = _deepcopy_dict |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 def _deepcopy_inst(x, memo): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 if hasattr(x, '__deepcopy__'): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 return x.__deepcopy__() |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
201 if hasattr(x, '__getinitargs__'): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
202 args = x.__getinitargs__() |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 args = deepcopy(args, memo) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 else: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 args = () |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
206 y = apply(x.__class__, args) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
207 memo[id(x)] = y |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
208 if hasattr(x, '__getstate__'): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 state = x.__getstate__() |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
210 else: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
211 state = x.__dict__ |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
212 state = deepcopy(state, memo) |
|
2591
2a3e48972261
fix bogus hasttr usage
Guido van Rossum <guido@python.org>
parents:
2540
diff
changeset
|
213 if hasattr(y, '__setstate__'): |
|
2101
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
214 y.__setstate__(state) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
215 else: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
216 for key in state.keys(): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
217 setattr(y, key, state[key]) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
218 return y |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
219 d[types.InstanceType] = _deepcopy_inst |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
220 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
221 del d |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
222 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
223 del types |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
224 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 def _test(): |
|
2540
d7ae5fb1d255
rename copy.Error to copy.error
Guido van Rossum <guido@python.org>
parents:
2303
diff
changeset
|
226 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
|
227 {'abc': 'ABC'}, (), [], {}] |
|
2101
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 l1 = copy(l) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
229 print l1==l |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
230 l1 = map(copy, l) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
231 print l1==l |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 l1 = deepcopy(l) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 print l1==l |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
234 class C: |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 def __init__(self, arg=None): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
236 self.a = 1 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 self.arg = arg |
|
5659
6f9a2c874018
Remove '(' in column 0 of doc strings.
Guido van Rossum <guido@python.org>
parents:
3685
diff
changeset
|
238 if __name__ == '__main__': |
|
6f9a2c874018
Remove '(' in column 0 of doc strings.
Guido van Rossum <guido@python.org>
parents:
3685
diff
changeset
|
239 import sys |
|
6f9a2c874018
Remove '(' in column 0 of doc strings.
Guido van Rossum <guido@python.org>
parents:
3685
diff
changeset
|
240 file = sys.argv[0] |
|
6f9a2c874018
Remove '(' in column 0 of doc strings.
Guido van Rossum <guido@python.org>
parents:
3685
diff
changeset
|
241 else: |
|
6f9a2c874018
Remove '(' in column 0 of doc strings.
Guido van Rossum <guido@python.org>
parents:
3685
diff
changeset
|
242 file = __file__ |
|
6f9a2c874018
Remove '(' in column 0 of doc strings.
Guido van Rossum <guido@python.org>
parents:
3685
diff
changeset
|
243 self.fp = open(file) |
|
2101
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
244 self.fp.close() |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
245 def __getstate__(self): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
246 return {'a': self.a, 'arg': self.arg} |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
247 def __setstate__(self, state): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
248 for key in state.keys(): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
249 setattr(self, key, state[key]) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
250 def __deepcopy__(self, memo = None): |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
251 new = self.__class__(deepcopy(self.arg, memo)) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
252 new.a = self.a |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
253 return new |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
254 c = C('argument sketch') |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
255 l.append(c) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 l2 = copy(l) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 print l == l2 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
258 print l |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 print l2 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
260 l2 = deepcopy(l) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
261 print l == l2 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
262 print l |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
263 print l2 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
264 l.append({l[1]: l, 'xyz': l[2]}) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
265 l3 = copy(l) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
266 import repr |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
267 print map(repr.repr, l) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
268 print map(repr.repr, l1) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
269 print map(repr.repr, l2) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
270 print map(repr.repr, l3) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
271 l3 = deepcopy(l) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
272 import repr |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
273 print map(repr.repr, l) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
274 print map(repr.repr, l1) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
275 print map(repr.repr, l2) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
276 print map(repr.repr, l3) |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
277 |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
278 if __name__ == '__main__': |
|
fdccc3222d0a
shallow and deep copy operations
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
279 _test() |
