MCPcopy Index your code
hub / github.com/python/cpython / copy

Function copy

Lib/copy.py:62–100  ·  view source on GitHub ↗

Shallow copy operation on arbitrary Python objects. See the module's __doc__ string for more info.

(x)

Source from the content-addressed store, hash-verified

60__all__ = ["Error", "copy", "deepcopy", "replace"]
61
62def 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,

Callers 10

test_copy_and_pickleMethod · 0.90
setUpMethod · 0.90
setUpMethod · 0.90
copy_loopMethod · 0.90
RestrictedDecimalFunction · 0.90

Calls 4

_reconstructFunction · 0.85
ErrorClass · 0.70
copyMethod · 0.45
getMethod · 0.45

Tested by 9

test_copy_and_pickleMethod · 0.72
setUpMethod · 0.72
setUpMethod · 0.72
copy_loopMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…