Skip to content

Latest commit

 

History

History
304 lines (251 loc) · 8.48 KB

File metadata and controls

304 lines (251 loc) · 8.48 KB
 
Feb 4, 2000
Feb 4, 2000
1
"""Generic (shallow and deep) copying operations.
Jan 10, 1995
Jan 10, 1995
2
Feb 9, 1995
Feb 9, 1995
3
Interface summary:
4
Jan 14, 2001
Jan 14, 2001
5
import copy
Feb 9, 1995
Feb 9, 1995
6
Jan 14, 2001
Jan 14, 2001
7
x = copy.copy(y) # make a shallow copy of y
8
x = copy.deepcopy(y) # make a deep copy of y
Feb 9, 1995
Feb 9, 1995
9
Feb 6, 2003
Feb 6, 2003
10
For module specific errors, copy.Error is raised.
Feb 9, 1995
Feb 9, 1995
11
12
The difference between shallow and deep copying is only relevant for
13
compound objects (objects that contain other objects, like lists or
14
class instances).
15
16
- A shallow copy constructs a new compound object and then (to the
Jun 13, 2005
Jun 13, 2005
17
extent possible) inserts *the same objects* into it that the
Feb 9, 1995
Feb 9, 1995
18
original contains.
19
20
- A deep copy constructs a new compound object and then, recursively,
21
inserts *copies* into it of the objects found in the original.
22
23
Two problems often exist with deep copy operations that don't exist
24
with shallow copy operations:
25
May 28, 1997
May 28, 1997
26
a) recursive objects (compound objects that, directly or indirectly,
Feb 9, 1995
Feb 9, 1995
27
contain a reference to themselves) may cause a recursive loop
28
May 28, 1997
May 28, 1997
29
b) because deep copy copies *everything* it may copy too much, e.g.
Feb 9, 1995
Feb 9, 1995
30
administrative data structures that should be shared even between
31
copies
32
33
Python's deep copy operation avoids these problems by:
34
May 28, 1997
May 28, 1997
35
a) keeping a table of objects already copied during the current
36
copying pass
Feb 9, 1995
Feb 9, 1995
37
May 28, 1997
May 28, 1997
38
b) letting user-defined classes override the copying operation or the
Feb 9, 1995
Feb 9, 1995
39
set of components copied
40
41
This version does not copy types like module, class, function, method,
Nov 14, 2021
Nov 14, 2021
42
nor stack trace, stack frame, nor file, socket, window, nor any
43
similar types.
Feb 9, 1995
Feb 9, 1995
44
45
Classes can use the same interfaces to control copying that they use
46
to control pickling: they can define methods called __getinitargs__(),
Dec 7, 1997
Dec 7, 1997
47
__getstate__() and __setstate__(). See the documentation for module
Feb 9, 1995
Feb 9, 1995
48
"pickle" for information on these methods.
49
"""
Jan 10, 1995
Jan 10, 1995
50
51
import types
May 15, 2009
May 15, 2009
52
import weakref
May 11, 2008
May 11, 2008
53
from copyreg import dispatch_table
Jan 10, 1995
Jan 10, 1995
54
Aug 17, 2000
Aug 17, 2000
55
class Error(Exception):
Jan 14, 2001
Jan 14, 2001
56
pass
57
error = Error # backward compatibility
Jan 10, 1995
Jan 10, 1995
58
Nov 27, 2000
Nov 27, 2000
59
try:
60
from org.python.core import PyStringMap
61
except ImportError:
62
PyStringMap = None
63
Feb 6, 2003
Feb 6, 2003
64
__all__ = ["Error", "copy", "deepcopy"]
Jan 20, 2001
Jan 20, 2001
65
Jan 10, 1995
Jan 10, 1995
66
def copy(x):
Jan 14, 2001
Jan 14, 2001
67
"""Shallow copy operation on arbitrary Python objects.
68
69
See the module's __doc__ string for more info.
70
"""
71
Feb 7, 2003
Feb 7, 2003
72
cls = type(x)
73
74
copier = _copy_dispatch.get(cls)
75
if copier:
76
return copier(x)
77
Jul 9, 2018
Jul 9, 2018
78
if issubclass(cls, type):
Dec 1, 2013
Dec 1, 2013
79
# treat it as a regular class:
80
return _copy_immutable(x)
81
Feb 7, 2003
Feb 7, 2003
82
copier = getattr(cls, "__copy__", None)
Jul 9, 2018
Jul 9, 2018
83
if copier is not None:
Feb 7, 2003
Feb 7, 2003
84
return copier(x)
85
86
reductor = dispatch_table.get(cls)
Jul 9, 2018
Jul 9, 2018
87
if reductor is not None:
Feb 19, 2003
Feb 19, 2003
88
rv = reductor(x)
89
else:
90
reductor = getattr(x, "__reduce_ex__", None)
Jul 9, 2018
Jul 9, 2018
91
if reductor is not None:
Mar 24, 2015
Mar 24, 2015
92
rv = reductor(4)
Feb 19, 2003
Feb 19, 2003
93
else:
94
reductor = getattr(x, "__reduce__", None)
95
if reductor:
96
rv = reductor()
97
else:
98
raise Error("un(shallow)copyable object of type %s" % cls)
99
Mar 6, 2016
Mar 6, 2016
100
if isinstance(rv, str):
101
return x
102
return _reconstruct(x, None, *rv)
Feb 19, 2003
Feb 19, 2003
103
Feb 6, 2003
Feb 6, 2003
104
Jan 10, 1995
Jan 10, 1995
105
_copy_dispatch = d = {}
106
Mar 8, 2004
Mar 8, 2004
107
def _copy_immutable(x):
Jan 14, 2001
Jan 14, 2001
108
return x
Mar 6, 2016
Mar 6, 2016
109
for t in (type(None), int, float, bool, complex, str, tuple,
Jan 12, 2020
Jan 12, 2020
110
bytes, frozenset, type, range, slice, property,
Mar 6, 2016
Mar 6, 2016
111
types.BuiltinFunctionType, type(Ellipsis), type(NotImplemented),
May 15, 2009
May 15, 2009
112
types.FunctionType, weakref.ref):
Mar 8, 2004
Mar 8, 2004
113
d[t] = _copy_immutable
Jun 7, 2007
Jun 7, 2007
114
t = getattr(types, "CodeType", None)
115
if t is not None:
116
d[t] = _copy_immutable
Mar 6, 2016
Mar 6, 2016
117
118
d[list] = list.copy
119
d[dict] = dict.copy
120
d[set] = set.copy
121
d[bytearray] = bytearray.copy
122
Nov 27, 2000
Nov 27, 2000
123
if PyStringMap is not None:
Mar 6, 2016
Mar 6, 2016
124
d[PyStringMap] = PyStringMap.copy
Jan 10, 1995
Jan 10, 1995
125
Mar 6, 2016
Mar 6, 2016
126
del d, t
Jan 10, 1995
Jan 10, 1995
127
Feb 7, 2003
Feb 7, 2003
128
def deepcopy(x, memo=None, _nil=[]):
Jan 14, 2001
Jan 14, 2001
129
"""Deep copy operation on arbitrary Python objects.
130
131
See the module's __doc__ string for more info.
132
"""
133
134
if memo is None:
135
memo = {}
Feb 7, 2003
Feb 7, 2003
136
Jan 14, 2001
Jan 14, 2001
137
d = id(x)
Feb 7, 2003
Feb 7, 2003
138
y = memo.get(d, _nil)
139
if y is not _nil:
140
return y
141
142
cls = type(x)
143
144
copier = _deepcopy_dispatch.get(cls)
Jul 9, 2018
Jul 9, 2018
145
if copier is not None:
Feb 7, 2003
Feb 7, 2003
146
y = copier(x, memo)
147
else:
Jul 9, 2018
Jul 9, 2018
148
if issubclass(cls, type):
Feb 19, 2003
Feb 19, 2003
149
y = _deepcopy_atomic(x, memo)
Feb 7, 2003
Feb 7, 2003
150
else:
Feb 19, 2003
Feb 19, 2003
151
copier = getattr(x, "__deepcopy__", None)
Jul 9, 2018
Jul 9, 2018
152
if copier is not None:
Feb 19, 2003
Feb 19, 2003
153
y = copier(memo)
154
else:
155
reductor = dispatch_table.get(cls)
156
if reductor:
157
rv = reductor(x)
158
else:
159
reductor = getattr(x, "__reduce_ex__", None)
Jul 9, 2018
Jul 9, 2018
160
if reductor is not None:
Mar 24, 2015
Mar 24, 2015
161
rv = reductor(4)
Feb 19, 2003
Feb 19, 2003
162
else:
163
reductor = getattr(x, "__reduce__", None)
164
if reductor:
165
rv = reductor()
166
else:
167
raise Error(
168
"un(deep)copyable object of type %s" % cls)
Mar 6, 2016
Mar 6, 2016
169
if isinstance(rv, str):
170
y = x
171
else:
172
y = _reconstruct(x, memo, *rv)
Feb 7, 2003
Feb 7, 2003
173
Jun 27, 2011
Jun 27, 2011
174
# If is its own copy, don't memoize.
175
if y is not x:
176
memo[d] = y
177
_keep_alive(x, memo) # Make sure x lives at least as long as d
Jan 14, 2001
Jan 14, 2001
178
return y
Jan 10, 1995
Jan 10, 1995
179
180
_deepcopy_dispatch = d = {}
181
182
def _deepcopy_atomic(x, memo):
Jan 14, 2001
Jan 14, 2001
183
return x
Feb 7, 2005
Feb 7, 2005
184
d[type(None)] = _deepcopy_atomic
Mar 25, 2008
Mar 25, 2008
185
d[type(Ellipsis)] = _deepcopy_atomic
Mar 6, 2016
Mar 6, 2016
186
d[type(NotImplemented)] = _deepcopy_atomic
Feb 7, 2005
Feb 7, 2005
187
d[int] = _deepcopy_atomic
188
d[float] = _deepcopy_atomic
189
d[bool] = _deepcopy_atomic
Mar 6, 2016
Mar 6, 2016
190
d[complex] = _deepcopy_atomic
Nov 6, 2007
Nov 6, 2007
191
d[bytes] = _deepcopy_atomic
Feb 7, 2005
Feb 7, 2005
192
d[str] = _deepcopy_atomic
Jul 9, 2018
Jul 9, 2018
193
d[types.CodeType] = _deepcopy_atomic
Feb 7, 2005
Feb 7, 2005
194
d[type] = _deepcopy_atomic
May 28, 2020
May 28, 2020
195
d[range] = _deepcopy_atomic
Jun 14, 2003
Jun 14, 2003
196
d[types.BuiltinFunctionType] = _deepcopy_atomic
Feb 25, 2006
Feb 25, 2006
197
d[types.FunctionType] = _deepcopy_atomic
May 15, 2009
May 15, 2009
198
d[weakref.ref] = _deepcopy_atomic
Jan 12, 2020
Jan 12, 2020
199
d[property] = _deepcopy_atomic
Jan 10, 1995
Jan 10, 1995
200
Mar 6, 2016
Mar 6, 2016
201
def _deepcopy_list(x, memo, deepcopy=deepcopy):
Jan 14, 2001
Jan 14, 2001
202
y = []
203
memo[id(x)] = y
Mar 6, 2016
Mar 6, 2016
204
append = y.append
Jan 14, 2001
Jan 14, 2001
205
for a in x:
Mar 6, 2016
Mar 6, 2016
206
append(deepcopy(a, memo))
Jan 14, 2001
Jan 14, 2001
207
return y
Feb 7, 2005
Feb 7, 2005
208
d[list] = _deepcopy_list
Jan 10, 1995
Jan 10, 1995
209
Mar 6, 2016
Mar 6, 2016
210
def _deepcopy_tuple(x, memo, deepcopy=deepcopy):
May 4, 2014
May 4, 2014
211
y = [deepcopy(a, memo) for a in x]
Jun 27, 2011
Jun 27, 2011
212
# We're not going to put the tuple in the memo, but it's still important we
213
# check for it, in case the tuple contains recursive mutable structures.
Jan 14, 2001
Jan 14, 2001
214
try:
Jun 27, 2011
Jun 27, 2011
215
return memo[id(x)]
Jan 14, 2001
Jan 14, 2001
216
except KeyError:
217
pass
May 4, 2014
May 4, 2014
218
for k, j in zip(x, y):
219
if k is not j:
Jan 14, 2001
Jan 14, 2001
220
y = tuple(y)
221
break
222
else:
223
y = x
224
return y
Feb 7, 2005
Feb 7, 2005
225
d[tuple] = _deepcopy_tuple
Jan 10, 1995
Jan 10, 1995
226
Mar 6, 2016
Mar 6, 2016
227
def _deepcopy_dict(x, memo, deepcopy=deepcopy):
Jan 14, 2001
Jan 14, 2001
228
y = {}
229
memo[id(x)] = y
Feb 11, 2007
Feb 11, 2007
230
for key, value in x.items():
Jun 2, 2002
Jun 2, 2002
231
y[deepcopy(key, memo)] = deepcopy(value, memo)
Jan 14, 2001
Jan 14, 2001
232
return y
Feb 7, 2005
Feb 7, 2005
233
d[dict] = _deepcopy_dict
Nov 27, 2000
Nov 27, 2000
234
if PyStringMap is not None:
235
d[PyStringMap] = _deepcopy_dict
Jan 10, 1995
Jan 10, 1995
236
Nov 28, 2009
Nov 28, 2009
237
def _deepcopy_method(x, memo): # Copy instance methods
238
return type(x)(x.__func__, deepcopy(x.__self__, memo))
Mar 6, 2016
Mar 6, 2016
239
d[types.MethodType] = _deepcopy_method
240
241
del d
Nov 28, 2009
Nov 28, 2009
242
Aug 20, 1997
Aug 20, 1997
243
def _keep_alive(x, memo):
Jan 14, 2001
Jan 14, 2001
244
"""Keeps a reference to the object x in the memo.
245
246
Because we remember objects by their id, we have
247
to assure that possibly temporary objects are kept
248
alive by referencing them.
249
We store a reference at the id of the memo, which should
250
normally not be used unless someone tries to deepcopy
251
the memo itself...
252
"""
253
try:
254
memo[id(memo)].append(x)
255
except KeyError:
256
# aha, this is the first one :-)
257
memo[id(memo)]=[x]
Aug 20, 1997
Aug 20, 1997
258
Mar 6, 2016
Mar 6, 2016
259
def _reconstruct(x, memo, func, args,
260
state=None, listiter=None, dictiter=None,
Jun 9, 2022
Jun 9, 2022
261
*, deepcopy=deepcopy):
Mar 6, 2016
Mar 6, 2016
262
deep = memo is not None
263
if deep and args:
264
args = (deepcopy(arg, memo) for arg in args)
265
y = func(*args)
Sep 28, 2001
Sep 28, 2001
266
if deep:
Mar 6, 2016
Mar 6, 2016
267
memo[id(x)] = y
Sep 4, 2010
Sep 4, 2010
268
Nov 30, 2015
Nov 30, 2015
269
if state is not None:
Sep 28, 2001
Sep 28, 2001
270
if deep:
Dec 28, 2001
Dec 28, 2001
271
state = deepcopy(state, memo)
Jun 6, 2002
Jun 6, 2002
272
if hasattr(y, '__setstate__'):
273
y.__setstate__(state)
274
else:
Feb 6, 2003
Feb 6, 2003
275
if isinstance(state, tuple) and len(state) == 2:
276
state, slotstate = state
277
else:
278
slotstate = None
279
if state is not None:
280
y.__dict__.update(state)
281
if slotstate is not None:
Feb 11, 2007
Feb 11, 2007
282
for key, value in slotstate.items():
Feb 6, 2003
Feb 6, 2003
283
setattr(y, key, value)
Sep 4, 2010
Sep 4, 2010
284
285
if listiter is not None:
Mar 6, 2016
Mar 6, 2016
286
if deep:
287
for item in listiter:
Sep 4, 2010
Sep 4, 2010
288
item = deepcopy(item, memo)
Mar 6, 2016
Mar 6, 2016
289
y.append(item)
290
else:
291
for item in listiter:
292
y.append(item)
Sep 4, 2010
Sep 4, 2010
293
if dictiter is not None:
Mar 6, 2016
Mar 6, 2016
294
if deep:
295
for key, value in dictiter:
Sep 4, 2010
Sep 4, 2010
296
key = deepcopy(key, memo)
297
value = deepcopy(value, memo)
Mar 6, 2016
Mar 6, 2016
298
y[key] = value
299
else:
300
for key, value in dictiter:
301
y[key] = value
Sep 28, 2001
Sep 28, 2001
302
return y
303
Mar 6, 2016
Mar 6, 2016
304
del types, weakref, PyStringMap