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 | ce111ca | 2010-09-04 17:49:13 +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 | |
| 79 | copier = getattr(cls, "__copy__", None) |
| 80 | if copier: |
| 81 | return copier(x) |
| 82 | |
| 83 | reductor = dispatch_table.get(cls) |
Guido van Rossum | e690883 | 2003-02-19 01:19:28 +0000 | [diff] [blame] | 84 | if reductor: |
| 85 | rv = reductor(x) |
| 86 | else: |
| 87 | reductor = getattr(x, "__reduce_ex__", None) |
| 88 | if reductor: |
| 89 | rv = reductor(2) |
| 90 | else: |
| 91 | reductor = getattr(x, "__reduce__", None) |
| 92 | if reductor: |
| 93 | rv = reductor() |
| 94 | else: |
| 95 | raise Error("un(shallow)copyable object of type %s" % cls) |
Guido van Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | e690883 | 2003-02-19 01:19:28 +0000 | [diff] [blame] | 97 | return _reconstruct(x, rv, 0) |
Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | c755758 | 2003-02-06 19:53:22 +0000 | [diff] [blame] | 99 | |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 100 | _copy_dispatch = d = {} |
| 101 | |
Raymond Hettinger | f0e3569 | 2004-03-08 05:59:33 +0000 | [diff] [blame] | 102 | def _copy_immutable(x): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 103 | return x |
Neal Norwitz | b69b2e5 | 2007-02-27 03:41:04 +0000 | [diff] [blame] | 104 | for t in (type(None), int, float, bool, str, tuple, |
Guido van Rossum | 1325790 | 2007-06-07 23:15:56 +0000 | [diff] [blame] | 105 | frozenset, type, range, |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 106 | types.BuiltinFunctionType, type(Ellipsis), |
Antoine Pitrou | 6e61006 | 2009-05-15 17:04:50 +0000 | [diff] [blame] | 107 | types.FunctionType, weakref.ref): |
Raymond Hettinger | f0e3569 | 2004-03-08 05:59:33 +0000 | [diff] [blame] | 108 | d[t] = _copy_immutable |
Guido van Rossum | 1325790 | 2007-06-07 23:15:56 +0000 | [diff] [blame] | 109 | t = getattr(types, "CodeType", None) |
| 110 | if t is not None: |
| 111 | d[t] = _copy_immutable |
| 112 | for name in ("complex", "unicode"): |
Antoine Pitrou | ce111ca | 2010-09-04 17:49:13 +0000 | [diff] [blame] | 113 | t = getattr(builtins, name, None) |
Raymond Hettinger | f0e3569 | 2004-03-08 05:59:33 +0000 | [diff] [blame] | 114 | if t is not None: |
| 115 | d[t] = _copy_immutable |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 116 | |
Raymond Hettinger | f0e3569 | 2004-03-08 05:59:33 +0000 | [diff] [blame] | 117 | def _copy_with_constructor(x): |
| 118 | return type(x)(x) |
| 119 | for t in (list, dict, set): |
| 120 | d[t] = _copy_with_constructor |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 121 | |
Raymond Hettinger | f0e3569 | 2004-03-08 05:59:33 +0000 | [diff] [blame] | 122 | def _copy_with_copy_method(x): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 123 | return x.copy() |
Guido van Rossum | f8baad0 | 2000-11-27 21:53:14 +0000 | [diff] [blame] | 124 | if PyStringMap is not None: |
Raymond Hettinger | f0e3569 | 2004-03-08 05:59:33 +0000 | [diff] [blame] | 125 | d[PyStringMap] = _copy_with_copy_method |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 126 | |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 127 | del d |
| 128 | |
Guido van Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 129 | def deepcopy(x, memo=None, _nil=[]): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 130 | """Deep copy operation on arbitrary Python objects. |
Guido van Rossum | cc6764c | 1995-02-09 17:18:10 +0000 | [diff] [blame] | 131 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 132 | See the module's __doc__ string for more info. |
| 133 | """ |
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 | if memo is None: |
| 136 | memo = {} |
Guido van Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 137 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 138 | d = id(x) |
Guido van Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 139 | y = memo.get(d, _nil) |
| 140 | if y is not _nil: |
| 141 | return y |
| 142 | |
| 143 | cls = type(x) |
| 144 | |
| 145 | copier = _deepcopy_dispatch.get(cls) |
| 146 | if copier: |
| 147 | y = copier(x, memo) |
| 148 | else: |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 149 | try: |
Guido van Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 150 | issc = issubclass(cls, type) |
| 151 | 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] | 152 | issc = 0 |
| 153 | if issc: |
Guido van Rossum | e690883 | 2003-02-19 01:19:28 +0000 | [diff] [blame] | 154 | y = _deepcopy_atomic(x, memo) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 155 | else: |
Guido van Rossum | e690883 | 2003-02-19 01:19:28 +0000 | [diff] [blame] | 156 | copier = getattr(x, "__deepcopy__", None) |
| 157 | if copier: |
| 158 | y = copier(memo) |
| 159 | else: |
| 160 | reductor = dispatch_table.get(cls) |
| 161 | if reductor: |
| 162 | rv = reductor(x) |
| 163 | else: |
| 164 | reductor = getattr(x, "__reduce_ex__", None) |
| 165 | if reductor: |
| 166 | rv = reductor(2) |
| 167 | else: |
| 168 | reductor = getattr(x, "__reduce__", None) |
| 169 | if reductor: |
| 170 | rv = reductor() |
| 171 | else: |
| 172 | raise Error( |
| 173 | "un(deep)copyable object of type %s" % cls) |
| 174 | y = _reconstruct(x, rv, 1, memo) |
Guido van Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 175 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 176 | memo[d] = y |
Guido van Rossum | 6115460 | 2002-08-12 20:20:08 +0000 | [diff] [blame] | 177 | _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] | 178 | return y |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 179 | |
| 180 | _deepcopy_dispatch = d = {} |
| 181 | |
| 182 | def _deepcopy_atomic(x, memo): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 183 | return x |
Raymond Hettinger | f715366 | 2005-02-07 14:16:21 +0000 | [diff] [blame] | 184 | d[type(None)] = _deepcopy_atomic |
Christian Heimes | cc47b05 | 2008-03-25 14:56:36 +0000 | [diff] [blame] | 185 | d[type(Ellipsis)] = _deepcopy_atomic |
Raymond Hettinger | f715366 | 2005-02-07 14:16:21 +0000 | [diff] [blame] | 186 | d[int] = _deepcopy_atomic |
Raymond Hettinger | f715366 | 2005-02-07 14:16:21 +0000 | [diff] [blame] | 187 | d[float] = _deepcopy_atomic |
| 188 | d[bool] = _deepcopy_atomic |
Guido van Rossum | 8b9def3 | 2001-09-28 18:16:13 +0000 | [diff] [blame] | 189 | try: |
Raymond Hettinger | f715366 | 2005-02-07 14:16:21 +0000 | [diff] [blame] | 190 | d[complex] = _deepcopy_atomic |
Martin v. Löwis | e2713be | 2005-03-08 15:03:08 +0000 | [diff] [blame] | 191 | except NameError: |
Guido van Rossum | 8b9def3 | 2001-09-28 18:16:13 +0000 | [diff] [blame] | 192 | pass |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 193 | d[bytes] = _deepcopy_atomic |
Raymond Hettinger | f715366 | 2005-02-07 14:16:21 +0000 | [diff] [blame] | 194 | d[str] = _deepcopy_atomic |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 195 | try: |
Guido van Rossum | 88b666c | 2002-02-28 23:19:52 +0000 | [diff] [blame] | 196 | d[types.CodeType] = _deepcopy_atomic |
| 197 | except AttributeError: |
| 198 | pass |
Raymond Hettinger | f715366 | 2005-02-07 14:16:21 +0000 | [diff] [blame] | 199 | d[type] = _deepcopy_atomic |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 200 | d[range] = _deepcopy_atomic |
Martin v. Löwis | ba8f5ff | 2003-06-14 07:10:06 +0000 | [diff] [blame] | 201 | d[types.BuiltinFunctionType] = _deepcopy_atomic |
Guido van Rossum | 1968ad3 | 2006-02-25 22:38:04 +0000 | [diff] [blame] | 202 | d[types.FunctionType] = _deepcopy_atomic |
Antoine Pitrou | 6e61006 | 2009-05-15 17:04:50 +0000 | [diff] [blame] | 203 | d[weakref.ref] = _deepcopy_atomic |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 204 | |
| 205 | def _deepcopy_list(x, memo): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 206 | y = [] |
| 207 | memo[id(x)] = y |
| 208 | for a in x: |
| 209 | y.append(deepcopy(a, memo)) |
| 210 | return y |
Raymond Hettinger | f715366 | 2005-02-07 14:16:21 +0000 | [diff] [blame] | 211 | d[list] = _deepcopy_list |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 212 | |
| 213 | def _deepcopy_tuple(x, memo): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 214 | y = [] |
| 215 | for a in x: |
| 216 | y.append(deepcopy(a, memo)) |
| 217 | d = id(x) |
| 218 | try: |
| 219 | return memo[d] |
| 220 | except KeyError: |
| 221 | pass |
| 222 | for i in range(len(x)): |
| 223 | if x[i] is not y[i]: |
| 224 | y = tuple(y) |
| 225 | break |
| 226 | else: |
| 227 | y = x |
| 228 | memo[d] = y |
| 229 | return y |
Raymond Hettinger | f715366 | 2005-02-07 14:16:21 +0000 | [diff] [blame] | 230 | d[tuple] = _deepcopy_tuple |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 231 | |
| 232 | def _deepcopy_dict(x, memo): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 233 | y = {} |
| 234 | memo[id(x)] = y |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 235 | for key, value in x.items(): |
Raymond Hettinger | e0d4972 | 2002-06-02 18:55:56 +0000 | [diff] [blame] | 236 | y[deepcopy(key, memo)] = deepcopy(value, memo) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 237 | return y |
Raymond Hettinger | f715366 | 2005-02-07 14:16:21 +0000 | [diff] [blame] | 238 | d[dict] = _deepcopy_dict |
Guido van Rossum | f8baad0 | 2000-11-27 21:53:14 +0000 | [diff] [blame] | 239 | if PyStringMap is not None: |
| 240 | d[PyStringMap] = _deepcopy_dict |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 241 | |
Guido van Rossum | 558be28 | 1997-08-20 22:26:19 +0000 | [diff] [blame] | 242 | def _keep_alive(x, memo): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 243 | """Keeps a reference to the object x in the memo. |
Guido van Rossum | 558be28 | 1997-08-20 22:26:19 +0000 | [diff] [blame] | 244 | |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 245 | Because we remember objects by their id, we have |
| 246 | to assure that possibly temporary objects are kept |
| 247 | alive by referencing them. |
| 248 | We store a reference at the id of the memo, which should |
| 249 | normally not be used unless someone tries to deepcopy |
| 250 | the memo itself... |
| 251 | """ |
| 252 | try: |
| 253 | memo[id(memo)].append(x) |
| 254 | except KeyError: |
| 255 | # aha, this is the first one :-) |
| 256 | memo[id(memo)]=[x] |
Guido van Rossum | 558be28 | 1997-08-20 22:26:19 +0000 | [diff] [blame] | 257 | |
Guido van Rossum | 1e91c14 | 2001-12-28 21:33:22 +0000 | [diff] [blame] | 258 | def _reconstruct(x, info, deep, memo=None): |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 259 | if isinstance(info, str): |
| 260 | return x |
| 261 | assert isinstance(info, tuple) |
Guido van Rossum | 1e91c14 | 2001-12-28 21:33:22 +0000 | [diff] [blame] | 262 | if memo is None: |
| 263 | memo = {} |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 264 | n = len(info) |
Guido van Rossum | 90e05b0 | 2003-02-06 18:18:23 +0000 | [diff] [blame] | 265 | assert n in (2, 3, 4, 5) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 266 | callable, args = info[:2] |
| 267 | if n > 2: |
| 268 | state = info[2] |
| 269 | else: |
| 270 | state = {} |
Guido van Rossum | 90e05b0 | 2003-02-06 18:18:23 +0000 | [diff] [blame] | 271 | if n > 3: |
| 272 | listiter = info[3] |
| 273 | else: |
| 274 | listiter = None |
| 275 | if n > 4: |
| 276 | dictiter = info[4] |
| 277 | else: |
| 278 | dictiter = 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 | args = deepcopy(args, memo) |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 281 | y = callable(*args) |
Guido van Rossum | 99d2c25 | 2003-06-13 19:28:47 +0000 | [diff] [blame] | 282 | memo[id(x)] = y |
Antoine Pitrou | ce111ca | 2010-09-04 17:49:13 +0000 | [diff] [blame] | 283 | |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 284 | if state: |
| 285 | if deep: |
Guido van Rossum | 1e91c14 | 2001-12-28 21:33:22 +0000 | [diff] [blame] | 286 | state = deepcopy(state, memo) |
Guido van Rossum | 3e3583c | 2002-06-06 17:41:20 +0000 | [diff] [blame] | 287 | if hasattr(y, '__setstate__'): |
| 288 | y.__setstate__(state) |
| 289 | else: |
Guido van Rossum | c755758 | 2003-02-06 19:53:22 +0000 | [diff] [blame] | 290 | if isinstance(state, tuple) and len(state) == 2: |
| 291 | state, slotstate = state |
| 292 | else: |
| 293 | slotstate = None |
| 294 | if state is not None: |
| 295 | y.__dict__.update(state) |
| 296 | if slotstate is not None: |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 297 | for key, value in slotstate.items(): |
Guido van Rossum | c755758 | 2003-02-06 19:53:22 +0000 | [diff] [blame] | 298 | setattr(y, key, value) |
Antoine Pitrou | ce111ca | 2010-09-04 17:49:13 +0000 | [diff] [blame] | 299 | |
| 300 | if listiter is not None: |
| 301 | for item in listiter: |
| 302 | if deep: |
| 303 | item = deepcopy(item, memo) |
| 304 | y.append(item) |
| 305 | if dictiter is not None: |
| 306 | for key, value in dictiter: |
| 307 | if deep: |
| 308 | key = deepcopy(key, memo) |
| 309 | value = deepcopy(value, memo) |
| 310 | y[key] = value |
Guido van Rossum | 6cef6d5 | 2001-09-28 18:13:29 +0000 | [diff] [blame] | 311 | return y |
| 312 | |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 313 | del d |
| 314 | |
| 315 | del types |
| 316 | |
Guido van Rossum | c5d2d51 | 1997-12-07 16:18:22 +0000 | [diff] [blame] | 317 | # Helper for instance creation without calling __init__ |
| 318 | class _EmptyClass: |
| 319 | pass |
| 320 | |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 321 | def _test(): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 322 | l = [None, 1, 2, 3.14, 'xyzzy', (1, 2), [3.14, 'abc'], |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 323 | {'abc': 'ABC'}, (), [], {}] |
| 324 | l1 = copy(l) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 325 | print(l1==l) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 326 | l1 = map(copy, l) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 327 | print(l1==l) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 328 | l1 = deepcopy(l) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 329 | print(l1==l) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 330 | class C: |
| 331 | def __init__(self, arg=None): |
| 332 | self.a = 1 |
| 333 | self.arg = arg |
| 334 | if __name__ == '__main__': |
| 335 | import sys |
| 336 | file = sys.argv[0] |
| 337 | else: |
| 338 | file = __file__ |
| 339 | self.fp = open(file) |
| 340 | self.fp.close() |
| 341 | def __getstate__(self): |
| 342 | return {'a': self.a, 'arg': self.arg} |
| 343 | def __setstate__(self, state): |
Guido van Rossum | cc2b016 | 2007-02-11 06:12:03 +0000 | [diff] [blame] | 344 | for key, value in state.items(): |
Raymond Hettinger | e0d4972 | 2002-06-02 18:55:56 +0000 | [diff] [blame] | 345 | setattr(self, key, value) |
Guido van Rossum | c06e3ac | 2003-02-07 17:30:18 +0000 | [diff] [blame] | 346 | def __deepcopy__(self, memo=None): |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 347 | new = self.__class__(deepcopy(self.arg, memo)) |
| 348 | new.a = self.a |
| 349 | return new |
| 350 | c = C('argument sketch') |
| 351 | l.append(c) |
| 352 | l2 = copy(l) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 353 | print(l == l2) |
| 354 | print(l) |
| 355 | print(l2) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 356 | l2 = deepcopy(l) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 357 | print(l == l2) |
| 358 | print(l) |
| 359 | print(l2) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 360 | l.append({l[1]: l, 'xyz': l[2]}) |
| 361 | l3 = copy(l) |
Alexandre Vassalotti | 1f2ba4b | 2008-05-16 07:12:44 +0000 | [diff] [blame] | 362 | import reprlib |
| 363 | print(map(reprlib.repr, l)) |
| 364 | print(map(reprlib.repr, l1)) |
| 365 | print(map(reprlib.repr, l2)) |
| 366 | print(map(reprlib.repr, l3)) |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 367 | l3 = deepcopy(l) |
Alexandre Vassalotti | 1f2ba4b | 2008-05-16 07:12:44 +0000 | [diff] [blame] | 368 | print(map(reprlib.repr, l)) |
| 369 | print(map(reprlib.repr, l1)) |
| 370 | print(map(reprlib.repr, l2)) |
| 371 | print(map(reprlib.repr, l3)) |
Antoine Pitrou | ce111ca | 2010-09-04 17:49:13 +0000 | [diff] [blame] | 372 | class odict(dict): |
| 373 | def __init__(self, d = {}): |
| 374 | self.a = 99 |
| 375 | dict.__init__(self, d) |
| 376 | def __setitem__(self, k, i): |
| 377 | dict.__setitem__(self, k, i) |
| 378 | self.a |
| 379 | o = odict({"A" : "B"}) |
| 380 | x = deepcopy(o) |
| 381 | print(o, x) |
Guido van Rossum | 409780f | 1995-01-10 00:34:21 +0000 | [diff] [blame] | 382 | |
| 383 | if __name__ == '__main__': |
Tim Peters | 88869f9 | 2001-01-14 23:36:06 +0000 | [diff] [blame] | 384 | _test() |