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.

classification
Title: MutableSet.__iand__ implementation calls self.discard while iterating on self
Type: Stage:
Components: Library (Lib) Versions: Python 3.0, Python 3.1, Python 2.7, Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: della, rhettinger
Priority: normal Keywords:

Created on 2009-04-01 13:43 by della, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
simpleset.py della, 2009-04-01 13:43
Messages (3)
msg85006 - (view) Author: Matteo Dell'Amico (della) Date: 2009-04-01 13:43
The current MutableSet.__iand__ implementation calls self.discard while
iterating on self. This creates strange problems while implementing
MutableSet with simple choices. For example, consider the attached file
which implements set by delegating either to a set or a list. In the
first cases, an exception is raised; in the second, the result is not
what is expected.

Python 2.6+ (r26:66714, Oct 22 2008, 09:21:39) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from simpleset import WithSet, WithList
>>> s = WithSet([1,2])
>>> s &= ()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/_abcoll.py", line 290, in __iand__
    for value in self:
RuntimeError: Set changed size during iteration
>>> s = WithList([1,2])
>>> s &= ()
>>> list(s)
[2]
msg85008 - (view) Author: Matteo Dell'Amico (della) Date: 2009-04-01 13:45
I suggest solving the problem by changing the implementation to:

def __iand__(self, c):
    self -= self - c:

or to

def __iand__(self, c):
    for item in self - c:
        self.discard(item)
msg85077 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-04-01 19:06
Thanks for the report.  Fixed in r70969, r70970, r70973, and r70974.
History
Date User Action Args
2022-04-11 14:56:47adminsetgithub: 49897
2009-04-01 19:06:15rhettingersetstatus: open -> closed
resolution: fixed
messages: + msg85077
2009-04-01 14:03:43georg.brandlsetassignee: rhettinger

nosy: + rhettinger
2009-04-01 13:45:33dellasetmessages: + msg85008
2009-04-01 13:43:20dellacreate