Skip to content

Commit cafe1b6

Browse files
bpo-40824: Do not mask errors in __iter__ in "in" and the operator module. (GH-20537)
Unexpected errors in calling the __iter__ method are no longer masked by TypeError in the "in" operator and functions operator.contains(), operator.indexOf() and operator.countOf().
1 parent 4901ea9 commit cafe1b6

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

Lib/test/test_iter.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ def __getitem__(self, i):
7676
return i
7777
__iter__ = None
7878

79+
class BadIterableClass:
80+
def __iter__(self):
81+
raise ZeroDivisionError
82+
7983
# Main test suite
8084

8185
class TestCase(unittest.TestCase):
@@ -658,6 +662,7 @@ def test_in_and_not_in(self):
658662

659663
self.assertRaises(TypeError, lambda: 3 in 12)
660664
self.assertRaises(TypeError, lambda: 3 not in map)
665+
self.assertRaises(ZeroDivisionError, lambda: 3 in BadIterableClass())
661666

662667
d = {"one": 1, "two": 2, "three": 3, 1j: 2j}
663668
for k in d:
@@ -740,6 +745,7 @@ def test_indexOf(self):
740745

741746
self.assertRaises(TypeError, indexOf, 42, 1)
742747
self.assertRaises(TypeError, indexOf, indexOf, indexOf)
748+
self.assertRaises(ZeroDivisionError, indexOf, BadIterableClass(), 1)
743749

744750
f = open(TESTFN, "w")
745751
try:
@@ -1027,6 +1033,7 @@ def test_free_after_iterating(self):
10271033
def test_error_iter(self):
10281034
for typ in (DefaultIterClass, NoIterClass):
10291035
self.assertRaises(TypeError, iter, typ())
1036+
self.assertRaises(ZeroDivisionError, iter, BadIterableClass())
10301037

10311038

10321039
def test_main():

Lib/test/test_operator.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ def __mul__(self, other):
3535
def __rmul__(self, other):
3636
return other * self.lst
3737

38+
class BadIterable:
39+
def __iter__(self):
40+
raise ZeroDivisionError
41+
3842

3943
class OperatorTestCase:
4044
def test_lt(self):
@@ -142,6 +146,7 @@ def test_countOf(self):
142146
operator = self.module
143147
self.assertRaises(TypeError, operator.countOf)
144148
self.assertRaises(TypeError, operator.countOf, None, None)
149+
self.assertRaises(ZeroDivisionError, operator.countOf, BadIterable(), 1)
145150
self.assertEqual(operator.countOf([1, 2, 1, 3, 1, 4], 3), 1)
146151
self.assertEqual(operator.countOf([1, 2, 1, 3, 1, 4], 5), 0)
147152

@@ -176,6 +181,7 @@ def test_indexOf(self):
176181
operator = self.module
177182
self.assertRaises(TypeError, operator.indexOf)
178183
self.assertRaises(TypeError, operator.indexOf, None, None)
184+
self.assertRaises(ZeroDivisionError, operator.indexOf, BadIterable(), 1)
179185
self.assertEqual(operator.indexOf([4, 3, 2, 1], 3), 1)
180186
self.assertRaises(ValueError, operator.indexOf, [4, 3, 2, 1], 0)
181187

@@ -258,6 +264,7 @@ def test_contains(self):
258264
operator = self.module
259265
self.assertRaises(TypeError, operator.contains)
260266
self.assertRaises(TypeError, operator.contains, None, None)
267+
self.assertRaises(ZeroDivisionError, operator.contains, BadIterable(), 1)
261268
self.assertTrue(operator.contains(range(4), 2))
262269
self.assertFalse(operator.contains(range(4), 5))
263270

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Unexpected errors in calling the ``__iter__`` method are no longer masked by
2+
``TypeError`` in the :keyword:`in` operator and functions
3+
:func:`~operator.contains`, :func:`~operator.indexOf` and
4+
:func:`~operator.countOf` of the :mod:`operator` module.

Objects/abstract.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2083,7 +2083,9 @@ _PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)
20832083

20842084
it = PyObject_GetIter(seq);
20852085
if (it == NULL) {
2086-
type_error("argument of type '%.200s' is not iterable", seq);
2086+
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
2087+
type_error("argument of type '%.200s' is not iterable", seq);
2088+
}
20872089
return -1;
20882090
}
20892091

0 commit comments

Comments
 (0)