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