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

Function deepcopy

Lib/copy.py:110–163  ·  view source on GitHub ↗

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

(x, memo=None)

Source from the content-addressed store, hash-verified

108_copy_builtin_containers = frozenset({list, dict, set, bytearray})
109
110def deepcopy(x, memo=None):
111 """Deep copy operation on arbitrary Python objects.
112
113 See the module's __doc__ string for more info.
114 """
115
116 cls = type(x)
117
118 if cls in _atomic_types:
119 return x
120
121 d = id(x)
122 if memo is None:
123 memo = {}
124 else:
125 y = memo.get(d, None)
126 if y is not None:
127 return y
128
129 copier = _deepcopy_dispatch.get(cls)
130 if copier is not None:
131 y = copier(x, memo)
132 else:
133 if issubclass(cls, type):
134 y = x # atomic copy
135 else:
136 copier = getattr(x, "__deepcopy__", None)
137 if copier is not None:
138 y = copier(memo)
139 else:
140 reductor = dispatch_table.get(cls)
141 if reductor:
142 rv = reductor(x)
143 else:
144 reductor = getattr(x, "__reduce_ex__", None)
145 if reductor is not None:
146 rv = reductor(4)
147 else:
148 reductor = getattr(x, "__reduce__", None)
149 if reductor:
150 rv = reductor()
151 else:
152 raise Error(
153 "un(deep)copyable object of type %s" % cls)
154 if isinstance(rv, str):
155 y = x
156 else:
157 y = _reconstruct(x, memo, *rv)
158
159 # If is its own copy, don't memoize.
160 if y is not x:
161 memo[d] = y
162 _keep_alive(x, memo) # Make sure x lives at least as long as d
163 return y
164
165_atomic_types = frozenset({types.NoneType, types.EllipsisType, types.NotImplementedType,
166 int, float, bool, complex, bytes, str, types.CodeType, type, range,

Callers 15

cloneMethod · 0.90
__deepcopy__Method · 0.90
__deepcopy__Method · 0.90
_writeMethod · 0.90
_handle_textMethod · 0.90
setop_testMethod · 0.90
set2op_testMethod · 0.90
setsliceop_testMethod · 0.90
test_reduce_copyingMethod · 0.90

Calls 5

idFunction · 0.85
_reconstructFunction · 0.85
_keep_aliveFunction · 0.85
ErrorClass · 0.70
getMethod · 0.45

Tested by 14

setop_testMethod · 0.72
set2op_testMethod · 0.72
setsliceop_testMethod · 0.72
test_reduce_copyingMethod · 0.72
test_copy_and_pickleMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…