FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-141510, PEP 814: Add frozendict support to pickle by vstinner · Pull Request #144967 · python/cpython · GitHub

/ cpython Public

gh-141510, PEP 814: Add frozendict support to pickle - #144967

Merged
vstinner merged 10 commits into
python:mainfrom
vstinner:frozendict_pickle2
Feb 21, 2026
Merged

gh-141510, PEP 814: Add frozendict support to pickle#144967
vstinner merged 10 commits into
python:mainfrom
vstinner:frozendict_pickle2

Conversation

vstinner commented Feb 18, 2026
edited
Loading

Copy link
Copy Markdown
Member

Add frozendict.__getnewargs__() method.

Comment thread Lib/test/picklecommon.py Outdated
Comment thread Lib/test/pickletester.py Outdated
# make sure that floats are formatted locale independent with proto 0
self.assertEqual(self.dumps(1.2, 0)[0:3], b'F1.')

def test_frozendict(self):

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Would not be better to add this test in test_frozendict.py? Together with tests for copy() and deepcopy()?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Ok, I moved this test to test_pickle.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Together with tests for copy() and deepcopy()?

Commit dd64e42, which adds frozendict support to the copy module, added frozendict tests to test_copy.

Copy link
Copy Markdown
Member Author

@serhiy-storchaka: I addressed your review. Please review the updated PR.

serhiy-storchaka left a comment

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

What about frozendict views and iterators? Are they copyable/pickleable?

Are there tests for deepcopying?

I am surprised that there is no separate test_frozendict.py.

Comment thread Lib/test/test_dict.py
Comment thread Lib/test/test_dict.py Outdated

Copy link
Copy Markdown
Member

Oh, I don't think deepcopy of frozendict is correct. It does not work for recursive frozendict.

Comment thread Lib/test/pickletester.py

Copy link
Copy Markdown
Member Author

I will try to update my PR later to address other comments.

What about frozendict views and iterators? Are they copyable/pickleable?

keys, values and items views cannot be copied nor serialized by pickle.

Ah, it seems like it's possible to serialize a frozendict iterator.

Are there tests for deepcopying?

test_copy.test_deepcopy_frozendict() tests frozendict deepcopy.

I am surprised that there is no separate test_frozendict.py.

Ah. It was simple to add frozendict tests to existing test_dict. Maybe we can create test_frozendict later once we will add more tests.

Comment thread Lib/test/test_dict.py

Copy link
Copy Markdown
Member Author

Oh, I don't think deepcopy of frozendict is correct. It does not work for recursive frozendict.

You're right, the current copy.deepcopy() implementation doesn't work for recursive frozendict.

Copy link
Copy Markdown
Member Author

I completed the PR to add requested tests.

Copy link
Copy Markdown
Member Author

You're right, the current copy.deepcopy() implementation doesn't work for recursive frozendict.

I created #145027 to fix copy.deepcopy() for recursive frozendict.

Comment thread Lib/test/test_dict.py Outdated
pickle.dumps(fd, proto)

def test_pickle_iter(self):
it = iter(frozendict(x=1, y=2))

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

What about value iterator and item iterator?

Consume one item from the iterator, to ensure that it correctly restores its state. See pickling tests in test_ordered_dict for example.

If things are pickleable, they should also be deepcopyable. It is worth to have explicit deepcopy tests, because they can preserve additional invariants. For example, it should be possible to deepcopy a frozendict containing lambdas or modules.

Comment thread Lib/test/pickletester.py Outdated
Comment thread Lib/test/pickletester.py Outdated
Comment thread Lib/test/pickletester.py Outdated

Copy link
Copy Markdown
Member Author

If things are pickleable, they should also be deepcopyable. It is worth to have explicit deepcopy tests, because they can preserve additional invariants. For example, it should be possible to deepcopy a frozendict containing lambdas or modules.

I propose merging this PR first (once it will be approved), and then adding such tests to #145027 PR.

Copy link
Copy Markdown
Member

Did you forget to push changes for comments which you marked resolved?

Copy link
Copy Markdown
Member Author

Ooops, you're correct. I forgot to push my changes, but I also removed my local branch... I had to rewrite my changes. I just pushed them. I should be ok now.

serhiy-storchaka left a comment

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM. 👍

vstinner merged commit 20b1535 into python:main Feb 21, 2026
49 checks passed
vstinner deleted the frozendict_pickle2 branch February 21, 2026 10:07

Copy link
Copy Markdown
Member Author

Merged. Thanks for your great review @serhiy-storchaka, it was very useful!

Copy link
Copy Markdown
Member

This broke Oddballs (i.e. text_xpickle): https://buildbot.python.org/#/builders/1868/builds/5

Copy link
Copy Markdown
Member Author

This broke Oddballs (i.e. text_xpickle): https://buildbot.python.org/#/builders/1868/builds/5

Oh, I'm not used to test_xpickle yet, I forgot about it. I wrote #145069 to fix test_xpickle.

brijkapadia pushed a commit to brijkapadia/cpython that referenced this pull request Feb 28, 2026
ljfp pushed a commit to ljfp/cpython that referenced this pull request Apr 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL