Skip to content
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions CHANGES/743.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed ``copy.copy()`` producing a broken shallow copy that shared
the internal items list with the original ``FrozenList``, causing
mutations to one to affect the other -- by :user:`veeceey`.
6 changes: 6 additions & 0 deletions frozenlist/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ def __hash__(self):
else:
raise RuntimeError("Cannot hash unfrozen list.")

def __copy__(self):
new_list = self.__class__(self._items)
if self._frozen:
new_list.freeze()
return new_list


PyFrozenList = FrozenList

Expand Down
1 change: 1 addition & 0 deletions frozenlist/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class FrozenList(MutableSequence[_T], Generic[_T]):
def insert(self, pos: int, item: _T) -> None: ...
def __repr__(self) -> str: ...
def __hash__(self) -> int: ...
def __copy__(self) -> FrozenList[_T]: ...

# types for C accelerators are the same
CFrozenList = PyFrozenList = FrozenList
7 changes: 7 additions & 0 deletions frozenlist/_frozenlist.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ cdef class FrozenList:
else:
raise RuntimeError("Cannot hash unfrozen list.")

def __copy__(self):
cdef FrozenList new_list
new_list = self.__class__(self._items)
if self._frozen.load():
new_list.freeze()
return new_list

def __deepcopy__(self, memo):
cdef FrozenList new_list
obj_id = id(self)
Expand Down
38 changes: 37 additions & 1 deletion tests/test_frozenlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# mypy: disable-error-code="misc"

from collections.abc import MutableSequence
from copy import deepcopy
from copy import copy, deepcopy

import pytest

Expand All @@ -17,6 +17,8 @@ class FrozenListMixin:
"__slots__",
"__static_attributes__",
"__firstlineno__",
"__annotations_cache__",
"__annotate_func__",
}

def test___class_getitem__(self) -> None:
Expand Down Expand Up @@ -249,6 +251,40 @@ def test_count(self) -> None:
_list = self.FrozenList([1, 2])
assert _list.count(1) == 1

def test_copy_unfrozen(self) -> None:
orig = self.FrozenList([1, 2, 3])
copied = copy(orig)
assert copied == orig
assert copied is not orig
assert not copied.frozen
# Verify the copy has independent storage
orig.append(4)
assert len(orig) == 4
assert len(copied) == 3

def test_copy_frozen(self) -> None:
orig = self.FrozenList([1, 2, 3])
orig.freeze()
copied = copy(orig)
assert copied == orig
assert copied is not orig
assert copied.frozen
# Verify the copy is also frozen
with pytest.raises(RuntimeError):
copied.append(4)

def test_copy_preserves_items(self) -> None:
inner = [1, 2]
orig = self.FrozenList([inner, 3])
copied = copy(orig)
# Shallow copy: inner objects are shared (same as list behavior)
assert copied[0] is orig[0]
# But the FrozenList containers are independent
assert copied is not orig
orig.append(4)
assert len(orig) == 3
assert len(copied) == 2

def test_deepcopy_unfrozen(self) -> None:
orig = self.FrozenList([1, 2, 3])
copied = deepcopy(orig)
Expand Down
Loading