[New-bugs-announce] [issue40824] Unexpected errors in __iter__ are masked in "in" and the operator module

Serhiy Storchaka report at bugs.python.org
Sat May 30 07:36:12 EDT 2020


New submission from Serhiy Storchaka <storchaka+cpython at gmail.com>:

All errors raised in the __iter__ method are masked by the TypeError exception in the "in" operator and functions operator.contains(), operator.indexOf() and operator.countOf().

>>> class BadIterable:
...     def __iter__(self):
...         1/0
... 
>>> 
>>> 1 in BadIterable()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument of type 'BadIterable' is not iterable

It includes exceptions out of control of the programmer like MemoryError and KeyboardInterrupt. Converting them to TypeError can lead to weird errors or incorrect results.

See also similar issue26407.

----------
components: Interpreter Core
messages: 370372
nosy: serhiy.storchaka
priority: normal
severity: normal
status: open
title: Unexpected errors in __iter__ are masked in "in" and the operator module
type: behavior
versions: Python 3.10, Python 3.6, Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue40824>
_______________________________________


More information about the New-bugs-announce mailing list