with self.assertRaises(StopAsyncIteration):
await it.__anext__()
res = await anext(it, ('a', 'b'))
- self.assertEqual(res, ('a', 'b'))
+ self.assertTupleEqual(res, ('a', 'b'))
self.loop.run_until_complete(run())
_PyGen_SetStopIterationValue(PyObject *value)
{
assert(!PyErr_Occurred());
- PyObject *e;
-
- if (value == NULL ||
- (!PyTuple_Check(value) && !PyExceptionInstance_Check(value)))
- {
- /* Delay exception instantiation if we can */
- PyErr_SetObject(PyExc_StopIteration, value);
- return 0;
- }
- /* Construct an exception instance manually with
- * PyObject_CallOneArg and pass it to PyErr_SetObject.
- *
- * We do this to handle a situation when "value" is a tuple, in which
- * case PyErr_SetObject would set the value of StopIteration to
- * the first element of the tuple.
- *
- * (See PyErr_SetObject/_PyErr_CreateException code for details.)
- */
- e = PyObject_CallOneArg(PyExc_StopIteration, value);
- if (e == NULL) {
+ // Construct an exception instance manually with PyObject_CallOneArg()
+ // but use PyErr_SetRaisedException() instead of PyErr_SetObject() as
+ // PyErr_SetObject(exc_type, value) has a fast path when 'value'
+ // is a tuple, where the value of the StopIteration exception would be
+ // set to 'value[0]' instead of 'value'.
+ PyObject *exc = value == NULL
+ ? PyObject_CallNoArgs(PyExc_StopIteration)
+ : PyObject_CallOneArg(PyExc_StopIteration, value);
+ if (exc == NULL) {
return -1;
}
- PyErr_SetObject(PyExc_StopIteration, e);
- Py_DECREF(e);
+ PyErr_SetRaisedException(exc /* stolen */);
return 0;
}