| 1 | n/a | """Generic (shallow and deep) copying operations. |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | Interface summary: |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | import copy |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | x = copy.copy(y) # make a shallow copy of y |
|---|
| 8 | n/a | x = copy.deepcopy(y) # make a deep copy of y |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | For module specific errors, copy.Error is raised. |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | The difference between shallow and deep copying is only relevant for |
|---|
| 13 | n/a | compound objects (objects that contain other objects, like lists or |
|---|
| 14 | n/a | class instances). |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | - A shallow copy constructs a new compound object and then (to the |
|---|
| 17 | n/a | extent possible) inserts *the same objects* into it that the |
|---|
| 18 | n/a | original contains. |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | - A deep copy constructs a new compound object and then, recursively, |
|---|
| 21 | n/a | inserts *copies* into it of the objects found in the original. |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | Two problems often exist with deep copy operations that don't exist |
|---|
| 24 | n/a | with shallow copy operations: |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | a) recursive objects (compound objects that, directly or indirectly, |
|---|
| 27 | n/a | contain a reference to themselves) may cause a recursive loop |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | b) because deep copy copies *everything* it may copy too much, e.g. |
|---|
| 30 | n/a | administrative data structures that should be shared even between |
|---|
| 31 | n/a | copies |
|---|
| 32 | n/a | |
|---|
| 33 | n/a | Python's deep copy operation avoids these problems by: |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | a) keeping a table of objects already copied during the current |
|---|
| 36 | n/a | copying pass |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | b) letting user-defined classes override the copying operation or the |
|---|
| 39 | n/a | set of components copied |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | This version does not copy types like module, class, function, method, |
|---|
| 42 | n/a | nor stack trace, stack frame, nor file, socket, window, nor array, nor |
|---|
| 43 | n/a | any similar types. |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | Classes can use the same interfaces to control copying that they use |
|---|
| 46 | n/a | to control pickling: they can define methods called __getinitargs__(), |
|---|
| 47 | n/a | __getstate__() and __setstate__(). See the documentation for module |
|---|
| 48 | n/a | "pickle" for information on these methods. |
|---|
| 49 | n/a | """ |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | import types |
|---|
| 52 | n/a | import weakref |
|---|
| 53 | n/a | from copyreg import dispatch_table |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | class Error(Exception): |
|---|
| 56 | n/a | pass |
|---|
| 57 | n/a | error = Error # backward compatibility |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | try: |
|---|
| 60 | n/a | from org.python.core import PyStringMap |
|---|
| 61 | n/a | except ImportError: |
|---|
| 62 | n/a | PyStringMap = None |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | __all__ = ["Error", "copy", "deepcopy"] |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | def copy(x): |
|---|
| 67 | n/a | """Shallow copy operation on arbitrary Python objects. |
|---|
| 68 | n/a | |
|---|
| 69 | n/a | See the module's __doc__ string for more info. |
|---|
| 70 | n/a | """ |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | cls = type(x) |
|---|
| 73 | n/a | |
|---|
| 74 | n/a | copier = _copy_dispatch.get(cls) |
|---|
| 75 | n/a | if copier: |
|---|
| 76 | n/a | return copier(x) |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | try: |
|---|
| 79 | n/a | issc = issubclass(cls, type) |
|---|
| 80 | n/a | except TypeError: # cls is not a class |
|---|
| 81 | n/a | issc = False |
|---|
| 82 | n/a | if issc: |
|---|
| 83 | n/a | # treat it as a regular class: |
|---|
| 84 | n/a | return _copy_immutable(x) |
|---|
| 85 | n/a | |
|---|
| 86 | n/a | copier = getattr(cls, "__copy__", None) |
|---|
| 87 | n/a | if copier: |
|---|
| 88 | n/a | return copier(x) |
|---|
| 89 | n/a | |
|---|
| 90 | n/a | reductor = dispatch_table.get(cls) |
|---|
| 91 | n/a | if reductor: |
|---|
| 92 | n/a | rv = reductor(x) |
|---|
| 93 | n/a | else: |
|---|
| 94 | n/a | reductor = getattr(x, "__reduce_ex__", None) |
|---|
| 95 | n/a | if reductor: |
|---|
| 96 | n/a | rv = reductor(4) |
|---|
| 97 | n/a | else: |
|---|
| 98 | n/a | reductor = getattr(x, "__reduce__", None) |
|---|
| 99 | n/a | if reductor: |
|---|
| 100 | n/a | rv = reductor() |
|---|
| 101 | n/a | else: |
|---|
| 102 | n/a | raise Error("un(shallow)copyable object of type %s" % cls) |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | if isinstance(rv, str): |
|---|
| 105 | n/a | return x |
|---|
| 106 | n/a | return _reconstruct(x, None, *rv) |
|---|
| 107 | n/a | |
|---|
| 108 | n/a | |
|---|
| 109 | n/a | _copy_dispatch = d = {} |
|---|
| 110 | n/a | |
|---|
| 111 | n/a | def _copy_immutable(x): |
|---|
| 112 | n/a | return x |
|---|
| 113 | n/a | for t in (type(None), int, float, bool, complex, str, tuple, |
|---|
| 114 | n/a | bytes, frozenset, type, range, slice, |
|---|
| 115 | n/a | types.BuiltinFunctionType, type(Ellipsis), type(NotImplemented), |
|---|
| 116 | n/a | types.FunctionType, weakref.ref): |
|---|
| 117 | n/a | d[t] = _copy_immutable |
|---|
| 118 | n/a | t = getattr(types, "CodeType", None) |
|---|
| 119 | n/a | if t is not None: |
|---|
| 120 | n/a | d[t] = _copy_immutable |
|---|
| 121 | n/a | |
|---|
| 122 | n/a | d[list] = list.copy |
|---|
| 123 | n/a | d[dict] = dict.copy |
|---|
| 124 | n/a | d[set] = set.copy |
|---|
| 125 | n/a | d[bytearray] = bytearray.copy |
|---|
| 126 | n/a | |
|---|
| 127 | n/a | if PyStringMap is not None: |
|---|
| 128 | n/a | d[PyStringMap] = PyStringMap.copy |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | del d, t |
|---|
| 131 | n/a | |
|---|
| 132 | n/a | def deepcopy(x, memo=None, _nil=[]): |
|---|
| 133 | n/a | """Deep copy operation on arbitrary Python objects. |
|---|
| 134 | n/a | |
|---|
| 135 | n/a | See the module's __doc__ string for more info. |
|---|
| 136 | n/a | """ |
|---|
| 137 | n/a | |
|---|
| 138 | n/a | if memo is None: |
|---|
| 139 | n/a | memo = {} |
|---|
| 140 | n/a | |
|---|
| 141 | n/a | d = id(x) |
|---|
| 142 | n/a | y = memo.get(d, _nil) |
|---|
| 143 | n/a | if y is not _nil: |
|---|
| 144 | n/a | return y |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | cls = type(x) |
|---|
| 147 | n/a | |
|---|
| 148 | n/a | copier = _deepcopy_dispatch.get(cls) |
|---|
| 149 | n/a | if copier: |
|---|
| 150 | n/a | y = copier(x, memo) |
|---|
| 151 | n/a | else: |
|---|
| 152 | n/a | try: |
|---|
| 153 | n/a | issc = issubclass(cls, type) |
|---|
| 154 | n/a | except TypeError: # cls is not a class (old Boost; see SF #502085) |
|---|
| 155 | n/a | issc = 0 |
|---|
| 156 | n/a | if issc: |
|---|
| 157 | n/a | y = _deepcopy_atomic(x, memo) |
|---|
| 158 | n/a | else: |
|---|
| 159 | n/a | copier = getattr(x, "__deepcopy__", None) |
|---|
| 160 | n/a | if copier: |
|---|
| 161 | n/a | y = copier(memo) |
|---|
| 162 | n/a | else: |
|---|
| 163 | n/a | reductor = dispatch_table.get(cls) |
|---|
| 164 | n/a | if reductor: |
|---|
| 165 | n/a | rv = reductor(x) |
|---|
| 166 | n/a | else: |
|---|
| 167 | n/a | reductor = getattr(x, "__reduce_ex__", None) |
|---|
| 168 | n/a | if reductor: |
|---|
| 169 | n/a | rv = reductor(4) |
|---|
| 170 | n/a | else: |
|---|
| 171 | n/a | reductor = getattr(x, "__reduce__", None) |
|---|
| 172 | n/a | if reductor: |
|---|
| 173 | n/a | rv = reductor() |
|---|
| 174 | n/a | else: |
|---|
| 175 | n/a | raise Error( |
|---|
| 176 | n/a | "un(deep)copyable object of type %s" % cls) |
|---|
| 177 | n/a | if isinstance(rv, str): |
|---|
| 178 | n/a | y = x |
|---|
| 179 | n/a | else: |
|---|
| 180 | n/a | y = _reconstruct(x, memo, *rv) |
|---|
| 181 | n/a | |
|---|
| 182 | n/a | # If is its own copy, don't memoize. |
|---|
| 183 | n/a | if y is not x: |
|---|
| 184 | n/a | memo[d] = y |
|---|
| 185 | n/a | _keep_alive(x, memo) # Make sure x lives at least as long as d |
|---|
| 186 | n/a | return y |
|---|
| 187 | n/a | |
|---|
| 188 | n/a | _deepcopy_dispatch = d = {} |
|---|
| 189 | n/a | |
|---|
| 190 | n/a | def _deepcopy_atomic(x, memo): |
|---|
| 191 | n/a | return x |
|---|
| 192 | n/a | d[type(None)] = _deepcopy_atomic |
|---|
| 193 | n/a | d[type(Ellipsis)] = _deepcopy_atomic |
|---|
| 194 | n/a | d[type(NotImplemented)] = _deepcopy_atomic |
|---|
| 195 | n/a | d[int] = _deepcopy_atomic |
|---|
| 196 | n/a | d[float] = _deepcopy_atomic |
|---|
| 197 | n/a | d[bool] = _deepcopy_atomic |
|---|
| 198 | n/a | d[complex] = _deepcopy_atomic |
|---|
| 199 | n/a | d[bytes] = _deepcopy_atomic |
|---|
| 200 | n/a | d[str] = _deepcopy_atomic |
|---|
| 201 | n/a | try: |
|---|
| 202 | n/a | d[types.CodeType] = _deepcopy_atomic |
|---|
| 203 | n/a | except AttributeError: |
|---|
| 204 | n/a | pass |
|---|
| 205 | n/a | d[type] = _deepcopy_atomic |
|---|
| 206 | n/a | d[types.BuiltinFunctionType] = _deepcopy_atomic |
|---|
| 207 | n/a | d[types.FunctionType] = _deepcopy_atomic |
|---|
| 208 | n/a | d[weakref.ref] = _deepcopy_atomic |
|---|
| 209 | n/a | |
|---|
| 210 | n/a | def _deepcopy_list(x, memo, deepcopy=deepcopy): |
|---|
| 211 | n/a | y = [] |
|---|
| 212 | n/a | memo[id(x)] = y |
|---|
| 213 | n/a | append = y.append |
|---|
| 214 | n/a | for a in x: |
|---|
| 215 | n/a | append(deepcopy(a, memo)) |
|---|
| 216 | n/a | return y |
|---|
| 217 | n/a | d[list] = _deepcopy_list |
|---|
| 218 | n/a | |
|---|
| 219 | n/a | def _deepcopy_tuple(x, memo, deepcopy=deepcopy): |
|---|
| 220 | n/a | y = [deepcopy(a, memo) for a in x] |
|---|
| 221 | n/a | # We're not going to put the tuple in the memo, but it's still important we |
|---|
| 222 | n/a | # check for it, in case the tuple contains recursive mutable structures. |
|---|
| 223 | n/a | try: |
|---|
| 224 | n/a | return memo[id(x)] |
|---|
| 225 | n/a | except KeyError: |
|---|
| 226 | n/a | pass |
|---|
| 227 | n/a | for k, j in zip(x, y): |
|---|
| 228 | n/a | if k is not j: |
|---|
| 229 | n/a | y = tuple(y) |
|---|
| 230 | n/a | break |
|---|
| 231 | n/a | else: |
|---|
| 232 | n/a | y = x |
|---|
| 233 | n/a | return y |
|---|
| 234 | n/a | d[tuple] = _deepcopy_tuple |
|---|
| 235 | n/a | |
|---|
| 236 | n/a | def _deepcopy_dict(x, memo, deepcopy=deepcopy): |
|---|
| 237 | n/a | y = {} |
|---|
| 238 | n/a | memo[id(x)] = y |
|---|
| 239 | n/a | for key, value in x.items(): |
|---|
| 240 | n/a | y[deepcopy(key, memo)] = deepcopy(value, memo) |
|---|
| 241 | n/a | return y |
|---|
| 242 | n/a | d[dict] = _deepcopy_dict |
|---|
| 243 | n/a | if PyStringMap is not None: |
|---|
| 244 | n/a | d[PyStringMap] = _deepcopy_dict |
|---|
| 245 | n/a | |
|---|
| 246 | n/a | def _deepcopy_method(x, memo): # Copy instance methods |
|---|
| 247 | n/a | return type(x)(x.__func__, deepcopy(x.__self__, memo)) |
|---|
| 248 | n/a | d[types.MethodType] = _deepcopy_method |
|---|
| 249 | n/a | |
|---|
| 250 | n/a | del d |
|---|
| 251 | n/a | |
|---|
| 252 | n/a | def _keep_alive(x, memo): |
|---|
| 253 | n/a | """Keeps a reference to the object x in the memo. |
|---|
| 254 | n/a | |
|---|
| 255 | n/a | Because we remember objects by their id, we have |
|---|
| 256 | n/a | to assure that possibly temporary objects are kept |
|---|
| 257 | n/a | alive by referencing them. |
|---|
| 258 | n/a | We store a reference at the id of the memo, which should |
|---|
| 259 | n/a | normally not be used unless someone tries to deepcopy |
|---|
| 260 | n/a | the memo itself... |
|---|
| 261 | n/a | """ |
|---|
| 262 | n/a | try: |
|---|
| 263 | n/a | memo[id(memo)].append(x) |
|---|
| 264 | n/a | except KeyError: |
|---|
| 265 | n/a | # aha, this is the first one :-) |
|---|
| 266 | n/a | memo[id(memo)]=[x] |
|---|
| 267 | n/a | |
|---|
| 268 | n/a | def _reconstruct(x, memo, func, args, |
|---|
| 269 | n/a | state=None, listiter=None, dictiter=None, |
|---|
| 270 | n/a | deepcopy=deepcopy): |
|---|
| 271 | n/a | deep = memo is not None |
|---|
| 272 | n/a | if deep and args: |
|---|
| 273 | n/a | args = (deepcopy(arg, memo) for arg in args) |
|---|
| 274 | n/a | y = func(*args) |
|---|
| 275 | n/a | if deep: |
|---|
| 276 | n/a | memo[id(x)] = y |
|---|
| 277 | n/a | |
|---|
| 278 | n/a | if state is not None: |
|---|
| 279 | n/a | if deep: |
|---|
| 280 | n/a | state = deepcopy(state, memo) |
|---|
| 281 | n/a | if hasattr(y, '__setstate__'): |
|---|
| 282 | n/a | y.__setstate__(state) |
|---|
| 283 | n/a | else: |
|---|
| 284 | n/a | if isinstance(state, tuple) and len(state) == 2: |
|---|
| 285 | n/a | state, slotstate = state |
|---|
| 286 | n/a | else: |
|---|
| 287 | n/a | slotstate = None |
|---|
| 288 | n/a | if state is not None: |
|---|
| 289 | n/a | y.__dict__.update(state) |
|---|
| 290 | n/a | if slotstate is not None: |
|---|
| 291 | n/a | for key, value in slotstate.items(): |
|---|
| 292 | n/a | setattr(y, key, value) |
|---|
| 293 | n/a | |
|---|
| 294 | n/a | if listiter is not None: |
|---|
| 295 | n/a | if deep: |
|---|
| 296 | n/a | for item in listiter: |
|---|
| 297 | n/a | item = deepcopy(item, memo) |
|---|
| 298 | n/a | y.append(item) |
|---|
| 299 | n/a | else: |
|---|
| 300 | n/a | for item in listiter: |
|---|
| 301 | n/a | y.append(item) |
|---|
| 302 | n/a | if dictiter is not None: |
|---|
| 303 | n/a | if deep: |
|---|
| 304 | n/a | for key, value in dictiter: |
|---|
| 305 | n/a | key = deepcopy(key, memo) |
|---|
| 306 | n/a | value = deepcopy(value, memo) |
|---|
| 307 | n/a | y[key] = value |
|---|
| 308 | n/a | else: |
|---|
| 309 | n/a | for key, value in dictiter: |
|---|
| 310 | n/a | y[key] = value |
|---|
| 311 | n/a | return y |
|---|
| 312 | n/a | |
|---|
| 313 | n/a | del types, weakref, PyStringMap |
|---|