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