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