blob: 789488c1bc51e3782f8a5af2fdb944664b2edee0 [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
Antoine Pitrou6e610062009-05-15 17:04:50 +000052import weakref
Alexandre Vassalottif7fa63d2008-05-11 08:55:36 +000053from copyreg import dispatch_table
Guido van Rossum409780f1995-01-10 00:34:21 +000054
Fred Drake227b1202000-08-17 05:06:49 +000055class Error(Exception):
Tim Peters88869f92001-01-14 23:36:06 +000056 pass
57error = Error # backward compatibility
Guido van Rossum409780f1995-01-10 00:34:21 +000058
Guido van Rossumf8baad02000-11-27 21:53:14 +000059try:
60 from org.python.core import PyStringMap
61except ImportError:
62 PyStringMap = None
63
Guido van Rossumc7557582003-02-06 19:53:22 +000064__all__ = ["Error", "copy", "deepcopy"]
Skip Montanaroe99d5ea2001-01-20 19:54:20 +000065
Guido van Rossum409780f1995-01-10 00:34:21 +000066def copy(x):
Tim Peters88869f92001-01-14 23:36:06 +000067 """Shallow copy operation on arbitrary Python objects.
Guido van Rossumcc6764c1995-02-09 17:18:10 +000068
Tim Peters88869f92001-01-14 23:36:06 +000069 See the module's __doc__ string for more info.
70 """
Guido van Rossumcc6764c1995-02-09 17:18:10 +000071
Guido van Rossumc06e3ac2003-02-07 17:30:18 +000072 cls = type(x)
73
74 copier = _copy_dispatch.get(cls)
75 if copier:
76 return copier(x)
77
78 copier = getattr(cls, "__copy__", None)
79 if copier:
80 return copier(x)
81
82 reductor = dispatch_table.get(cls)
Guido van Rossume6908832003-02-19 01:19:28 +000083 if reductor:
84 rv = reductor(x)
85 else:
86 reductor = getattr(x, "__reduce_ex__", None)
87 if reductor:
88 rv = reductor(2)
89 else:
90 reductor = getattr(x, "__reduce__", None)
91 if reductor:
92 rv = reductor()
93 else:
94 raise Error("un(shallow)copyable object of type %s" % cls)
Guido van Rossumc06e3ac2003-02-07 17:30:18 +000095
Guido van Rossume6908832003-02-19 01:19:28 +000096 return _reconstruct(x, rv, 0)
Tim Petersf2715e02003-02-19 02:35:07 +000097
Guido van Rossumc7557582003-02-06 19:53:22 +000098
Guido van Rossum409780f1995-01-10 00:34:21 +000099_copy_dispatch = d = {}
100
Raymond Hettingerf0e35692004-03-08 05:59:33 +0000101def _copy_immutable(x):
Tim Peters88869f92001-01-14 23:36:06 +0000102 return x
Neal Norwitzb69b2e52007-02-27 03:41:04 +0000103for t in (type(None), int, float, bool, str, tuple,
Guido van Rossum13257902007-06-07 23:15:56 +0000104 frozenset, type, range,
Christian Heimescc47b052008-03-25 14:56:36 +0000105 types.BuiltinFunctionType, type(Ellipsis),
Antoine Pitrou6e610062009-05-15 17:04:50 +0000106 types.FunctionType, weakref.ref):
Raymond Hettingerf0e35692004-03-08 05:59:33 +0000107 d[t] = _copy_immutable
Guido van Rossum13257902007-06-07 23:15:56 +0000108t = getattr(types, "CodeType", None)
109if t is not None:
110 d[t] = _copy_immutable
111for name in ("complex", "unicode"):
112 t = globals()['__builtins__'].get(name)
Raymond Hettingerf0e35692004-03-08 05:59:33 +0000113 if t is not None:
114 d[t] = _copy_immutable
Guido van Rossum409780f1995-01-10 00:34:21 +0000115
Raymond Hettingerf0e35692004-03-08 05:59:33 +0000116def _copy_with_constructor(x):
117 return type(x)(x)
118for t in (list, dict, set):
119 d[t] = _copy_with_constructor
Guido van Rossum409780f1995-01-10 00:34:21 +0000120
Raymond Hettingerf0e35692004-03-08 05:59:33 +0000121def _copy_with_copy_method(x):
Tim Peters88869f92001-01-14 23:36:06 +0000122 return x.copy()
Guido van Rossumf8baad02000-11-27 21:53:14 +0000123if PyStringMap is not None:
Raymond Hettingerf0e35692004-03-08 05:59:33 +0000124 d[PyStringMap] = _copy_with_copy_method
Guido van Rossum409780f1995-01-10 00:34:21 +0000125
Guido van Rossum409780f1995-01-10 00:34:21 +0000126del d
127
Guido van Rossumc06e3ac2003-02-07 17:30:18 +0000128def deepcopy(x, memo=None, _nil=[]):
Tim Peters88869f92001-01-14 23:36:06 +0000129 """Deep copy operation on arbitrary Python objects.
Guido van Rossumcc6764c1995-02-09 17:18:10 +0000130
Tim Peters88869f92001-01-14 23:36:06 +0000131 See the module's __doc__ string for more info.
132 """
Guido van Rossumcc6764c1995-02-09 17:18:10 +0000133
Tim Peters88869f92001-01-14 23:36:06 +0000134 if memo is None:
135 memo = {}
Guido van Rossumc06e3ac2003-02-07 17:30:18 +0000136
Tim Peters88869f92001-01-14 23:36:06 +0000137 d = id(x)
Guido van Rossumc06e3ac2003-02-07 17:30:18 +0000138 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)
145 if copier:
146 y = copier(x, memo)
147 else:
Tim Peters88869f92001-01-14 23:36:06 +0000148 try:
Guido van Rossumc06e3ac2003-02-07 17:30:18 +0000149 issc = issubclass(cls, type)
150 except TypeError: # cls is not a class (old Boost; see SF #502085)
Guido van Rossum11ade1d2002-06-10 21:10:27 +0000151 issc = 0
152 if issc:
Guido van Rossume6908832003-02-19 01:19:28 +0000153 y = _deepcopy_atomic(x, memo)
Guido van Rossum6cef6d52001-09-28 18:13:29 +0000154 else:
Guido van Rossume6908832003-02-19 01:19:28 +0000155 copier = getattr(x, "__deepcopy__", None)
156 if copier:
157 y = copier(memo)
158 else:
159 reductor = dispatch_table.get(cls)
160 if reductor:
161 rv = reductor(x)
162 else:
163 reductor = getattr(x, "__reduce_ex__", None)
164 if reductor:
165 rv = reductor(2)
166 else:
167 reductor = getattr(x, "__reduce__", None)
168 if reductor:
169 rv = reductor()
170 else:
171 raise Error(
172 "un(deep)copyable object of type %s" % cls)
173 y = _reconstruct(x, rv, 1, memo)
Guido van Rossumc06e3ac2003-02-07 17:30:18 +0000174
Tim Peters88869f92001-01-14 23:36:06 +0000175 memo[d] = y
Guido van Rossum61154602002-08-12 20:20:08 +0000176 _keep_alive(x, memo) # Make sure x lives at least as long as d
Tim Peters88869f92001-01-14 23:36:06 +0000177 return y
Guido van Rossum409780f1995-01-10 00:34:21 +0000178
179_deepcopy_dispatch = d = {}
180
181def _deepcopy_atomic(x, memo):
Tim Peters88869f92001-01-14 23:36:06 +0000182 return x
Raymond Hettingerf7153662005-02-07 14:16:21 +0000183d[type(None)] = _deepcopy_atomic
Christian Heimescc47b052008-03-25 14:56:36 +0000184d[type(Ellipsis)] = _deepcopy_atomic
Raymond Hettingerf7153662005-02-07 14:16:21 +0000185d[int] = _deepcopy_atomic
Raymond Hettingerf7153662005-02-07 14:16:21 +0000186d[float] = _deepcopy_atomic
187d[bool] = _deepcopy_atomic
Guido van Rossum8b9def32001-09-28 18:16:13 +0000188try:
Raymond Hettingerf7153662005-02-07 14:16:21 +0000189 d[complex] = _deepcopy_atomic
Martin v. Löwise2713be2005-03-08 15:03:08 +0000190except NameError:
Guido van Rossum8b9def32001-09-28 18:16:13 +0000191 pass
Guido van Rossum98297ee2007-11-06 21:34:58 +0000192d[bytes] = _deepcopy_atomic
Raymond Hettingerf7153662005-02-07 14:16:21 +0000193d[str] = _deepcopy_atomic
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000194try:
Guido van Rossum88b666c2002-02-28 23:19:52 +0000195 d[types.CodeType] = _deepcopy_atomic
196except AttributeError:
197 pass
Raymond Hettingerf7153662005-02-07 14:16:21 +0000198d[type] = _deepcopy_atomic
Guido van Rossum805365e2007-05-07 22:24:25 +0000199d[range] = _deepcopy_atomic
Martin v. Löwisba8f5ff2003-06-14 07:10:06 +0000200d[types.BuiltinFunctionType] = _deepcopy_atomic
Guido van Rossum1968ad32006-02-25 22:38:04 +0000201d[types.FunctionType] = _deepcopy_atomic
Antoine Pitrou6e610062009-05-15 17:04:50 +0000202d[weakref.ref] = _deepcopy_atomic
Guido van Rossum409780f1995-01-10 00:34:21 +0000203
204def _deepcopy_list(x, memo):
Tim Peters88869f92001-01-14 23:36:06 +0000205 y = []
206 memo[id(x)] = y
207 for a in x:
208 y.append(deepcopy(a, memo))
209 return y
Raymond Hettingerf7153662005-02-07 14:16:21 +0000210d[list] = _deepcopy_list
Guido van Rossum409780f1995-01-10 00:34:21 +0000211
212def _deepcopy_tuple(x, memo):
Tim Peters88869f92001-01-14 23:36:06 +0000213 y = []
214 for a in x:
215 y.append(deepcopy(a, memo))
216 d = id(x)
217 try:
218 return memo[d]
219 except KeyError:
220 pass
221 for i in range(len(x)):
222 if x[i] is not y[i]:
223 y = tuple(y)
224 break
225 else:
226 y = x
227 memo[d] = y
228 return y
Raymond Hettingerf7153662005-02-07 14:16:21 +0000229d[tuple] = _deepcopy_tuple
Guido van Rossum409780f1995-01-10 00:34:21 +0000230
231def _deepcopy_dict(x, memo):
Tim Peters88869f92001-01-14 23:36:06 +0000232 y = {}
233 memo[id(x)] = y
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000234 for key, value in x.items():
Raymond Hettingere0d49722002-06-02 18:55:56 +0000235 y[deepcopy(key, memo)] = deepcopy(value, memo)
Tim Peters88869f92001-01-14 23:36:06 +0000236 return y
Raymond Hettingerf7153662005-02-07 14:16:21 +0000237d[dict] = _deepcopy_dict
Guido van Rossumf8baad02000-11-27 21:53:14 +0000238if PyStringMap is not None:
239 d[PyStringMap] = _deepcopy_dict
Guido van Rossum409780f1995-01-10 00:34:21 +0000240
Antoine Pitrou1fc0d2b2009-11-28 15:58:27 +0000241def _deepcopy_method(x, memo): # Copy instance methods
242 return type(x)(x.__func__, deepcopy(x.__self__, memo))
243_deepcopy_dispatch[types.MethodType] = _deepcopy_method
244
Guido van Rossum558be281997-08-20 22:26:19 +0000245def _keep_alive(x, memo):
Tim Peters88869f92001-01-14 23:36:06 +0000246 """Keeps a reference to the object x in the memo.
Guido van Rossum558be281997-08-20 22:26:19 +0000247
Tim Peters88869f92001-01-14 23:36:06 +0000248 Because we remember objects by their id, we have
249 to assure that possibly temporary objects are kept
250 alive by referencing them.
251 We store a reference at the id of the memo, which should
252 normally not be used unless someone tries to deepcopy
253 the memo itself...
254 """
255 try:
256 memo[id(memo)].append(x)
257 except KeyError:
258 # aha, this is the first one :-)
259 memo[id(memo)]=[x]
Guido van Rossum558be281997-08-20 22:26:19 +0000260
Guido van Rossum1e91c142001-12-28 21:33:22 +0000261def _reconstruct(x, info, deep, memo=None):
Guido van Rossum6cef6d52001-09-28 18:13:29 +0000262 if isinstance(info, str):
263 return x
264 assert isinstance(info, tuple)
Guido van Rossum1e91c142001-12-28 21:33:22 +0000265 if memo is None:
266 memo = {}
Guido van Rossum6cef6d52001-09-28 18:13:29 +0000267 n = len(info)
Guido van Rossum90e05b02003-02-06 18:18:23 +0000268 assert n in (2, 3, 4, 5)
Guido van Rossum6cef6d52001-09-28 18:13:29 +0000269 callable, args = info[:2]
270 if n > 2:
271 state = info[2]
272 else:
273 state = {}
Guido van Rossum90e05b02003-02-06 18:18:23 +0000274 if n > 3:
275 listiter = info[3]
276 else:
277 listiter = None
278 if n > 4:
279 dictiter = info[4]
280 else:
281 dictiter = None
Guido van Rossum6cef6d52001-09-28 18:13:29 +0000282 if deep:
Guido van Rossum1e91c142001-12-28 21:33:22 +0000283 args = deepcopy(args, memo)
Guido van Rossum6cef6d52001-09-28 18:13:29 +0000284 y = callable(*args)
Guido van Rossum99d2c252003-06-13 19:28:47 +0000285 memo[id(x)] = y
Guido van Rossum90e05b02003-02-06 18:18:23 +0000286 if listiter is not None:
287 for item in listiter:
288 if deep:
289 item = deepcopy(item, memo)
290 y.append(item)
291 if dictiter is not None:
292 for key, value in dictiter:
293 if deep:
294 key = deepcopy(key, memo)
295 value = deepcopy(value, memo)
296 y[key] = value
Guido van Rossum6cef6d52001-09-28 18:13:29 +0000297 if state:
298 if deep:
Guido van Rossum1e91c142001-12-28 21:33:22 +0000299 state = deepcopy(state, memo)
Guido van Rossum3e3583c2002-06-06 17:41:20 +0000300 if hasattr(y, '__setstate__'):
301 y.__setstate__(state)
302 else:
Guido van Rossumc7557582003-02-06 19:53:22 +0000303 if isinstance(state, tuple) and len(state) == 2:
304 state, slotstate = state
305 else:
306 slotstate = None
307 if state is not None:
308 y.__dict__.update(state)
309 if slotstate is not None:
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000310 for key, value in slotstate.items():
Guido van Rossumc7557582003-02-06 19:53:22 +0000311 setattr(y, key, value)
Guido van Rossum6cef6d52001-09-28 18:13:29 +0000312 return y
313
Guido van Rossum409780f1995-01-10 00:34:21 +0000314del d
315
316del types
317
Guido van Rossumc5d2d511997-12-07 16:18:22 +0000318# Helper for instance creation without calling __init__
319class _EmptyClass:
320 pass
321
Guido van Rossum409780f1995-01-10 00:34:21 +0000322def _test():
Guido van Rossume2a383d2007-01-15 16:59:06 +0000323 l = [None, 1, 2, 3.14, 'xyzzy', (1, 2), [3.14, 'abc'],
Tim Peters88869f92001-01-14 23:36:06 +0000324 {'abc': 'ABC'}, (), [], {}]
325 l1 = copy(l)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000326 print(l1==l)
Tim Peters88869f92001-01-14 23:36:06 +0000327 l1 = map(copy, l)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000328 print(l1==l)
Tim Peters88869f92001-01-14 23:36:06 +0000329 l1 = deepcopy(l)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000330 print(l1==l)
Tim Peters88869f92001-01-14 23:36:06 +0000331 class C:
332 def __init__(self, arg=None):
333 self.a = 1
334 self.arg = arg
335 if __name__ == '__main__':
336 import sys
337 file = sys.argv[0]
338 else:
339 file = __file__
340 self.fp = open(file)
341 self.fp.close()
342 def __getstate__(self):
343 return {'a': self.a, 'arg': self.arg}
344 def __setstate__(self, state):
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000345 for key, value in state.items():
Raymond Hettingere0d49722002-06-02 18:55:56 +0000346 setattr(self, key, value)
Guido van Rossumc06e3ac2003-02-07 17:30:18 +0000347 def __deepcopy__(self, memo=None):
Tim Peters88869f92001-01-14 23:36:06 +0000348 new = self.__class__(deepcopy(self.arg, memo))
349 new.a = self.a
350 return new
351 c = C('argument sketch')
352 l.append(c)
353 l2 = copy(l)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000354 print(l == l2)
355 print(l)
356 print(l2)
Tim Peters88869f92001-01-14 23:36:06 +0000357 l2 = deepcopy(l)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000358 print(l == l2)
359 print(l)
360 print(l2)
Tim Peters88869f92001-01-14 23:36:06 +0000361 l.append({l[1]: l, 'xyz': l[2]})
362 l3 = copy(l)
Alexandre Vassalotti1f2ba4b2008-05-16 07:12:44 +0000363 import reprlib
364 print(map(reprlib.repr, l))
365 print(map(reprlib.repr, l1))
366 print(map(reprlib.repr, l2))
367 print(map(reprlib.repr, l3))
Tim Peters88869f92001-01-14 23:36:06 +0000368 l3 = deepcopy(l)
Alexandre Vassalotti1f2ba4b2008-05-16 07:12:44 +0000369 print(map(reprlib.repr, l))
370 print(map(reprlib.repr, l1))
371 print(map(reprlib.repr, l2))
372 print(map(reprlib.repr, l3))
Guido van Rossum409780f1995-01-10 00:34:21 +0000373
374if __name__ == '__main__':
Tim Peters88869f92001-01-14 23:36:06 +0000375 _test()