Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 1 | """Generic (shallow and deep) copying operations. |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 3 | Interface summary: |
| 4 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 5 | import copy |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 6 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 7 | x = copy.copy(y) # make a shallow copy of y |
| 8 | x = copy.deepcopy(y) # make a deep copy of y |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | c755758 | 2003-02-06 19:53:22 +0000 | [diff] [blame] | 10 | For module specific errors, copy.Error is raised. |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 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 |
Raymond Hettinger | f9d88ab | 2005-06-13 01:10:15 +0000 | [diff] [blame] | 17 | extent possible) inserts *the same objects* into it that the |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | f7cea10 | 1997-05-28 19:31:14 +0000 | [diff] [blame] | 26 | a) recursive objects (compound objects that, directly or indirectly, |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 27 | contain a reference to themselves) may cause a recursive loop |
| 28 | |
Guido van Rossum | f7cea10 | 1997-05-28 19:31:14 +0000 | [diff] [blame] | 29 | b) because deep copy copies *everything* it may copy too much, e.g. |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 30 | administrative data structures that should be shared even between |
| 31 | copies |
| 32 | |
| 33 | Python's deep copy operation avoids these problems by: |
| 34 | |
Guido van Rossum | f7cea10 | 1997-05-28 19:31:14 +0000 | [diff] [blame] | 35 | a) keeping a table of objects already copied during the current |
| 36 | copying pass |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 37 | |
Guido van Rossum | f7cea10 | 1997-05-28 19:31:14 +0000 | [diff] [blame] | 38 | b) letting user-defined classes override the copying operation or the |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 39 | set of components copied |
| 40 | |
| 41 | This version does not copy types like module, class, function, method, |
| 42 | nor stack trace, stack frame, nor file, socket, window, nor array, nor |
| 43 | any similar types. |
| 44 | |
| 45 | Classes can use the same interfaces to control copying that they use |
| 46 | to control pickling: they can define methods called __getinitargs__(), |
Guido van Rossum | c5d2d51 | 1997-12-07 16:18:22 +0000 | [diff] [blame] | 47 | __getstate__() and __setstate__(). See the documentation for module |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 48 | "pickle" for information on these methods. |
| 49 | """ |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 50 | |
| 51 | import types |
Antoine Pitrou | 6e61006 | 2009-05-15 17:04:50 +0000 | [diff] [blame] | 52 | import weakref |
Alexandre Vassalotti | f7fa63d | 2008-05-11 08:55:36 +0000 | [diff] [blame] | 53 | from copyreg import dispatch_table |
Antoine Pitrou | d6399d2 | 2010-09-04 17:46:44 +0000 | [diff] [blame] | 54 | import builtins |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 55 | |
Fred Drake | 227b120 | 2000-08-17 05:06:49 +0000 | [diff] [blame] | 56 | class Error(Exception): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 57 | pass |
| 58 | error = Error # backward compatibility |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 59 | |
Guido van Rossum | f8baad0 | 2000-11-27 21:53:14 +0000 | [diff] [blame] | 60 | try: |
| 61 | from org.python.core import PyStringMap |
| 62 | except ImportError: |
| 63 | PyStringMap = None |
| 64 | |
Guido van Rossum | c755758 | 2003-02-06 19:53:22 +0000 | [diff] [blame] | 65 | __all__ = ["Error", "copy", "deepcopy"] |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 67 | def copy(x): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 68 | """Shallow copy operation on arbitrary Python objects. |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 69 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 70 | See the module's __doc__ string for more info. |
| 71 | """ |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 72 | |
Guido van Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 73 | cls = type(x) |
| 74 | |
| 75 | copier = _copy_dispatch.get(cls) |
| 76 | if copier: |
| 77 | return copier(x) |
| 78 | |
Alexandre Vassalotti | 5c1c3b4 | 2013-12-01 13:25:26 -0800 | [diff] [blame] | 79 | 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 Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 87 | copier = getattr(cls, "__copy__", None) |
| 88 | if copier: |
| 89 | return copier(x) |
| 90 | |
| 91 | reductor = dispatch_table.get(cls) |
Guido van Rossum | e690883 | 2003-02-19 01:19:28 +0000 | [diff] [blame] | 92 | if reductor: |
| 93 | rv = reductor(x) |
| 94 | else: |
| 95 | reductor = getattr(x, "__reduce_ex__", None) |
| 96 | if reductor: |
| 97 | rv = reductor(2) |
| 98 | 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 Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 104 | |
Guido van Rossum | e690883 | 2003-02-19 01:19:28 +0000 | [diff] [blame] | 105 | return _reconstruct(x, rv, 0) |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 106 | |
Guido van Rossum | c755758 | 2003-02-06 19:53:22 +0000 | [diff] [blame] | 107 | |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 108 | _copy_dispatch = d = {} |
| 109 | |
Raymond Hettinger | f0e3569 | 2004-03-08 05:59:33 +0000 | [diff] [blame] | 110 | def _copy_immutable(x): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 111 | return x |
Neal Norwitz | b69b2e5 | 2007-02-27 03:41:04 +0000 | [diff] [blame] | 112 | for t in (type(None), int, float, bool, str, tuple, |
Antoine Pitrou | dc9215f | 2014-02-27 22:14:31 +0100 | [diff] [blame] | 113 | bytes, frozenset, type, range, |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 114 | types.BuiltinFunctionType, type(Ellipsis), |
Antoine Pitrou | 6e61006 | 2009-05-15 17:04:50 +0000 | [diff] [blame] | 115 | types.FunctionType, weakref.ref): |
Raymond Hettinger | f0e3569 | 2004-03-08 05:59:33 +0000 | [diff] [blame] | 116 | d[t] = _copy_immutable |
Guido van Rossum | 1325790 | 2007-06-07 23:15:56 +0000 | [diff] [blame] | 117 | t = getattr(types, "CodeType", None) |
| 118 | if t is not None: |
| 119 | d[t] = _copy_immutable |
| 120 | for name in ("complex", "unicode"): |
Antoine Pitrou | d6399d2 | 2010-09-04 17:46:44 +0000 | [diff] [blame] | 121 | t = getattr(builtins, name, None) |
Raymond Hettinger | f0e3569 | 2004-03-08 05:59:33 +0000 | [diff] [blame] | 122 | if t is not None: |
| 123 | d[t] = _copy_immutable |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 124 | |
Raymond Hettinger | f0e3569 | 2004-03-08 05:59:33 +0000 | [diff] [blame] | 125 | def _copy_with_constructor(x): |
| 126 | return type(x)(x) |
| 127 | for t in (list, dict, set): |
| 128 | d[t] = _copy_with_constructor |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 129 | |
Raymond Hettinger | f0e3569 | 2004-03-08 05:59:33 +0000 | [diff] [blame] | 130 | def _copy_with_copy_method(x): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 131 | return x.copy() |
Guido van Rossum | f8baad0 | 2000-11-27 21:53:14 +0000 | [diff] [blame] | 132 | if PyStringMap is not None: |
Raymond Hettinger | f0e3569 | 2004-03-08 05:59:33 +0000 | [diff] [blame] | 133 | d[PyStringMap] = _copy_with_copy_method |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 134 | |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 135 | del d |
| 136 | |
Guido van Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 137 | def deepcopy(x, memo=None, _nil=[]): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 138 | """Deep copy operation on arbitrary Python objects. |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 139 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 140 | See the module's __doc__ string for more info. |
| 141 | """ |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 142 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 143 | if memo is None: |
| 144 | memo = {} |
Guido van Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 145 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 146 | d = id(x) |
Guido van Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 147 | 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 Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 157 | try: |
Guido van Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 158 | issc = issubclass(cls, type) |
| 159 | except TypeError: # cls is not a class (old Boost; see SF #502085) |
Guido van Rossum | 11ade1d | 2002-06-10 21:10:27 +0000 | [diff] [blame] | 160 | issc = 0 |
| 161 | if issc: |
Guido van Rossum | e690883 | 2003-02-19 01:19:28 +0000 | [diff] [blame] | 162 | y = _deepcopy_atomic(x, memo) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 163 | else: |
Guido van Rossum | e690883 | 2003-02-19 01:19:28 +0000 | [diff] [blame] | 164 | 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: |
| 174 | rv = reductor(2) |
| 175 | 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 Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 183 | |
Benjamin Peterson | e90ec36 | 2011-06-27 16:22:46 -0500 | [diff] [blame] | 184 | # 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 Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 188 | return y |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 189 | |
| 190 | _deepcopy_dispatch = d = {} |
| 191 | |
| 192 | def _deepcopy_atomic(x, memo): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 193 | return x |
Raymond Hettinger | f715366 | 2005-02-07 14:16:21 +0000 | [diff] [blame] | 194 | d[type(None)] = _deepcopy_atomic |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 195 | d[type(Ellipsis)] = _deepcopy_atomic |
Raymond Hettinger | f715366 | 2005-02-07 14:16:21 +0000 | [diff] [blame] | 196 | d[int] = _deepcopy_atomic |
Raymond Hettinger | f715366 | 2005-02-07 14:16:21 +0000 | [diff] [blame] | 197 | d[float] = _deepcopy_atomic |
| 198 | d[bool] = _deepcopy_atomic |
Guido van Rossum | 8b9def3 | 2001-09-28 18:16:13 +0000 | [diff] [blame] | 199 | try: |
Raymond Hettinger | f715366 | 2005-02-07 14:16:21 +0000 | [diff] [blame] | 200 | d[complex] = _deepcopy_atomic |
Martin v. Löwis | e2713be | 2005-03-08 15:03:08 +0000 | [diff] [blame] | 201 | except NameError: |
Guido van Rossum | 8b9def3 | 2001-09-28 18:16:13 +0000 | [diff] [blame] | 202 | pass |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 203 | d[bytes] = _deepcopy_atomic |
Raymond Hettinger | f715366 | 2005-02-07 14:16:21 +0000 | [diff] [blame] | 204 | d[str] = _deepcopy_atomic |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 205 | try: |
Guido van Rossum | 88b666c | 2002-02-28 23:19:52 +0000 | [diff] [blame] | 206 | d[types.CodeType] = _deepcopy_atomic |
| 207 | except AttributeError: |
| 208 | pass |
Raymond Hettinger | f715366 | 2005-02-07 14:16:21 +0000 | [diff] [blame] | 209 | d[type] = _deepcopy_atomic |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 210 | d[range] = _deepcopy_atomic |
Martin v. Löwis | ba8f5ff | 2003-06-14 07:10:06 +0000 | [diff] [blame] | 211 | d[types.BuiltinFunctionType] = _deepcopy_atomic |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 212 | d[types.FunctionType] = _deepcopy_atomic |
Antoine Pitrou | 6e61006 | 2009-05-15 17:04:50 +0000 | [diff] [blame] | 213 | d[weakref.ref] = _deepcopy_atomic |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 214 | |
| 215 | def _deepcopy_list(x, memo): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 216 | y = [] |
| 217 | memo[id(x)] = y |
| 218 | for a in x: |
| 219 | y.append(deepcopy(a, memo)) |
| 220 | return y |
Raymond Hettinger | f715366 | 2005-02-07 14:16:21 +0000 | [diff] [blame] | 221 | d[list] = _deepcopy_list |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 222 | |
| 223 | def _deepcopy_tuple(x, memo): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 224 | y = [] |
| 225 | for a in x: |
| 226 | y.append(deepcopy(a, memo)) |
Benjamin Peterson | e90ec36 | 2011-06-27 16:22:46 -0500 | [diff] [blame] | 227 | # We're not going to put the tuple in the memo, but it's still important we |
| 228 | # check for it, in case the tuple contains recursive mutable structures. |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 229 | try: |
Benjamin Peterson | e90ec36 | 2011-06-27 16:22:46 -0500 | [diff] [blame] | 230 | return memo[id(x)] |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 231 | except KeyError: |
| 232 | pass |
| 233 | for i in range(len(x)): |
| 234 | if x[i] is not y[i]: |
| 235 | y = tuple(y) |
| 236 | break |
| 237 | else: |
| 238 | y = x |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 239 | return y |
Raymond Hettinger | f715366 | 2005-02-07 14:16:21 +0000 | [diff] [blame] | 240 | d[tuple] = _deepcopy_tuple |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 241 | |
| 242 | def _deepcopy_dict(x, memo): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 243 | y = {} |
| 244 | memo[id(x)] = y |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 245 | for key, value in x.items(): |
Raymond Hettinger | e0d4972 | 2002-06-02 18:55:56 +0000 | [diff] [blame] | 246 | y[deepcopy(key, memo)] = deepcopy(value, memo) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 247 | return y |
Raymond Hettinger | f715366 | 2005-02-07 14:16:21 +0000 | [diff] [blame] | 248 | d[dict] = _deepcopy_dict |
Guido van Rossum | f8baad0 | 2000-11-27 21:53:14 +0000 | [diff] [blame] | 249 | if PyStringMap is not None: |
| 250 | d[PyStringMap] = _deepcopy_dict |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 251 | |
Antoine Pitrou | 1fc0d2b | 2009-11-28 15:58:27 +0000 | [diff] [blame] | 252 | def _deepcopy_method(x, memo): # Copy instance methods |
| 253 | return type(x)(x.__func__, deepcopy(x.__self__, memo)) |
| 254 | _deepcopy_dispatch[types.MethodType] = _deepcopy_method |
| 255 | |
Guido van Rossum | 558be28 | 1997-08-20 22:26:19 +0000 | [diff] [blame] | 256 | def _keep_alive(x, memo): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 257 | """Keeps a reference to the object x in the memo. |
Guido van Rossum | 558be28 | 1997-08-20 22:26:19 +0000 | [diff] [blame] | 258 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 259 | Because we remember objects by their id, we have |
| 260 | to assure that possibly temporary objects are kept |
| 261 | alive by referencing them. |
| 262 | We store a reference at the id of the memo, which should |
| 263 | normally not be used unless someone tries to deepcopy |
| 264 | the memo itself... |
| 265 | """ |
| 266 | try: |
| 267 | memo[id(memo)].append(x) |
| 268 | except KeyError: |
| 269 | # aha, this is the first one :-) |
| 270 | memo[id(memo)]=[x] |
Guido van Rossum | 558be28 | 1997-08-20 22:26:19 +0000 | [diff] [blame] | 271 | |
Guido van Rossum | 1e91c14 | 2001-12-28 21:33:22 +0000 | [diff] [blame] | 272 | def _reconstruct(x, info, deep, memo=None): |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 273 | if isinstance(info, str): |
| 274 | return x |
| 275 | assert isinstance(info, tuple) |
Guido van Rossum | 1e91c14 | 2001-12-28 21:33:22 +0000 | [diff] [blame] | 276 | if memo is None: |
| 277 | memo = {} |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 278 | n = len(info) |
Guido van Rossum | 90e05b0 | 2003-02-06 18:18:23 +0000 | [diff] [blame] | 279 | assert n in (2, 3, 4, 5) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 280 | callable, args = info[:2] |
| 281 | if n > 2: |
| 282 | state = info[2] |
| 283 | else: |
| 284 | state = {} |
Guido van Rossum | 90e05b0 | 2003-02-06 18:18:23 +0000 | [diff] [blame] | 285 | if n > 3: |
| 286 | listiter = info[3] |
| 287 | else: |
| 288 | listiter = None |
| 289 | if n > 4: |
| 290 | dictiter = info[4] |
| 291 | else: |
| 292 | dictiter = None |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 293 | if deep: |
Guido van Rossum | 1e91c14 | 2001-12-28 21:33:22 +0000 | [diff] [blame] | 294 | args = deepcopy(args, memo) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 295 | y = callable(*args) |
Guido van Rossum | 99d2c25 | 2003-06-13 19:28:47 +0000 | [diff] [blame] | 296 | memo[id(x)] = y |
Antoine Pitrou | 3941a8f | 2010-09-04 17:40:21 +0000 | [diff] [blame] | 297 | |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 298 | if state: |
| 299 | if deep: |
Guido van Rossum | 1e91c14 | 2001-12-28 21:33:22 +0000 | [diff] [blame] | 300 | state = deepcopy(state, memo) |
Guido van Rossum | 3e3583c | 2002-06-06 17:41:20 +0000 | [diff] [blame] | 301 | if hasattr(y, '__setstate__'): |
| 302 | y.__setstate__(state) |
| 303 | else: |
Guido van Rossum | c755758 | 2003-02-06 19:53:22 +0000 | [diff] [blame] | 304 | if isinstance(state, tuple) and len(state) == 2: |
| 305 | state, slotstate = state |
| 306 | else: |
| 307 | slotstate = None |
| 308 | if state is not None: |
| 309 | y.__dict__.update(state) |
| 310 | if slotstate is not None: |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 311 | for key, value in slotstate.items(): |
Guido van Rossum | c755758 | 2003-02-06 19:53:22 +0000 | [diff] [blame] | 312 | setattr(y, key, value) |
Antoine Pitrou | 3941a8f | 2010-09-04 17:40:21 +0000 | [diff] [blame] | 313 | |
| 314 | if listiter is not None: |
| 315 | for item in listiter: |
| 316 | if deep: |
| 317 | item = deepcopy(item, memo) |
| 318 | y.append(item) |
| 319 | if dictiter is not None: |
| 320 | for key, value in dictiter: |
| 321 | if deep: |
| 322 | key = deepcopy(key, memo) |
| 323 | value = deepcopy(value, memo) |
| 324 | y[key] = value |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 325 | return y |
| 326 | |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 327 | del d |
| 328 | |
| 329 | del types |
| 330 | |
Guido van Rossum | c5d2d51 | 1997-12-07 16:18:22 +0000 | [diff] [blame] | 331 | # Helper for instance creation without calling __init__ |
| 332 | class _EmptyClass: |
| 333 | pass |