diff --git a/Lib/copy.py b/Lib/copy.py index c64fc0761793f5..fff7e93c2a1b89 100644 --- a/Lib/copy.py +++ b/Lib/copy.py @@ -100,14 +100,14 @@ def copy(x): return _reconstruct(x, None, *rv) -_copy_atomic_types = {types.NoneType, int, float, bool, complex, str, tuple, +_copy_atomic_types = frozenset({types.NoneType, int, float, bool, complex, str, tuple, bytes, frozenset, type, range, slice, property, types.BuiltinFunctionType, types.EllipsisType, types.NotImplementedType, types.FunctionType, types.CodeType, - weakref.ref, super} -_copy_builtin_containers = {list, dict, set, bytearray} + weakref.ref, super}) +_copy_builtin_containers = frozenset({list, dict, set, bytearray}) -def deepcopy(x, memo=None, _nil=[]): +def deepcopy(x, memo=None): """Deep copy operation on arbitrary Python objects. See the module's __doc__ string for more info. @@ -122,8 +122,8 @@ def deepcopy(x, memo=None, _nil=[]): if memo is None: memo = {} else: - y = memo.get(d, _nil) - if y is not _nil: + y = memo.get(d, None) + if y is not None: return y copier = _deepcopy_dispatch.get(cls) @@ -162,9 +162,9 @@ def deepcopy(x, memo=None, _nil=[]): _keep_alive(x, memo) # Make sure x lives at least as long as d return y -_atomic_types = {types.NoneType, types.EllipsisType, types.NotImplementedType, +_atomic_types = frozenset({types.NoneType, types.EllipsisType, types.NotImplementedType, int, float, bool, complex, bytes, str, types.CodeType, type, range, - types.BuiltinFunctionType, types.FunctionType, weakref.ref, property} + types.BuiltinFunctionType, types.FunctionType, weakref.ref, property}) _deepcopy_dispatch = d = {} diff --git a/Misc/NEWS.d/next/Library/2025-09-03-09-03-11.gh-issue-132657.cbAIDh.rst b/Misc/NEWS.d/next/Library/2025-09-03-09-03-11.gh-issue-132657.cbAIDh.rst new file mode 100644 index 00000000000000..8f07f8fc8d1372 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-09-03-09-03-11.gh-issue-132657.cbAIDh.rst @@ -0,0 +1 @@ +Improve the scaling of :func:`copy.copy` and :func:`copy.deepcopy` in the free-threading build.