@@ -121,6 +121,11 @@ def deepcopy(x, memo=None, _nil=[]):
121121 See the module's __doc__ string for more info.
122122 """
123123
124+ cls = type (x )
125+
126+ if cls in _atomic_types :
127+ return x
128+
124129 d = id (x )
125130 if memo is None :
126131 memo = {}
@@ -129,14 +134,12 @@ def deepcopy(x, memo=None, _nil=[]):
129134 if y is not _nil :
130135 return y
131136
132- cls = type (x )
133-
134137 copier = _deepcopy_dispatch .get (cls )
135138 if copier is not None :
136139 y = copier (x , memo )
137140 else :
138141 if issubclass (cls , type ):
139- y = _deepcopy_atomic ( x , memo )
142+ y = x # atomic copy
140143 else :
141144 copier = getattr (x , "__deepcopy__" , None )
142145 if copier is not None :
@@ -167,26 +170,12 @@ def deepcopy(x, memo=None, _nil=[]):
167170 _keep_alive (x , memo ) # Make sure x lives at least as long as d
168171 return y
169172
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+
170177_deepcopy_dispatch = d = {}
171178
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
190179
191180def _deepcopy_list (x , memo , deepcopy = deepcopy ):
192181 y = []
0 commit comments