Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
10 changes: 8 additions & 2 deletions Lib/copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def copy(x):


_copy_atomic_types = frozenset({types.NoneType, int, float, bool, complex, str, tuple,
bytes, frozenset, type, range, slice, property,
bytes, frozendict, frozenset, type, range, slice, property,
types.BuiltinFunctionType, types.EllipsisType,
types.NotImplementedType, types.FunctionType, types.CodeType,
weakref.ref, super})
Expand Down Expand Up @@ -166,7 +166,7 @@ def deepcopy(x, memo=None):
int, float, bool, complex, bytes, str, types.CodeType, type, range,
types.BuiltinFunctionType, types.FunctionType, weakref.ref, property})

_deepcopy_dispatch = d = {}
d = {}


def _deepcopy_list(x, memo, deepcopy=deepcopy):
Expand Down Expand Up @@ -203,10 +203,16 @@ def _deepcopy_dict(x, memo, deepcopy=deepcopy):
return y
d[dict] = _deepcopy_dict

def _deepcopy_frozendict(x, memo, deepcopy=deepcopy):
y = _deepcopy_dict(x, memo, deepcopy)
return frozendict(y)
d[frozendict] = _deepcopy_frozendict

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

_deepcopy_dispatch = frozendict(d)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to not convert it to a frozendict in this PR, since it's not directly related to supporting frozendict in copy: remove this line and revert test_descr changes.

Copy link
Copy Markdown
Contributor Author

@eendebakpt eendebakpt Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll remove it here. Do you want me to open a separate PR for this part?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote #144909 to use frozendict in multiple stdlib modules. I will to see how this PR goes before proposing changing copy._deepcopy_dispatch to frozendict.

del d

def _keep_alive(x, memo):
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ def test_copy_dict(self):
self.assertEqual(y, x)
self.assertIsNot(y, x)

def test_copy_frozendict(self):
x = frozendict(x=1, y=2)
self.assertIs(copy.copy(x), x)
x = frozendict()
self.assertIs(copy.copy(x), x)

def test_copy_set(self):
x = {1, 2, 3}
y = copy.copy(x)
Expand Down Expand Up @@ -419,6 +425,13 @@ def test_deepcopy_dict(self):
self.assertIsNot(x, y)
self.assertIsNot(x["foo"], y["foo"])

def test_deepcopy_frozendict(self):
x = frozendict({"foo": [1, 2], "bar": 3})
y = copy.deepcopy(x)
self.assertEqual(y, x)
self.assertIsNot(x, y)
self.assertIsNot(x["foo"], y["foo"])

@support.skip_emscripten_stack_overflow()
@support.skip_wasi_stack_overflow()
def test_deepcopy_reflexive_dict(self):
Expand Down
35 changes: 21 additions & 14 deletions Lib/test/test_descr.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,7 @@ def test_spam_lists(self):
# Testing spamlist operations...
import copy, xxsubtype as spam

def spamlist(l, memo=None):
import xxsubtype as spam
return spam.spamlist(l)

# This is an ugly hack:
copy._deepcopy_dispatch[spam.spamlist] = spamlist
spamlist = spam.spamlist
Comment thread
eendebakpt marked this conversation as resolved.
Outdated

self.binop_test(spamlist([1]), spamlist([2]), spamlist([1,2]), "a+b",
"__add__")
Expand Down Expand Up @@ -350,19 +345,22 @@ def foo(self): return 1
a.setstate(42)
self.assertEqual(a.getstate(), 42)

# test deepcopy makes a deep copy of the elements of a spamlist
a.append([])
a_copy = copy.deepcopy(a)
assert a_copy is not a
assert a_copy[-1] is not a[-1]
assert a_copy == a
# the spamlist does not explictly implement deepcopy, the state is not copied
assert a_copy.getstate() != a.getstate()

@support.impl_detail("the module 'xxsubtype' is internal")
@unittest.skipIf(xxsubtype is None, "requires xxsubtype module")
def test_spam_dicts(self):
# Testing spamdict operations...
import copy, xxsubtype as spam
def spamdict(d, memo=None):
import xxsubtype as spam
sd = spam.spamdict()
for k, v in list(d.items()):
sd[k] = v
return sd
# This is an ugly hack:
copy._deepcopy_dispatch[spam.spamdict] = spamdict

spamdict = spam.spamdict

self.binop_test(spamdict({1:2,3:4}), 1, 1, "b in a", "__contains__")
self.binop_test(spamdict({1:2,3:4}), 2, 0, "b in a", "__contains__")
Expand Down Expand Up @@ -401,6 +399,15 @@ def foo(self): return 1
a.setstate(100)
self.assertEqual(a.getstate(), 100)

# test deepcopy makes a deep copy of the elements of a spamdict
a[-1] = []
a_copy = copy.deepcopy(a)
assert a_copy is not a
assert a_copy[-1] is not a[-1]
assert a_copy == a
# the spamdict does not explictly implement deepcopy, the state is not copied
assert a_copy.getstate() != a.getstate()

def test_wrap_lenfunc_bad_cast(self):
self.assertEqual(range(sys.maxsize).__len__(), sys.maxsize)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The :mod:`copy` module now supports the :class:`frozendict` type. Patch by
Pieter Eendebak based on work by Victor Stinner.
Loading