Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions Lib/copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ class instances).
"pickle" for information on these methods.
"""

import types
import weakref
from copyreg import dispatch_table

class Error(Exception):
pass
def dummy(self): # for define MethodType
pass
error = Error # backward compatibility

try:
Expand Down Expand Up @@ -106,13 +106,16 @@ def copy(x):

def _copy_immutable(x):
return x

CodeType = type(compile("pass", "<string>", "exec"))
BuiltinFunctionType = type(eval)
FunctionType = type(_copy_immutable)
MethodType = type(Error().dummy)

for t in (type(None), int, float, bool, complex, str, tuple,
bytes, frozenset, type, range, slice, property,
types.BuiltinFunctionType, type(Ellipsis), type(NotImplemented),
types.FunctionType, weakref.ref):
d[t] = _copy_immutable
t = getattr(types, "CodeType", None)
if t is not None:
BuiltinFunctionType, type(Ellipsis), type(NotImplemented),
FunctionType, CodeType, weakref.ref):
d[t] = _copy_immutable

d[list] = list.copy
Expand Down Expand Up @@ -190,11 +193,11 @@ def _deepcopy_atomic(x, memo):
d[complex] = _deepcopy_atomic
d[bytes] = _deepcopy_atomic
d[str] = _deepcopy_atomic
d[types.CodeType] = _deepcopy_atomic
d[CodeType] = _deepcopy_atomic
d[type] = _deepcopy_atomic
d[range] = _deepcopy_atomic
d[types.BuiltinFunctionType] = _deepcopy_atomic
d[types.FunctionType] = _deepcopy_atomic
d[BuiltinFunctionType] = _deepcopy_atomic
d[FunctionType] = _deepcopy_atomic
d[weakref.ref] = _deepcopy_atomic
d[property] = _deepcopy_atomic

Expand Down Expand Up @@ -236,7 +239,7 @@ def _deepcopy_dict(x, memo, deepcopy=deepcopy):

def _deepcopy_method(x, memo): # Copy instance methods
return type(x)(x.__func__, deepcopy(x.__self__, memo))
d[types.MethodType] = _deepcopy_method
d[MethodType] = _deepcopy_method

del d

Expand Down Expand Up @@ -301,4 +304,4 @@ def _reconstruct(x, memo, func, args,
y[key] = value
return y

del types, weakref, PyStringMap
del weakref, PyStringMap
Loading