Shallow copy operation on arbitrary Python objects. See the module's __doc__ string for more info.
(x)
| 64 | __all__ = ["Error", "copy", "deepcopy"] |
| 65 | |
| 66 | def copy(x): |
| 67 | """Shallow copy operation on arbitrary Python objects. |
| 68 | |
| 69 | See the module's __doc__ string for more info. |
| 70 | """ |
| 71 | |
| 72 | cls = type(x) |
| 73 | |
| 74 | copier = _copy_dispatch.get(cls) |
| 75 | if copier: |
| 76 | return copier(x) |
| 77 | |
| 78 | if issubclass(cls, type): |
| 79 | # treat it as a regular class: |
| 80 | return _copy_immutable(x) |
| 81 | |
| 82 | copier = getattr(cls, "__copy__", None) |
| 83 | if copier is not None: |
| 84 | return copier(x) |
| 85 | |
| 86 | reductor = dispatch_table.get(cls) |
| 87 | if reductor is not None: |
| 88 | rv = reductor(x) |
| 89 | else: |
| 90 | reductor = getattr(x, "__reduce_ex__", None) |
| 91 | if reductor is not None: |
| 92 | rv = reductor(4) |
| 93 | else: |
| 94 | reductor = getattr(x, "__reduce__", None) |
| 95 | if reductor: |
| 96 | rv = reductor() |
| 97 | else: |
| 98 | raise Error("un(shallow)copyable object of type %s" % cls) |
| 99 | |
| 100 | if isinstance(rv, str): |
| 101 | return x |
| 102 | return _reconstruct(x, None, *rv) |
| 103 | |
| 104 | |
| 105 | _copy_dispatch = d = {} |