Skip to content

[3.6] bpo-40824: Do not mask errors in __iter__ in "in" and the operator module. (GH-20537) #21046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Lib/test/test_iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ def __getitem__(self, i):
return i
__iter__ = None

class BadIterableClass:
def __iter__(self):
raise ZeroDivisionError

# Main test suite

class TestCase(unittest.TestCase):
Expand Down Expand Up @@ -637,6 +641,7 @@ def test_in_and_not_in(self):

self.assertRaises(TypeError, lambda: 3 in 12)
self.assertRaises(TypeError, lambda: 3 not in map)
self.assertRaises(ZeroDivisionError, lambda: 3 in BadIterableClass())

d = {"one": 1, "two": 2, "three": 3, 1j: 2j}
for k in d:
Expand Down Expand Up @@ -719,6 +724,7 @@ def test_indexOf(self):

self.assertRaises(TypeError, indexOf, 42, 1)
self.assertRaises(TypeError, indexOf, indexOf, indexOf)
self.assertRaises(ZeroDivisionError, indexOf, BadIterableClass(), 1)

f = open(TESTFN, "w")
try:
Expand Down Expand Up @@ -1006,6 +1012,7 @@ def test_free_after_iterating(self):
def test_error_iter(self):
for typ in (DefaultIterClass, NoIterClass):
self.assertRaises(TypeError, iter, typ())
self.assertRaises(ZeroDivisionError, iter, BadIterableClass())


def test_main():
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def __mul__(self, other):
def __rmul__(self, other):
return other * self.lst

class BadIterable:
def __iter__(self):
raise ZeroDivisionError


class OperatorTestCase:
def test_lt(self):
Expand Down Expand Up @@ -142,6 +146,7 @@ def test_countOf(self):
operator = self.module
self.assertRaises(TypeError, operator.countOf)
self.assertRaises(TypeError, operator.countOf, None, None)
self.assertRaises(ZeroDivisionError, operator.countOf, BadIterable(), 1)
self.assertEqual(operator.countOf([1, 2, 1, 3, 1, 4], 3), 1)
self.assertEqual(operator.countOf([1, 2, 1, 3, 1, 4], 5), 0)

Expand Down Expand Up @@ -176,6 +181,7 @@ def test_indexOf(self):
operator = self.module
self.assertRaises(TypeError, operator.indexOf)
self.assertRaises(TypeError, operator.indexOf, None, None)
self.assertRaises(ZeroDivisionError, operator.indexOf, BadIterable(), 1)
self.assertEqual(operator.indexOf([4, 3, 2, 1], 3), 1)
self.assertRaises(ValueError, operator.indexOf, [4, 3, 2, 1], 0)

Expand Down Expand Up @@ -258,6 +264,7 @@ def test_contains(self):
operator = self.module
self.assertRaises(TypeError, operator.contains)
self.assertRaises(TypeError, operator.contains, None, None)
self.assertRaises(ZeroDivisionError, operator.contains, BadIterable(), 1)
self.assertTrue(operator.contains(range(4), 2))
self.assertFalse(operator.contains(range(4), 5))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Unexpected errors in calling the ``__iter__`` method are no longer masked by
``TypeError`` in the :keyword:`in` operator and functions
:func:`~operator.contains`, :func:`~operator.indexOf` and
:func:`~operator.countOf` of the :mod:`operator` module.
4 changes: 3 additions & 1 deletion Objects/abstract.c
Original file line number Diff line number Diff line change
Expand Up @@ -1924,7 +1924,9 @@ _PySequence_IterSearch(PyObject *seq, PyObject *obj, int operation)

it = PyObject_GetIter(seq);
if (it == NULL) {
type_error("argument of type '%.200s' is not iterable", seq);
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
type_error("argument of type '%.200s' is not iterable", seq);
}
return -1;
}

Expand Down