This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: WeakKeyDictionary.__len__ fragile w/ _IterationGuards
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 3.5
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: pitrou, pjenvey, python-dev
Priority: normal Keywords: patch

Created on 2014-04-07 20:17 by pjenvey, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit
issue21173-test.diff pjenvey, 2014-04-07 20:18 review
weakdictlen.patch pitrou, 2014-10-04 21:05
Messages (3)
msg215716 - (view) Author: Philip Jenvey (pjenvey) * (Python committer) Date: 2014-04-07 20:17
len() on WeakKeyDictionarys can fail with ValueErrors when _IterationGuards are kept alive

Attached is a test showing this:

ERROR: test_weak_keys_len_destroy_while_iterating (__main__.MappingTestCase)
Traceback (most recent call last):
  File "Lib/test/", line 1336, in test_weak_keys_len_destroy_while_iterating
    self.assertEqual(len(dict), 0)
ValueError: __len__() should return >= 0

One probably shouldn't keep them alive like this, but __len__ shouldn't be blowing up either. On non ref counting GC platforms this situation is easier to trigger unintentionally
msg228495 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-10-04 21:05
Thank you for reporting this. Here is a patch with tests.
msg228606 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-10-05 18:10
New changeset 657d21b4b121 by Antoine Pitrou in branch '3.4':
Closes #21173: Fix len() on a WeakKeyDictionary when .clear() was called with an iterator alive.

New changeset 27533444b964 by Antoine Pitrou in branch 'default':
Closes #21173: Fix len() on a WeakKeyDictionary when .clear() was called with an iterator alive.
Date User Action Args
2022-04-11 14:58:01adminsetgithub: 65372
2014-10-05 18:10:44python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg228606

resolution: fixed
stage: patch review -> resolved
2014-10-04 21:05:52pitrousetfiles: + weakdictlen.patch

stage: patch review
messages: + msg228495
versions: + Python 3.5, - Python 3.2, Python 3.3
2014-04-07 20:18:22pjenveysetfiles: + issue21173-test.diff
keywords: + patch
2014-04-07 20:17:27pjenveycreate