Refactor the copy dispatcher code in copy.py. Simplifies and shortens
the code by grouping common cases together.
This commit is contained in:
parent
99842b6534
commit
f0e3569a28
48
Lib/copy.py
48
Lib/copy.py
|
@ -97,44 +97,26 @@ def copy(x):
|
||||||
|
|
||||||
_copy_dispatch = d = {}
|
_copy_dispatch = d = {}
|
||||||
|
|
||||||
def _copy_atomic(x):
|
def _copy_immutable(x):
|
||||||
return x
|
return x
|
||||||
d[types.NoneType] = _copy_atomic
|
for t in (types.NoneType, int, long, float, bool, str, tuple,
|
||||||
d[types.IntType] = _copy_atomic
|
frozenset, type, xrange, types.ClassType,
|
||||||
d[types.LongType] = _copy_atomic
|
types.BuiltinFunctionType):
|
||||||
d[types.FloatType] = _copy_atomic
|
d[t] = _copy_immutable
|
||||||
d[types.BooleanType] = _copy_atomic
|
for name in ("ComplexType", "UnicodeType", "CodeType"):
|
||||||
try:
|
t = getattr(types, name, None)
|
||||||
d[types.ComplexType] = _copy_atomic
|
if t is not None:
|
||||||
except AttributeError:
|
d[t] = _copy_immutable
|
||||||
pass
|
|
||||||
d[types.StringType] = _copy_atomic
|
|
||||||
try:
|
|
||||||
d[types.UnicodeType] = _copy_atomic
|
|
||||||
except AttributeError:
|
|
||||||
pass
|
|
||||||
try:
|
|
||||||
d[types.CodeType] = _copy_atomic
|
|
||||||
except AttributeError:
|
|
||||||
pass
|
|
||||||
d[types.TypeType] = _copy_atomic
|
|
||||||
d[types.XRangeType] = _copy_atomic
|
|
||||||
d[types.ClassType] = _copy_atomic
|
|
||||||
d[types.BuiltinFunctionType] = _copy_atomic
|
|
||||||
|
|
||||||
def _copy_list(x):
|
def _copy_with_constructor(x):
|
||||||
return x[:]
|
return type(x)(x)
|
||||||
d[types.ListType] = _copy_list
|
for t in (list, dict, set):
|
||||||
|
d[t] = _copy_with_constructor
|
||||||
|
|
||||||
def _copy_tuple(x):
|
def _copy_with_copy_method(x):
|
||||||
return x[:]
|
|
||||||
d[types.TupleType] = _copy_tuple
|
|
||||||
|
|
||||||
def _copy_dict(x):
|
|
||||||
return x.copy()
|
return x.copy()
|
||||||
d[types.DictionaryType] = _copy_dict
|
|
||||||
if PyStringMap is not None:
|
if PyStringMap is not None:
|
||||||
d[PyStringMap] = _copy_dict
|
d[PyStringMap] = _copy_with_copy_method
|
||||||
|
|
||||||
def _copy_inst(x):
|
def _copy_inst(x):
|
||||||
if hasattr(x, '__copy__'):
|
if hasattr(x, '__copy__'):
|
||||||
|
|
Loading…
Reference in New Issue