blob: a334b7961bb254fa58fccd1ac24077aaa87b592e [file] [log] [blame]
Guido van Rossum4b8c6ea2000-02-04 15:39:30 +00001"""Generic (shallow and deep) copying operations.
Guido van Rossum409780f1995-01-10 00:34:21 +00002
Guido van Rossumcc6764c1995-02-09 17:18:10 +00003Interface summary:
4
Tim Peters88869f92001-01-14 23:36:06 +00005 import copy
Guido van Rossumcc6764c1995-02-09 17:18:10 +00006
Tim Peters88869f92001-01-14 23:36:06 +00007 x = copy.copy(y) # make a shallow copy of y
8 x = copy.deepcopy(y) # make a deep copy of y
Guido van Rossumcc6764c1995-02-09 17:18:10 +00009
Guido van Rossumc7557582003-02-06 19:53:22 +000010For module specific errors, copy.Error is raised.
Guido van Rossumcc6764c1995-02-09 17:18:10 +000011
12The difference between shallow and deep copying is only relevant for
13compound objects (objects that contain other objects, like lists or
14class instances).
15
16- A shallow copy constructs a new compound object and then (to the
Raymond Hettingerf9d88ab2005-06-13 01:10:15 +000017 extent possible) inserts *the same objects* into it that the
Guido van Rossumcc6764c1995-02-09 17:18:10 +000018 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
23Two problems often exist with deep copy operations that don't exist
24with shallow copy operations:
25
Guido van Rossumf7cea101997-05-28 19:31:14 +000026 a) recursive objects (compound objects that, directly or indirectly,
Guido van Rossumcc6764c1995-02-09 17:18:10 +000027 contain a reference to themselves) may cause a recursive loop
28
Guido van Rossumf7cea101997-05-28 19:31:14 +000029 b) because deep copy copies *everything* it may copy too much, e.g.
Guido van Rossumcc6764c1995-02-09 17:18:10 +000030 administrative data structures that should be shared even between
31 copies
32
33Python's deep copy operation avoids these problems by:
34
Guido van Rossumf7cea101997-05-28 19:31:14 +000035 a) keeping a table of objects already copied during the current
36 copying pass
Guido van Rossumcc6764c1995-02-09 17:18:10 +000037
Guido van Rossumf7cea101997-05-28 19:31:14 +000038 b) letting user-defined classes override the copying operation or the
Guido van Rossumcc6764c1995-02-09 17:18:10 +000039 set of components copied
40
41This version does not copy types like module, class, function, method,
42nor stack trace, stack frame, nor file, socket, window, nor array, nor
43any similar types.
44
45Classes can use the same interfaces to control copying that they use
46to control pickling: they can define methods called __getinitargs__(),
Guido van Rossumc5d2d511997-12-07 16:18:22 +000047__getstate__() and __setstate__(). See the documentation for module
Guido van Rossumcc6764c1995-02-09 17:18:10 +000048"pickle" for information on these methods.
49"""
Guido van Rossum409780f1995-01-10 00:34:21 +000050
51import types
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +000052from copyreg import dispatch_table
Guido van Rossum409780f1995-01-10 00:34:21 +000053
Fred Drake227b1202000-08-17 05:06:49 +000054class Error(Exception):
Tim Peters88869f92001-01-14 23:36:06 +000055 pass
56error = Error # backward compatibility
Guido van Rossum409780f1995-01-10 00:34:21 +000057
Guido van Rossumf8baad02000-11-27 21:53:14 +000058try:
59 from org.python.core import PyStringMap
60except ImportError:
61 PyStringMap = None
62
Guido van Rossumc7557582003-02-06 19:53:22 +000063__all__ = ["Error", "copy", "deepcopy"]
Skip Montanaroe99d5ea2001-01-20 19:54:20 +000064
Guido van Rossum409780f1995-01-10 00:34:21 +000065def copy(x):
Tim Peters88869f92001-01-14 23:36:06 +000066 """Shallow copy operation on arbitrary Python objects.
Guido van Rossumcc6764c1995-02-09 17:18:10 +000067
Tim Peters88869f92001-01-14 23:36:06 +000068 See the module's __doc__ string for more info.
69 """
Guido van Rossumcc6764c1995-02-09 17:18:10 +000070
Guido van Rossumc06e3ac2003-02-07 17:30:18 +000071 cls = type(x)
72
73 copier = _copy_dispatch.get(cls)
74 if copier:
75 return copier(x)
76
77 copier = getattr(cls, "__copy__", None)
78 if copier:
79 return copier(x)
80
81 reductor = dispatch_table.get(cls)
Guido van Rossume6908832003-02-19 01:19:28 +000082 if reductor:
83 rv = reductor(x)
84 else:
85 reductor = getattr(x, "__reduce_ex__", None)
86 if reductor:
87 rv = reductor(2)
88 else:
89 reductor = getattr(x, "__reduce__", None)
90 if reductor:
91 rv = reductor()
92 else:
93 raise Error("un(shallow)copyable object of type %s" % cls)
Guido van Rossumc06e3ac2003-02-07 17:30:18 +000094
Guido van Rossume6908832003-02-19 01:19:28 +000095 return _reconstruct(x, rv, 0)
Tim Petersf2715e02003-02-19 02:35:07 +000096
Guido van Rossumc7557582003-02-06 19:53:22 +000097
Guido van Rossum409780f1995-01-10 00:34:21 +000098_copy_dispatch = d = {}
99
Raymond Hettingerf0e35692004-03-08 05:59:33 +0000100def _copy_immutable(x):
Tim Peters88869f92001-01-14 23:36:06 +0000101 return x
Neal Norwitzb69b2e52007-02-27 03:41:04 +0000102for t in (type(None), int, float, bool, str, tuple,
Guido van Rossum13257902007-06-07 23:15:56 +0000103 frozenset, type, range,
Christian Heimescc47b052008-03-25 14:56:36 +0000104 types.BuiltinFunctionType, type(Ellipsis),
Tim Petersd6e7e732006-02-26 04:21:50 +0000105 types.FunctionType):
Raymond Hettingerf0e35692004-03-08 05:59:33 +0000106 d[t] = _copy_immutable
Guido van Rossum13257902007-06-07 23:15:56 +0000107t = getattr(types, "CodeType", None)
108if t is not None:
109 d[t] = _copy_immutable
110for name in ("complex", "unicode"):
111 t = globals()['__builtins__'].get(name)
Raymond Hettingerf0e35692004-03-08 05:59:33 +0000112 if t is not None:
113 d[t] = _copy_immutable
Guido van Rossum409780f1995-01-10 00:34:21 +0000114
Raymond Hettingerf0e35692004-03-08 05:59:33 +0000115def _copy_with_constructor(x):
116 return type(x)(x)
117for t in (list, dict, set):
118 d[t] = _copy_with_constructor
Guido van Rossum409780f1995-01-10 00:34:21 +0000119
Raymond Hettingerf0e35692004-03-08 05:59:33 +0000120def _copy_with_copy_method(x):
Tim Peters88869f92001-01-14 23:36:06 +0000121 return x.copy()
Guido van Rossumf8baad02000-11-27 21:53:14 +0000122if PyStringMap is not None:
Raymond Hettingerf0e35692004-03-08 05:59:33 +0000123 d[PyStringMap] = _copy_with_copy_method
Guido van Rossum409780f1995-01-10 00:34:21 +0000124
Guido van Rossum409780f1995-01-10 00:34:21 +0000125del d
126
Guido van Rossumc06e3ac2003-02-07 17:30:18 +0000127def deepcopy(x, memo=None, _nil=[]):
Tim Peters88869f92001-01-14 23:36:06 +0000128 """Deep copy operation on arbitrary Python objects.
Guido van Rossumcc6764c1995-02-09 17:18:10 +0000129
Tim Peters88869f92001-01-14 23:36:06 +0000130 See the module's __doc__ string for more info.
131 """
Guido van Rossumcc6764c1995-02-09 17:18:10 +0000132
Tim Peters88869f92001-01-14 23:36:06 +0000133 if memo is None:
134 memo = {}
Guido van Rossumc06e3ac2003-02-07 17:30:18 +0000135
Tim Peters88869f92001-01-14 23:36:06 +0000136 d = id(x)
Guido van Rossumc06e3ac2003-02-07 17:30:18 +0000137 y = memo.get(d, _nil)
138 if y is not _nil:
139 return y
140
141 cls = type(x)
142
143 copier = _deepcopy_dispatch.get(cls)
144 if copier:
145 y = copier(x, memo)
146 else:
Tim Peters88869f92001-01-14 23:36:06 +0000147 try:
Guido van Rossumc06e3ac2003-02-07 17:30:18 +0000148 issc = issubclass(cls, type)
149 except TypeError: # cls is not a class (old Boost; see SF #502085)
Guido van Rossum11ade1d2002-06-10 21:10:27 +0000150 issc = 0
151 if issc:
Guido van Rossume6908832003-02-19 01:19:28 +0000152 y = _deepcopy_atomic(x, memo)
Guido van Rossum6cef6d52001-09-28 18:13:29 +0000153 else:
Guido van Rossume6908832003-02-19 01:19:28 +0000154 copier = getattr(x, "__deepcopy__", None)
155 if copier:
156 y = copier(memo)
157 else:
158 reductor = dispatch_table.get(cls)
159 if reductor:
160 rv = reductor(x)
161 else:
162 reductor = getattr(x, "__reduce_ex__", None)
163 if reductor:
164 rv = reductor(2)
165 else:
166 reductor = getattr(x, "__reduce__", None)
167 if reductor:
168 rv = reductor()
169 else:
170 raise Error(
171 "un(deep)copyable object of type %s" % cls)
172 y = _reconstruct(x, rv, 1, memo)
Guido van Rossumc06e3ac2003-02-07 17:30:18 +0000173
Tim Peters88869f92001-01-14 23:36:06 +0000174 memo[d] = y
Guido van Rossum61154602002-08-12 20:20:08 +0000175 _keep_alive(x, memo) # Make sure x lives at least as long as d
Tim Peters88869f92001-01-14 23:36:06 +0000176 return y
Guido van Rossum409780f1995-01-10 00:34:21 +0000177
178_deepcopy_dispatch = d = {}
179
180def _deepcopy_atomic(x, memo):
Tim Peters88869f92001-01-14 23:36:06 +0000181 return x
Raymond Hettingerf7153662005-02-07 14:16:21 +0000182d[type(None)] = _deepcopy_atomic
Christian Heimescc47b052008-03-25 14:56:36 +0000183d[type(Ellipsis)] = _deepcopy_atomic
Raymond Hettingerf7153662005-02-07 14:16:21 +0000184d[int] = _deepcopy_atomic
Raymond Hettingerf7153662005-02-07 14:16:21 +0000185d[float] = _deepcopy_atomic
186d[bool] = _deepcopy_atomic
Guido van Rossum8b9def32001-09-28 18:16:13 +0000187try:
Raymond Hettingerf7153662005-02-07 14:16:21 +0000188 d[complex] = _deepcopy_atomic
Martin v. Löwise2713be2005-03-08 15:03:08 +0000189except NameError:
Guido van Rossum8b9def32001-09-28 18:16:13 +0000190 pass
Guido van Rossum98297ee2007-11-06 21:34:58 +0000191d[bytes] = _deepcopy_atomic
Raymond Hettingerf7153662005-02-07 14:16:21 +0000192d[str] = _deepcopy_atomic
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000193try:
Guido van Rossum88b666c2002-02-28 23:19:52 +0000194 d[types.CodeType] = _deepcopy_atomic
195except AttributeError:
196 pass
Raymond Hettingerf7153662005-02-07 14:16:21 +0000197d[type] = _deepcopy_atomic
Guido van Rossum805365e2007-05-07 22:24:25 +0000198d[range] = _deepcopy_atomic
Martin v. Löwisba8f5ff2003-06-14 07:10:06 +0000199d[types.BuiltinFunctionType] = _deepcopy_atomic
Guido van Rossum1968ad32006-02-25 22:38:04 +0000200d[types.FunctionType] = _deepcopy_atomic
Guido van Rossum409780f1995-01-10 00:34:21 +0000201
202def _deepcopy_list(x, memo):
Tim Peters88869f92001-01-14 23:36:06 +0000203 y = []
204 memo[id(x)] = y
205 for a in x:
206 y.append(deepcopy(a, memo))
207 return y
Raymond Hettingerf7153662005-02-07 14:16:21 +0000208d[list] = _deepcopy_list
Guido van Rossum409780f1995-01-10 00:34:21 +0000209
210def _deepcopy_tuple(x, memo):
Tim Peters88869f92001-01-14 23:36:06 +0000211 y = []
212 for a in x:
213 y.append(deepcopy(a, memo))
214 d = id(x)
215 try:
216 return memo[d]
217 except KeyError:
218 pass
219 for i in range(len(x)):
220 if x[i] is not y[i]:
221 y = tuple(y)
222 break
223 else:
224 y = x
225 memo[d] = y
226 return y
Raymond Hettingerf7153662005-02-07 14:16:21 +0000227d[tuple] = _deepcopy_tuple
Guido van Rossum409780f1995-01-10 00:34:21 +0000228
229def _deepcopy_dict(x, memo):
Tim Peters88869f92001-01-14 23:36:06 +0000230 y = {}
231 memo[id(x)] = y
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000232 for key, value in x.items():
Raymond Hettingere0d49722002-06-02 18:55:56 +0000233 y[deepcopy(key, memo)] = deepcopy(value, memo)
Tim Peters88869f92001-01-14 23:36:06 +0000234 return y
Raymond Hettingerf7153662005-02-07 14:16:21 +0000235d[dict] = _deepcopy_dict
Guido van Rossumf8baad02000-11-27 21:53:14 +0000236if PyStringMap is not None:
237 d[PyStringMap] = _deepcopy_dict
Guido van Rossum409780f1995-01-10 00:34:21 +0000238
Guido van Rossum558be281997-08-20 22:26:19 +0000239def _keep_alive(x, memo):
Tim Peters88869f92001-01-14 23:36:06 +0000240 """Keeps a reference to the object x in the memo.
Guido van Rossum558be281997-08-20 22:26:19 +0000241
Tim Peters88869f92001-01-14 23:36:06 +0000242 Because we remember objects by their id, we have
243 to assure that possibly temporary objects are kept
244 alive by referencing them.
245 We store a reference at the id of the memo, which should
246 normally not be used unless someone tries to deepcopy
247 the memo itself...
248 """
249 try:
250 memo[id(memo)].append(x)
251 except KeyError:
252 # aha, this is the first one :-)
253 memo[id(memo)]=[x]
Guido van Rossum558be281997-08-20 22:26:19 +0000254
Guido van Rossum1e91c142001-12-28 21:33:22 +0000255def _reconstruct(x, info, deep, memo=None):
Guido van Rossum6cef6d52001-09-28 18:13:29 +0000256 if isinstance(info, str):
257 return x
258 assert isinstance(info, tuple)
Guido van Rossum1e91c142001-12-28 21:33:22 +0000259 if memo is None:
260 memo = {}
Guido van Rossum6cef6d52001-09-28 18:13:29 +0000261 n = len(info)
Guido van Rossum90e05b02003-02-06 18:18:23 +0000262 assert n in (2, 3, 4, 5)
Guido van Rossum6cef6d52001-09-28 18:13:29 +0000263 callable, args = info[:2]
264 if n > 2:
265 state = info[2]
266 else:
267 state = {}
Guido van Rossum90e05b02003-02-06 18:18:23 +0000268 if n > 3:
269 listiter = info[3]
270 else:
271 listiter = None
272 if n > 4:
273 dictiter = info[4]
274 else:
275 dictiter = None
Guido van Rossum6cef6d52001-09-28 18:13:29 +0000276 if deep:
Guido van Rossum1e91c142001-12-28 21:33:22 +0000277 args = deepcopy(args, memo)
Guido van Rossum6cef6d52001-09-28 18:13:29 +0000278 y = callable(*args)
Guido van Rossum99d2c252003-06-13 19:28:47 +0000279 memo[id(x)] = y
Guido van Rossum90e05b02003-02-06 18:18:23 +0000280 if listiter is not None:
281 for item in listiter:
282 if deep:
283 item = deepcopy(item, memo)
284 y.append(item)
285 if dictiter is not None:
286 for key, value in dictiter:
287 if deep:
288 key = deepcopy(key, memo)
289 value = deepcopy(value, memo)
290 y[key] = value
Guido van Rossum6cef6d52001-09-28 18:13:29 +0000291 if state:
292 if deep:
Guido van Rossum1e91c142001-12-28 21:33:22 +0000293 state = deepcopy(state, memo)
Guido van Rossum3e3583c2002-06-06 17:41:20 +0000294 if hasattr(y, '__setstate__'):
295 y.__setstate__(state)
296 else:
Guido van Rossumc7557582003-02-06 19:53:22 +0000297 if isinstance(state, tuple) and len(state) == 2:
298 state, slotstate = state
299 else:
300 slotstate = None
301 if state is not None:
302 y.__dict__.update(state)
303 if slotstate is not None:
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000304 for key, value in slotstate.items():
Guido van Rossumc7557582003-02-06 19:53:22 +0000305 setattr(y, key, value)
Guido van Rossum6cef6d52001-09-28 18:13:29 +0000306 return y
307
Guido van Rossum409780f1995-01-10 00:34:21 +0000308del d
309
310del types
311
Guido van Rossumc5d2d511997-12-07 16:18:22 +0000312# Helper for instance creation without calling __init__
313class _EmptyClass:
314 pass
315
Guido van Rossum409780f1995-01-10 00:34:21 +0000316def _test():
Guido van Rossume2a383d2007-01-15 16:59:06 +0000317 l = [None, 1, 2, 3.14, 'xyzzy', (1, 2), [3.14, 'abc'],
Tim Peters88869f92001-01-14 23:36:06 +0000318 {'abc': 'ABC'}, (), [], {}]
319 l1 = copy(l)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000320 print(l1==l)
Tim Peters88869f92001-01-14 23:36:06 +0000321 l1 = map(copy, l)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000322 print(l1==l)
Tim Peters88869f92001-01-14 23:36:06 +0000323 l1 = deepcopy(l)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000324 print(l1==l)
Tim Peters88869f92001-01-14 23:36:06 +0000325 class C:
326 def __init__(self, arg=None):
327 self.a = 1
328 self.arg = arg
329 if __name__ == '__main__':
330 import sys
331 file = sys.argv[0]
332 else:
333 file = __file__
334 self.fp = open(file)
335 self.fp.close()
336 def __getstate__(self):
337 return {'a': self.a, 'arg': self.arg}
338 def __setstate__(self, state):
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000339 for key, value in state.items():
Raymond Hettingere0d49722002-06-02 18:55:56 +0000340 setattr(self, key, value)
Guido van Rossumc06e3ac2003-02-07 17:30:18 +0000341 def __deepcopy__(self, memo=None):
Tim Peters88869f92001-01-14 23:36:06 +0000342 new = self.__class__(deepcopy(self.arg, memo))
343 new.a = self.a
344 return new
345 c = C('argument sketch')
346 l.append(c)
347 l2 = copy(l)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000348 print(l == l2)
349 print(l)
350 print(l2)
Tim Peters88869f92001-01-14 23:36:06 +0000351 l2 = deepcopy(l)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000352 print(l == l2)
353 print(l)
354 print(l2)
Tim Peters88869f92001-01-14 23:36:06 +0000355 l.append({l[1]: l, 'xyz': l[2]})
356 l3 = copy(l)
Alexandre Vassalotti1f2ba4b2008-05-16 07:12:44 +0000357 import reprlib
358 print(map(reprlib.repr, l))
359 print(map(reprlib.repr, l1))
360 print(map(reprlib.repr, l2))
361 print(map(reprlib.repr, l3))
Tim Peters88869f92001-01-14 23:36:06 +0000362 l3 = deepcopy(l)
Alexandre Vassalotti1f2ba4b2008-05-16 07:12:44 +0000363 print(map(reprlib.repr, l))
364 print(map(reprlib.repr, l1))
365 print(map(reprlib.repr, l2))
366 print(map(reprlib.repr, l3))
Guido van Rossum409780f1995-01-10 00:34:21 +0000367
368if __name__ == '__main__':
Tim Peters88869f92001-01-14 23:36:06 +0000369 _test()