blob: 3a45fdf49b23788eea55051f9e35c55a3643f4e0 [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
Antoine Pitroud6399d22010-09-04 17:46:44 +000054import builtins
Guido van Rossum409780f1995-01-10 00:34:21 +000055
Fred Drake227b1202000-08-17 05:06:49 +000056class Error(Exception):
Tim Peters88869f92001-01-14 23:36:06 +000057 pass
58error = Error # backward compatibility
Guido van Rossum409780f1995-01-10 00:34:21 +000059
Guido van Rossumf8baad02000-11-27 21:53:14 +000060try:
61 from org.python.core import PyStringMap
62except ImportError:
63 PyStringMap = None
64
Guido van Rossumc7557582003-02-06 19:53:22 +000065__all__ = ["Error", "copy", "deepcopy"]
Skip Montanaroe99d5ea2001-01-20 19:54:20 +000066
Guido van Rossum409780f1995-01-10 00:34:21 +000067def copy(x):
Tim Peters88869f92001-01-14 23:36:06 +000068 """Shallow copy operation on arbitrary Python objects.
Guido van Rossumcc6764c1995-02-09 17:18:10 +000069
Tim Peters88869f92001-01-14 23:36:06 +000070 See the module's __doc__ string for more info.
71 """
Guido van Rossumcc6764c1995-02-09 17:18:10 +000072
Guido van Rossumc06e3ac2003-02-07 17:30:18 +000073 cls = type(x)
74
75 copier = _copy_dispatch.get(cls)
76 if copier:
77 return copier(x)
78
Alexandre Vassalotti5c1c3b42013-12-01 13:25:26 -080079 try:
80 issc = issubclass(cls, type)
81 except TypeError: # cls is not a class
82 issc = False
83 if issc:
84 # treat it as a regular class:
85 return _copy_immutable(x)
86
Guido van Rossumc06e3ac2003-02-07 17:30:18 +000087 copier = getattr(cls, "__copy__", None)
88 if copier:
89 return copier(x)
90
91 reductor = dispatch_table.get(cls)
Guido van Rossume6908832003-02-19 01:19:28 +000092 if reductor:
93 rv = reductor(x)
94 else:
95 reductor = getattr(x, "__reduce_ex__", None)
96 if reductor:
Serhiy Storchaka32af7542015-03-24 18:06:42 +020097 rv = reductor(4)
Guido van Rossume6908832003-02-19 01:19:28 +000098 else:
99 reductor = getattr(x, "__reduce__", None)
100 if reductor:
101 rv = reductor()
102 else:
103 raise Error("un(shallow)copyable object of type %s" % cls)
Guido van Rossumc06e3ac2003-02-07 17:30:18 +0000104
Guido van Rossume6908832003-02-19 01:19:28 +0000105 return _reconstruct(x, rv, 0)
Tim Petersf2715e02003-02-19 02:35:07 +0000106
Guido van Rossumc7557582003-02-06 19:53:22 +0000107
Guido van Rossum409780f1995-01-10 00:34:21 +0000108_copy_dispatch = d = {}
109
Raymond Hettingerf0e35692004-03-08 05:59:33 +0000110def _copy_immutable(x):
Tim Peters88869f92001-01-14 23:36:06 +0000111 return x
Neal Norwitzb69b2e52007-02-27 03:41:04 +0000112for t in (type(None), int, float, bool, str, tuple,
Antoine Pitroudc9215f2014-02-27 22:14:31 +0100113 bytes, frozenset, type, range,
Christian Heimescc47b052008-03-25 14:56:36 +0000114 types.BuiltinFunctionType, type(Ellipsis),
Antoine Pitrou6e610062009-05-15 17:04:50 +0000115 types.FunctionType, weakref.ref):
Raymond Hettingerf0e35692004-03-08 05:59:33 +0000116 d[t] = _copy_immutable
Guido van Rossum13257902007-06-07 23:15:56 +0000117t = getattr(types, "CodeType", None)
118if t is not None:
119 d[t] = _copy_immutable
120for name in ("complex", "unicode"):
Antoine Pitroud6399d22010-09-04 17:46:44 +0000121 t = getattr(builtins, name, None)
Raymond Hettingerf0e35692004-03-08 05:59:33 +0000122 if t is not None:
123 d[t] = _copy_immutable
Guido van Rossum409780f1995-01-10 00:34:21 +0000124
Raymond Hettingerf0e35692004-03-08 05:59:33 +0000125def _copy_with_constructor(x):
126 return type(x)(x)
127for t in (list, dict, set):
128 d[t] = _copy_with_constructor
Guido van Rossum409780f1995-01-10 00:34:21 +0000129
Raymond Hettingerf0e35692004-03-08 05:59:33 +0000130def _copy_with_copy_method(x):
Tim Peters88869f92001-01-14 23:36:06 +0000131 return x.copy()
Guido van Rossumf8baad02000-11-27 21:53:14 +0000132if PyStringMap is not None:
Raymond Hettingerf0e35692004-03-08 05:59:33 +0000133 d[PyStringMap] = _copy_with_copy_method
Guido van Rossum409780f1995-01-10 00:34:21 +0000134
Guido van Rossum409780f1995-01-10 00:34:21 +0000135del d
136
Guido van Rossumc06e3ac2003-02-07 17:30:18 +0000137def deepcopy(x, memo=None, _nil=[]):
Tim Peters88869f92001-01-14 23:36:06 +0000138 """Deep copy operation on arbitrary Python objects.
Guido van Rossumcc6764c1995-02-09 17:18:10 +0000139
Tim Peters88869f92001-01-14 23:36:06 +0000140 See the module's __doc__ string for more info.
141 """
Guido van Rossumcc6764c1995-02-09 17:18:10 +0000142
Tim Peters88869f92001-01-14 23:36:06 +0000143 if memo is None:
144 memo = {}
Guido van Rossumc06e3ac2003-02-07 17:30:18 +0000145
Tim Peters88869f92001-01-14 23:36:06 +0000146 d = id(x)
Guido van Rossumc06e3ac2003-02-07 17:30:18 +0000147 y = memo.get(d, _nil)
148 if y is not _nil:
149 return y
150
151 cls = type(x)
152
153 copier = _deepcopy_dispatch.get(cls)
154 if copier:
155 y = copier(x, memo)
156 else:
Tim Peters88869f92001-01-14 23:36:06 +0000157 try:
Guido van Rossumc06e3ac2003-02-07 17:30:18 +0000158 issc = issubclass(cls, type)
159 except TypeError: # cls is not a class (old Boost; see SF #502085)
Guido van Rossum11ade1d2002-06-10 21:10:27 +0000160 issc = 0
161 if issc:
Guido van Rossume6908832003-02-19 01:19:28 +0000162 y = _deepcopy_atomic(x, memo)
Guido van Rossum6cef6d52001-09-28 18:13:29 +0000163 else:
Guido van Rossume6908832003-02-19 01:19:28 +0000164 copier = getattr(x, "__deepcopy__", None)
165 if copier:
166 y = copier(memo)
167 else:
168 reductor = dispatch_table.get(cls)
169 if reductor:
170 rv = reductor(x)
171 else:
172 reductor = getattr(x, "__reduce_ex__", None)
173 if reductor:
Serhiy Storchaka32af7542015-03-24 18:06:42 +0200174 rv = reductor(4)
Guido van Rossume6908832003-02-19 01:19:28 +0000175 else:
176 reductor = getattr(x, "__reduce__", None)
177 if reductor:
178 rv = reductor()
179 else:
180 raise Error(
181 "un(deep)copyable object of type %s" % cls)
182 y = _reconstruct(x, rv, 1, memo)
Guido van Rossumc06e3ac2003-02-07 17:30:18 +0000183
Benjamin Petersone90ec362011-06-27 16:22:46 -0500184 # If is its own copy, don't memoize.
185 if y is not x:
186 memo[d] = y
187 _keep_alive(x, memo) # Make sure x lives at least as long as d
Tim Peters88869f92001-01-14 23:36:06 +0000188 return y
Guido van Rossum409780f1995-01-10 00:34:21 +0000189
190_deepcopy_dispatch = d = {}
191
192def _deepcopy_atomic(x, memo):
Tim Peters88869f92001-01-14 23:36:06 +0000193 return x
Raymond Hettingerf7153662005-02-07 14:16:21 +0000194d[type(None)] = _deepcopy_atomic
Christian Heimescc47b052008-03-25 14:56:36 +0000195d[type(Ellipsis)] = _deepcopy_atomic
Raymond Hettingerf7153662005-02-07 14:16:21 +0000196d[int] = _deepcopy_atomic
Raymond Hettingerf7153662005-02-07 14:16:21 +0000197d[float] = _deepcopy_atomic
198d[bool] = _deepcopy_atomic
Guido van Rossum8b9def32001-09-28 18:16:13 +0000199try:
Raymond Hettingerf7153662005-02-07 14:16:21 +0000200 d[complex] = _deepcopy_atomic
Martin v. Löwise2713be2005-03-08 15:03:08 +0000201except NameError:
Guido van Rossum8b9def32001-09-28 18:16:13 +0000202 pass
Guido van Rossum98297ee2007-11-06 21:34:58 +0000203d[bytes] = _deepcopy_atomic
Raymond Hettingerf7153662005-02-07 14:16:21 +0000204d[str] = _deepcopy_atomic
Martin v. Löwis339d0f72001-08-17 18:39:25 +0000205try:
Guido van Rossum88b666c2002-02-28 23:19:52 +0000206 d[types.CodeType] = _deepcopy_atomic
207except AttributeError:
208 pass
Raymond Hettingerf7153662005-02-07 14:16:21 +0000209d[type] = _deepcopy_atomic
Guido van Rossum805365e2007-05-07 22:24:25 +0000210d[range] = _deepcopy_atomic
Martin v. Löwisba8f5ff2003-06-14 07:10:06 +0000211d[types.BuiltinFunctionType] = _deepcopy_atomic
Guido van Rossum1968ad32006-02-25 22:38:04 +0000212d[types.FunctionType] = _deepcopy_atomic
Antoine Pitrou6e610062009-05-15 17:04:50 +0000213d[weakref.ref] = _deepcopy_atomic
Guido van Rossum409780f1995-01-10 00:34:21 +0000214
215def _deepcopy_list(x, memo):
Tim Peters88869f92001-01-14 23:36:06 +0000216 y = []
217 memo[id(x)] = y
218 for a in x:
219 y.append(deepcopy(a, memo))
220 return y
Raymond Hettingerf7153662005-02-07 14:16:21 +0000221d[list] = _deepcopy_list
Guido van Rossum409780f1995-01-10 00:34:21 +0000222
223def _deepcopy_tuple(x, memo):
Benjamin Peterson4ce5f3f2014-05-03 20:22:00 -0400224 y = [deepcopy(a, memo) for a in x]
Benjamin Petersone90ec362011-06-27 16:22:46 -0500225 # We're not going to put the tuple in the memo, but it's still important we
226 # check for it, in case the tuple contains recursive mutable structures.
Tim Peters88869f92001-01-14 23:36:06 +0000227 try:
Benjamin Petersone90ec362011-06-27 16:22:46 -0500228 return memo[id(x)]
Tim Peters88869f92001-01-14 23:36:06 +0000229 except KeyError:
230 pass
Benjamin Peterson4ce5f3f2014-05-03 20:22:00 -0400231 for k, j in zip(x, y):
232 if k is not j:
Tim Peters88869f92001-01-14 23:36:06 +0000233 y = tuple(y)
234 break
235 else:
236 y = x
Tim Peters88869f92001-01-14 23:36:06 +0000237 return y
Raymond Hettingerf7153662005-02-07 14:16:21 +0000238d[tuple] = _deepcopy_tuple
Guido van Rossum409780f1995-01-10 00:34:21 +0000239
240def _deepcopy_dict(x, memo):
Tim Peters88869f92001-01-14 23:36:06 +0000241 y = {}
242 memo[id(x)] = y
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000243 for key, value in x.items():
Raymond Hettingere0d49722002-06-02 18:55:56 +0000244 y[deepcopy(key, memo)] = deepcopy(value, memo)
Tim Peters88869f92001-01-14 23:36:06 +0000245 return y
Raymond Hettingerf7153662005-02-07 14:16:21 +0000246d[dict] = _deepcopy_dict
Guido van Rossumf8baad02000-11-27 21:53:14 +0000247if PyStringMap is not None:
248 d[PyStringMap] = _deepcopy_dict
Guido van Rossum409780f1995-01-10 00:34:21 +0000249
Antoine Pitrou1fc0d2b2009-11-28 15:58:27 +0000250def _deepcopy_method(x, memo): # Copy instance methods
251 return type(x)(x.__func__, deepcopy(x.__self__, memo))
252_deepcopy_dispatch[types.MethodType] = _deepcopy_method
253
Guido van Rossum558be281997-08-20 22:26:19 +0000254def _keep_alive(x, memo):
Tim Peters88869f92001-01-14 23:36:06 +0000255 """Keeps a reference to the object x in the memo.
Guido van Rossum558be281997-08-20 22:26:19 +0000256
Tim Peters88869f92001-01-14 23:36:06 +0000257 Because we remember objects by their id, we have
258 to assure that possibly temporary objects are kept
259 alive by referencing them.
260 We store a reference at the id of the memo, which should
261 normally not be used unless someone tries to deepcopy
262 the memo itself...
263 """
264 try:
265 memo[id(memo)].append(x)
266 except KeyError:
267 # aha, this is the first one :-)
268 memo[id(memo)]=[x]
Guido van Rossum558be281997-08-20 22:26:19 +0000269
Guido van Rossum1e91c142001-12-28 21:33:22 +0000270def _reconstruct(x, info, deep, memo=None):
Guido van Rossum6cef6d52001-09-28 18:13:29 +0000271 if isinstance(info, str):
272 return x
273 assert isinstance(info, tuple)
Guido van Rossum1e91c142001-12-28 21:33:22 +0000274 if memo is None:
275 memo = {}
Guido van Rossum6cef6d52001-09-28 18:13:29 +0000276 n = len(info)
Guido van Rossum90e05b02003-02-06 18:18:23 +0000277 assert n in (2, 3, 4, 5)
Guido van Rossum6cef6d52001-09-28 18:13:29 +0000278 callable, args = info[:2]
279 if n > 2:
280 state = info[2]
281 else:
282 state = {}
Guido van Rossum90e05b02003-02-06 18:18:23 +0000283 if n > 3:
284 listiter = info[3]
285 else:
286 listiter = None
287 if n > 4:
288 dictiter = info[4]
289 else:
290 dictiter = None
Guido van Rossum6cef6d52001-09-28 18:13:29 +0000291 if deep:
Guido van Rossum1e91c142001-12-28 21:33:22 +0000292 args = deepcopy(args, memo)
Guido van Rossum6cef6d52001-09-28 18:13:29 +0000293 y = callable(*args)
Guido van Rossum99d2c252003-06-13 19:28:47 +0000294 memo[id(x)] = y
Antoine Pitrou3941a8f2010-09-04 17:40:21 +0000295
Guido van Rossum6cef6d52001-09-28 18:13:29 +0000296 if state:
297 if deep:
Guido van Rossum1e91c142001-12-28 21:33:22 +0000298 state = deepcopy(state, memo)
Guido van Rossum3e3583c2002-06-06 17:41:20 +0000299 if hasattr(y, '__setstate__'):
300 y.__setstate__(state)
301 else:
Guido van Rossumc7557582003-02-06 19:53:22 +0000302 if isinstance(state, tuple) and len(state) == 2:
303 state, slotstate = state
304 else:
305 slotstate = None
306 if state is not None:
307 y.__dict__.update(state)
308 if slotstate is not None:
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000309 for key, value in slotstate.items():
Guido van Rossumc7557582003-02-06 19:53:22 +0000310 setattr(y, key, value)
Antoine Pitrou3941a8f2010-09-04 17:40:21 +0000311
312 if listiter is not None:
313 for item in listiter:
314 if deep:
315 item = deepcopy(item, memo)
316 y.append(item)
317 if dictiter is not None:
318 for key, value in dictiter:
319 if deep:
320 key = deepcopy(key, memo)
321 value = deepcopy(value, memo)
322 y[key] = value
Guido van Rossum6cef6d52001-09-28 18:13:29 +0000323 return y
324
Guido van Rossum409780f1995-01-10 00:34:21 +0000325del d
326
327del types
328
Guido van Rossumc5d2d511997-12-07 16:18:22 +0000329# Helper for instance creation without calling __init__
330class _EmptyClass:
331 pass