classification
Title: Unexpected errors in __iter__ are masked in "in" and the operator module
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: miss-islington, ned.deily, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2020-05-30 11:36 by serhiy.storchaka, last changed 2020-06-24 09:11 by ned.deily. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 20537 merged serhiy.storchaka, 2020-05-30 11:41
PR 21043 merged miss-islington, 2020-06-22 07:43
PR 21044 merged miss-islington, 2020-06-22 07:44
PR 21045 closed miss-islington, 2020-06-22 07:44
PR 21046 closed miss-islington, 2020-06-22 07:44
Messages (6)
msg370372 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-05-30 11:36
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.
msg372039 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-06-22 07:43
New changeset cafe1b6e9d3594a34aba50e872d4198296ffaadf by Serhiy Storchaka in branch 'master':
bpo-40824: Do not mask errors in __iter__ in "in" and the operator module. (GH-20537)
https://github.com/python/cpython/commit/cafe1b6e9d3594a34aba50e872d4198296ffaadf
msg372044 - (view) Author: miss-islington (miss-islington) Date: 2020-06-22 08:21
New changeset 353c4bab7d103cb66391a83f3c27cd75ae13a754 by Miss Islington (bot) in branch '3.9':
bpo-40824: Do not mask errors in __iter__ in "in" and the operator module. (GH-20537)
https://github.com/python/cpython/commit/353c4bab7d103cb66391a83f3c27cd75ae13a754
msg372045 - (view) Author: miss-islington (miss-islington) Date: 2020-06-22 08:21
New changeset b99824a8e14d94c3c5c29499a08fe70deb477d0c by Miss Islington (bot) in branch '3.8':
bpo-40824: Do not mask errors in __iter__ in "in" and the operator module. (GH-20537)
https://github.com/python/cpython/commit/b99824a8e14d94c3c5c29499a08fe70deb477d0c
msg372238 - (view) Author: Ned Deily (ned.deily) * (Python committer) Date: 2020-06-24 07:36
@Serhiy, you have opened PRs for this for 3.7 and 3.6, both of which are now in the security-fix phase of their release cycles. It looks like this behavior has been around for a long time and does not appear to be a security issue. Unless there is some important reason why this behavior should be changed in a security fix release (for 3.6) or as a release critical fix for 3.7.8 final and unless another core developer reviews the changes, I do not think the PRs should be merged to 3.7 or 3.6.
msg372240 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-06-24 08:15
Sorry, I was not sure which versions are in security-fix phase now.
History
Date User Action Args
2020-06-24 09:11:43ned.deilysetversions: - Python 3.6, Python 3.7
2020-06-24 08:15:36serhiy.storchakasetmessages: + msg372240
2020-06-24 07:36:53ned.deilysetnosy: + ned.deily
messages: + msg372238
2020-06-22 09:35:14serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-06-22 08:21:14miss-islingtonsetmessages: + msg372045
2020-06-22 08:21:06miss-islingtonsetmessages: + msg372044
2020-06-22 07:44:16miss-islingtonsetpull_requests: + pull_request20217
2020-06-22 07:44:09miss-islingtonsetpull_requests: + pull_request20216
2020-06-22 07:44:01miss-islingtonsetpull_requests: + pull_request20215
2020-06-22 07:43:49miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request20214
2020-06-22 07:43:39serhiy.storchakasetmessages: + msg372039
2020-05-30 11:41:48serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request19780
2020-05-30 11:36:12serhiy.storchakacreate