Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 'Dictionary changed during iteration' exception raising #628

Merged
merged 3 commits into from
Sep 30, 2021
Merged
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
1 change: 1 addition & 0 deletions CHANGES/620.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix pure-python implementation that used to raise "Dictionary changed during iteration" error when iterated view (``.keys()``, ``.values()`` or ``.items()``) was created before the dictionary's content change.
19 changes: 9 additions & 10 deletions multidict/_multidict_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,6 @@ def __length_hint__(self):
class _ViewBase:
def __init__(self, impl):
self._impl = impl
self._version = impl._version

def __len__(self):
return len(self._impl._items)
Expand All @@ -451,11 +450,11 @@ def __contains__(self, item):
return False

def __iter__(self):
return _Iter(len(self), self._iter())
return _Iter(len(self), self._iter(self._impl._version))

def _iter(self):
def _iter(self, version):
for i, k, v in self._impl._items:
if self._version != self._impl._version:
if version != self._impl._version:
raise RuntimeError("Dictionary changed during iteration")
yield k, v

Expand All @@ -475,11 +474,11 @@ def __contains__(self, value):
return False

def __iter__(self):
return _Iter(len(self), self._iter())
return _Iter(len(self), self._iter(self._impl._version))

def _iter(self):
def _iter(self, version):
for item in self._impl._items:
if self._version != self._impl._version:
if version != self._impl._version:
raise RuntimeError("Dictionary changed during iteration")
yield item[2]

Expand All @@ -499,11 +498,11 @@ def __contains__(self, key):
return False

def __iter__(self):
return _Iter(len(self), self._iter())
return _Iter(len(self), self._iter(self._impl._version))

def _iter(self):
def _iter(self, version):
for item in self._impl._items:
if self._version != self._impl._version:
if version != self._impl._version:
raise RuntimeError("Dictionary changed during iteration")
yield item[1]

Expand Down
24 changes: 24 additions & 0 deletions tests/test_mutable_multidict.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,27 @@ def test_sizeof(self, cls):
def test_min_sizeof(self, cls):
md = cls()
assert sys.getsizeof(md) < 1024

def test_issue_620_items(self, cls):
# https://github.com/aio-libs/multidict/issues/620
d = cls({"a": "123, 456", "b": "789"})
before_mutation_items = d.items()
d["c"] = "000"
# This causes an error on pypy.
list(before_mutation_items)

def test_issue_620_keys(self, cls):
# https://github.com/aio-libs/multidict/issues/620
d = cls({"a": "123, 456", "b": "789"})
before_mutation_keys = d.keys()
d["c"] = "000"
# This causes an error on pypy.
list(before_mutation_keys)

def test_issue_620_values(self, cls):
# https://github.com/aio-libs/multidict/issues/620
d = cls({"a": "123, 456", "b": "789"})
before_mutation_values = d.values()
d["c"] = "000"
# This causes an error on pypy.
list(before_mutation_values)