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 |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 54 | |
Fred Drake | 227b120 | 2000-08-17 05:06:49 +0000 | [diff] [blame] | 55 | class Error(Exception): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 56 | pass |
| 57 | error = Error # backward compatibility |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 58 | |
Guido van Rossum | f8baad0 | 2000-11-27 21:53:14 +0000 | [diff] [blame] | 59 | try: |
| 60 | from org.python.core import PyStringMap |
| 61 | except ImportError: |
| 62 | PyStringMap = None |
| 63 | |
Guido van Rossum | c755758 | 2003-02-06 19:53:22 +0000 | [diff] [blame] | 64 | __all__ = ["Error", "copy", "deepcopy"] |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 66 | def copy(x): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 67 | """Shallow copy operation on arbitrary Python objects. |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 68 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 69 | See the module's __doc__ string for more info. |
| 70 | """ |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 71 | |
Guido van Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 72 | cls = type(x) |
| 73 | |
| 74 | copier = _copy_dispatch.get(cls) |
| 75 | if copier: |
| 76 | return copier(x) |
| 77 | |
Alexandre Vassalotti | 5c1c3b4 | 2013-12-01 13:25:26 -0800 | [diff] [blame] | 78 | try: |
| 79 | issc = issubclass(cls, type) |
| 80 | except TypeError: # cls is not a class |
| 81 | issc = False |
| 82 | if issc: |
| 83 | # treat it as a regular class: |
| 84 | return _copy_immutable(x) |
| 85 | |
Guido van Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 86 | copier = getattr(cls, "__copy__", None) |
| 87 | if copier: |
| 88 | return copier(x) |
| 89 | |
| 90 | reductor = dispatch_table.get(cls) |
Guido van Rossum | e690883 | 2003-02-19 01:19:28 +0000 | [diff] [blame] | 91 | if reductor: |
| 92 | rv = reductor(x) |
| 93 | else: |
| 94 | reductor = getattr(x, "__reduce_ex__", None) |
| 95 | if reductor: |
Serhiy Storchaka | 32af754 | 2015-03-24 18:06:42 +0200 | [diff] [blame] | 96 | rv = reductor(4) |
Guido van Rossum | e690883 | 2003-02-19 01:19:28 +0000 | [diff] [blame] | 97 | else: |
| 98 | reductor = getattr(x, "__reduce__", None) |
| 99 | if reductor: |
| 100 | rv = reductor() |
| 101 | else: |
| 102 | raise Error("un(shallow)copyable object of type %s" % cls) |
Guido van Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 103 | |
Serhiy Storchaka | 818e18d | 2016-03-06 14:56:57 +0200 | [diff] [blame] | 104 | if isinstance(rv, str): |
| 105 | return x |
| 106 | return _reconstruct(x, None, *rv) |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 107 | |
Guido van Rossum | c755758 | 2003-02-06 19:53:22 +0000 | [diff] [blame] | 108 | |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 109 | _copy_dispatch = d = {} |
| 110 | |
Raymond Hettinger | f0e3569 | 2004-03-08 05:59:33 +0000 | [diff] [blame] | 111 | def _copy_immutable(x): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 112 | return x |
Serhiy Storchaka | 818e18d | 2016-03-06 14:56:57 +0200 | [diff] [blame] | 113 | for t in (type(None), int, float, bool, complex, str, tuple, |
| 114 | bytes, frozenset, type, range, slice, |
| 115 | types.BuiltinFunctionType, type(Ellipsis), type(NotImplemented), |
Antoine Pitrou | 6e61006 | 2009-05-15 17:04:50 +0000 | [diff] [blame] | 116 | types.FunctionType, weakref.ref): |
Raymond Hettinger | f0e3569 | 2004-03-08 05:59:33 +0000 | [diff] [blame] | 117 | d[t] = _copy_immutable |
Guido van Rossum | 1325790 | 2007-06-07 23:15:56 +0000 | [diff] [blame] | 118 | t = getattr(types, "CodeType", None) |
| 119 | if t is not None: |
| 120 | d[t] = _copy_immutable |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 121 | |
Serhiy Storchaka | 818e18d | 2016-03-06 14:56:57 +0200 | [diff] [blame] | 122 | d[list] = list.copy |
| 123 | d[dict] = dict.copy |
| 124 | d[set] = set.copy |
| 125 | d[bytearray] = bytearray.copy |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 126 | |
Guido van Rossum | f8baad0 | 2000-11-27 21:53:14 +0000 | [diff] [blame] | 127 | if PyStringMap is not None: |
Serhiy Storchaka | 818e18d | 2016-03-06 14:56:57 +0200 | [diff] [blame] | 128 | d[PyStringMap] = PyStringMap.copy |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 129 | |
Serhiy Storchaka | 818e18d | 2016-03-06 14:56:57 +0200 | [diff] [blame] | 130 | del d, t |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 131 | |
Guido van Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 132 | def deepcopy(x, memo=None, _nil=[]): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 133 | """Deep copy operation on arbitrary Python objects. |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 134 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 135 | See the module's __doc__ string for more info. |
| 136 | """ |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 137 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 138 | if memo is None: |
| 139 | memo = {} |
Guido van Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 140 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 141 | d = id(x) |
Guido van Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 142 | y = memo.get(d, _nil) |
| 143 | if y is not _nil: |
| 144 | return y |
| 145 | |
| 146 | cls = type(x) |
| 147 | |
| 148 | copier = _deepcopy_dispatch.get(cls) |
| 149 | if copier: |
| 150 | y = copier(x, memo) |
| 151 | else: |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 152 | try: |
Guido van Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 153 | issc = issubclass(cls, type) |
| 154 | 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] | 155 | issc = 0 |
| 156 | if issc: |
Guido van Rossum | e690883 | 2003-02-19 01:19:28 +0000 | [diff] [blame] | 157 | y = _deepcopy_atomic(x, memo) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 158 | else: |
Guido van Rossum | e690883 | 2003-02-19 01:19:28 +0000 | [diff] [blame] | 159 | copier = getattr(x, "__deepcopy__", None) |
| 160 | if copier: |
| 161 | y = copier(memo) |
| 162 | else: |
| 163 | reductor = dispatch_table.get(cls) |
| 164 | if reductor: |
| 165 | rv = reductor(x) |
| 166 | else: |
| 167 | reductor = getattr(x, "__reduce_ex__", None) |
| 168 | if reductor: |
Serhiy Storchaka | 32af754 | 2015-03-24 18:06:42 +0200 | [diff] [blame] | 169 | rv = reductor(4) |
Guido van Rossum | e690883 | 2003-02-19 01:19:28 +0000 | [diff] [blame] | 170 | else: |
| 171 | reductor = getattr(x, "__reduce__", None) |
| 172 | if reductor: |
| 173 | rv = reductor() |
| 174 | else: |
| 175 | raise Error( |
| 176 | "un(deep)copyable object of type %s" % cls) |
Serhiy Storchaka | 818e18d | 2016-03-06 14:56:57 +0200 | [diff] [blame] | 177 | if isinstance(rv, str): |
| 178 | y = x |
| 179 | else: |
| 180 | y = _reconstruct(x, memo, *rv) |
Guido van Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 181 | |
Benjamin Peterson | e90ec36 | 2011-06-27 16:22:46 -0500 | [diff] [blame] | 182 | # If is its own copy, don't memoize. |
| 183 | if y is not x: |
| 184 | memo[d] = y |
| 185 | _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] | 186 | return y |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 187 | |
| 188 | _deepcopy_dispatch = d = {} |
| 189 | |
| 190 | def _deepcopy_atomic(x, memo): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 191 | return x |
Raymond Hettinger | f715366 | 2005-02-07 14:16:21 +0000 | [diff] [blame] | 192 | d[type(None)] = _deepcopy_atomic |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 193 | d[type(Ellipsis)] = _deepcopy_atomic |
Serhiy Storchaka | 818e18d | 2016-03-06 14:56:57 +0200 | [diff] [blame] | 194 | d[type(NotImplemented)] = _deepcopy_atomic |
Raymond Hettinger | f715366 | 2005-02-07 14:16:21 +0000 | [diff] [blame] | 195 | d[int] = _deepcopy_atomic |
Raymond Hettinger | f715366 | 2005-02-07 14:16:21 +0000 | [diff] [blame] | 196 | d[float] = _deepcopy_atomic |
| 197 | d[bool] = _deepcopy_atomic |
Serhiy Storchaka | 818e18d | 2016-03-06 14:56:57 +0200 | [diff] [blame] | 198 | d[complex] = _deepcopy_atomic |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 199 | d[bytes] = _deepcopy_atomic |
Raymond Hettinger | f715366 | 2005-02-07 14:16:21 +0000 | [diff] [blame] | 200 | d[str] = _deepcopy_atomic |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 201 | try: |
Guido van Rossum | 88b666c | 2002-02-28 23:19:52 +0000 | [diff] [blame] | 202 | d[types.CodeType] = _deepcopy_atomic |
| 203 | except AttributeError: |
| 204 | pass |
Raymond Hettinger | f715366 | 2005-02-07 14:16:21 +0000 | [diff] [blame] | 205 | d[type] = _deepcopy_atomic |
Martin v. Löwis | ba8f5ff | 2003-06-14 07:10:06 +0000 | [diff] [blame] | 206 | d[types.BuiltinFunctionType] = _deepcopy_atomic |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 207 | d[types.FunctionType] = _deepcopy_atomic |
Antoine Pitrou | 6e61006 | 2009-05-15 17:04:50 +0000 | [diff] [blame] | 208 | d[weakref.ref] = _deepcopy_atomic |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 209 | |
Serhiy Storchaka | 818e18d | 2016-03-06 14:56:57 +0200 | [diff] [blame] | 210 | def _deepcopy_list(x, memo, deepcopy=deepcopy): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 211 | y = [] |
| 212 | memo[id(x)] = y |
Serhiy Storchaka | 818e18d | 2016-03-06 14:56:57 +0200 | [diff] [blame] | 213 | append = y.append |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 214 | for a in x: |
Serhiy Storchaka | 818e18d | 2016-03-06 14:56:57 +0200 | [diff] [blame] | 215 | append(deepcopy(a, memo)) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 216 | return y |
Raymond Hettinger | f715366 | 2005-02-07 14:16:21 +0000 | [diff] [blame] | 217 | d[list] = _deepcopy_list |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 218 | |
Serhiy Storchaka | 818e18d | 2016-03-06 14:56:57 +0200 | [diff] [blame] | 219 | def _deepcopy_tuple(x, memo, deepcopy=deepcopy): |
Benjamin Peterson | 4ce5f3f | 2014-05-03 20:22:00 -0400 | [diff] [blame] | 220 | y = [deepcopy(a, memo) for a in x] |
Benjamin Peterson | e90ec36 | 2011-06-27 16:22:46 -0500 | [diff] [blame] | 221 | # We're not going to put the tuple in the memo, but it's still important we |
| 222 | # check for it, in case the tuple contains recursive mutable structures. |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 223 | try: |
Benjamin Peterson | e90ec36 | 2011-06-27 16:22:46 -0500 | [diff] [blame] | 224 | return memo[id(x)] |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 225 | except KeyError: |
| 226 | pass |
Benjamin Peterson | 4ce5f3f | 2014-05-03 20:22:00 -0400 | [diff] [blame] | 227 | for k, j in zip(x, y): |
| 228 | if k is not j: |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 229 | y = tuple(y) |
| 230 | break |
| 231 | else: |
| 232 | y = x |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 233 | return y |
Raymond Hettinger | f715366 | 2005-02-07 14:16:21 +0000 | [diff] [blame] | 234 | d[tuple] = _deepcopy_tuple |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 235 | |
Serhiy Storchaka | 818e18d | 2016-03-06 14:56:57 +0200 | [diff] [blame] | 236 | def _deepcopy_dict(x, memo, deepcopy=deepcopy): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 237 | y = {} |
| 238 | memo[id(x)] = y |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 239 | for key, value in x.items(): |
Raymond Hettinger | e0d4972 | 2002-06-02 18:55:56 +0000 | [diff] [blame] | 240 | y[deepcopy(key, memo)] = deepcopy(value, memo) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 241 | return y |
Raymond Hettinger | f715366 | 2005-02-07 14:16:21 +0000 | [diff] [blame] | 242 | d[dict] = _deepcopy_dict |
Guido van Rossum | f8baad0 | 2000-11-27 21:53:14 +0000 | [diff] [blame] | 243 | if PyStringMap is not None: |
| 244 | d[PyStringMap] = _deepcopy_dict |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 245 | |
Antoine Pitrou | 1fc0d2b | 2009-11-28 15:58:27 +0000 | [diff] [blame] | 246 | def _deepcopy_method(x, memo): # Copy instance methods |
| 247 | return type(x)(x.__func__, deepcopy(x.__self__, memo)) |
Serhiy Storchaka | 818e18d | 2016-03-06 14:56:57 +0200 | [diff] [blame] | 248 | d[types.MethodType] = _deepcopy_method |
| 249 | |
| 250 | del d |
Antoine Pitrou | 1fc0d2b | 2009-11-28 15:58:27 +0000 | [diff] [blame] | 251 | |
Guido van Rossum | 558be28 | 1997-08-20 22:26:19 +0000 | [diff] [blame] | 252 | def _keep_alive(x, memo): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 253 | """Keeps a reference to the object x in the memo. |
Guido van Rossum | 558be28 | 1997-08-20 22:26:19 +0000 | [diff] [blame] | 254 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 255 | Because we remember objects by their id, we have |
| 256 | to assure that possibly temporary objects are kept |
| 257 | alive by referencing them. |
| 258 | We store a reference at the id of the memo, which should |
| 259 | normally not be used unless someone tries to deepcopy |
| 260 | the memo itself... |
| 261 | """ |
| 262 | try: |
| 263 | memo[id(memo)].append(x) |
| 264 | except KeyError: |
| 265 | # aha, this is the first one :-) |
| 266 | memo[id(memo)]=[x] |
Guido van Rossum | 558be28 | 1997-08-20 22:26:19 +0000 | [diff] [blame] | 267 | |
Serhiy Storchaka | 818e18d | 2016-03-06 14:56:57 +0200 | [diff] [blame] | 268 | def _reconstruct(x, memo, func, args, |
| 269 | state=None, listiter=None, dictiter=None, |
| 270 | deepcopy=deepcopy): |
| 271 | deep = memo is not None |
| 272 | if deep and args: |
| 273 | args = (deepcopy(arg, memo) for arg in args) |
| 274 | y = func(*args) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 275 | if deep: |
Serhiy Storchaka | 818e18d | 2016-03-06 14:56:57 +0200 | [diff] [blame] | 276 | memo[id(x)] = y |
Antoine Pitrou | 3941a8f | 2010-09-04 17:40:21 +0000 | [diff] [blame] | 277 | |
Serhiy Storchaka | cbbec1c | 2015-11-30 17:20:02 +0200 | [diff] [blame] | 278 | if state is not None: |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 279 | if deep: |
Guido van Rossum | 1e91c14 | 2001-12-28 21:33:22 +0000 | [diff] [blame] | 280 | state = deepcopy(state, memo) |
Guido van Rossum | 3e3583c | 2002-06-06 17:41:20 +0000 | [diff] [blame] | 281 | if hasattr(y, '__setstate__'): |
| 282 | y.__setstate__(state) |
| 283 | else: |
Guido van Rossum | c755758 | 2003-02-06 19:53:22 +0000 | [diff] [blame] | 284 | if isinstance(state, tuple) and len(state) == 2: |
| 285 | state, slotstate = state |
| 286 | else: |
| 287 | slotstate = None |
| 288 | if state is not None: |
| 289 | y.__dict__.update(state) |
| 290 | if slotstate is not None: |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 291 | for key, value in slotstate.items(): |
Guido van Rossum | c755758 | 2003-02-06 19:53:22 +0000 | [diff] [blame] | 292 | setattr(y, key, value) |
Antoine Pitrou | 3941a8f | 2010-09-04 17:40:21 +0000 | [diff] [blame] | 293 | |
| 294 | if listiter is not None: |
Serhiy Storchaka | 818e18d | 2016-03-06 14:56:57 +0200 | [diff] [blame] | 295 | if deep: |
| 296 | for item in listiter: |
Antoine Pitrou | 3941a8f | 2010-09-04 17:40:21 +0000 | [diff] [blame] | 297 | item = deepcopy(item, memo) |
Serhiy Storchaka | 818e18d | 2016-03-06 14:56:57 +0200 | [diff] [blame] | 298 | y.append(item) |
| 299 | else: |
| 300 | for item in listiter: |
| 301 | y.append(item) |
Antoine Pitrou | 3941a8f | 2010-09-04 17:40:21 +0000 | [diff] [blame] | 302 | if dictiter is not None: |
Serhiy Storchaka | 818e18d | 2016-03-06 14:56:57 +0200 | [diff] [blame] | 303 | if deep: |
| 304 | for key, value in dictiter: |
Antoine Pitrou | 3941a8f | 2010-09-04 17:40:21 +0000 | [diff] [blame] | 305 | key = deepcopy(key, memo) |
| 306 | value = deepcopy(value, memo) |
Serhiy Storchaka | 818e18d | 2016-03-06 14:56:57 +0200 | [diff] [blame] | 307 | y[key] = value |
| 308 | else: |
| 309 | for key, value in dictiter: |
| 310 | y[key] = value |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 311 | return y |
| 312 | |
Serhiy Storchaka | 818e18d | 2016-03-06 14:56:57 +0200 | [diff] [blame] | 313 | del types, weakref, PyStringMap |