@@ -121,6 +121,11 @@ def deepcopy(x, memo=None, _nil=[]):
121
121
See the module's __doc__ string for more info.
122
122
"""
123
123
124
+ cls = type (x )
125
+
126
+ if cls in _atomic_types :
127
+ return x
128
+
124
129
d = id (x )
125
130
if memo is None :
126
131
memo = {}
@@ -129,14 +134,12 @@ def deepcopy(x, memo=None, _nil=[]):
129
134
if y is not _nil :
130
135
return y
131
136
132
- cls = type (x )
133
-
134
137
copier = _deepcopy_dispatch .get (cls )
135
138
if copier is not None :
136
139
y = copier (x , memo )
137
140
else :
138
141
if issubclass (cls , type ):
139
- y = _deepcopy_atomic ( x , memo )
142
+ y = x # atomic copy
140
143
else :
141
144
copier = getattr (x , "__deepcopy__" , None )
142
145
if copier is not None :
@@ -167,26 +170,12 @@ def deepcopy(x, memo=None, _nil=[]):
167
170
_keep_alive (x , memo ) # Make sure x lives at least as long as d
168
171
return y
169
172
173
+ _atomic_types = {types .NoneType , types .EllipsisType , types .NotImplementedType ,
174
+ int , float , bool , complex , bytes , str , types .CodeType , type , range ,
175
+ types .BuiltinFunctionType , types .FunctionType , weakref .ref , property }
176
+
170
177
_deepcopy_dispatch = d = {}
171
178
172
- def _deepcopy_atomic (x , memo ):
173
- return x
174
- d [types .NoneType ] = _deepcopy_atomic
175
- d [types .EllipsisType ] = _deepcopy_atomic
176
- d [types .NotImplementedType ] = _deepcopy_atomic
177
- d [int ] = _deepcopy_atomic
178
- d [float ] = _deepcopy_atomic
179
- d [bool ] = _deepcopy_atomic
180
- d [complex ] = _deepcopy_atomic
181
- d [bytes ] = _deepcopy_atomic
182
- d [str ] = _deepcopy_atomic
183
- d [types .CodeType ] = _deepcopy_atomic
184
- d [type ] = _deepcopy_atomic
185
- d [range ] = _deepcopy_atomic
186
- d [types .BuiltinFunctionType ] = _deepcopy_atomic
187
- d [types .FunctionType ] = _deepcopy_atomic
188
- d [weakref .ref ] = _deepcopy_atomic
189
- d [property ] = _deepcopy_atomic
190
179
191
180
def _deepcopy_list (x , memo , deepcopy = deepcopy ):
192
181
y = []
0 commit comments