Fix copy.copy() sharing internal list between original and copy#743
Open
veeceey wants to merge 4 commits intoaio-libs:masterfrom
Open
Fix copy.copy() sharing internal list between original and copy#743veeceey wants to merge 4 commits intoaio-libs:masterfrom
veeceey wants to merge 4 commits intoaio-libs:masterfrom
Conversation
veeceey
added a commit
to veeceey/frozenlist
that referenced
this pull request
Feb 10, 2026
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #743 +/- ##
===========================================
+ Coverage 94.90% 100.00% +5.09%
===========================================
Files 10 2 -8
Lines 726 370 -356
Branches 48 26 -22
===========================================
- Hits 689 370 -319
+ Misses 13 0 -13
+ Partials 24 0 -24
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Author
|
Friendly ping - any chance someone could take a look at this when they get a chance? Happy to make any changes if needed. |
copy.copy() on a FrozenList produced a shallow copy that shared the same underlying _items list object, causing mutations to one to affect the other. This violates the contract of copy.copy() for mutable containers (e.g. copy.copy() on a regular list correctly creates an independent list). Implement __copy__ in both the Python and Cython implementations to create a new FrozenList with a copied _items list while preserving the frozen state. Also add the method to the type stubs and add tests.
Python 3.14 added __annotations_cache__ to MutableSequence, which the Cython extension class does not implement. Add it to the SKIP_METHODS set alongside the other CPython internals already skipped there.
834cc68 to
0a353e2
Compare
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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
copy.copy()on aFrozenListproduced a broken shallow copy that shared the same underlying_itemslist with the original. Mutating either the original or the copy would affect both, violating the contract ofcopy.copy()for mutable containers.__copy__in both the Python and Cython (.pyx) implementations to create a properly independent shallow copy that preserves the frozen state.__copy__signature to the.pyitype stubs.Details
Bug reproduction (before this fix):
Expected behavior (after this fix):
The root cause is that
FrozenListuses__slots__, so the defaultcopy.copy()mechanism copies slot values by reference. Since_itemsis alist, both the original and the copy end up pointing to the exact same list object. The fix implements__copy__which passesself._itemsto the constructor, which callslist(items)and thus creates a new list.For comparison,
copy.copy()on a standardlistcorrectly produces an independent copy. The__deepcopy__method was already implemented (PR #662), but__copy__was missed.Test plan
test_copy_unfrozenverifies that mutating the original does not affect the copytest_copy_frozenverifies frozen state is preserved in the copytest_copy_preserves_itemsverifies shallow-copy semantics (inner objects shared, but containers independent)