File tree 3 files changed +44
-3
lines changed
3 files changed +44
-3
lines changed Original file line number Diff line number Diff line change @@ -1680,6 +1680,44 @@ async def foo():
1680
1680
warnings .simplefilter ("error" )
1681
1681
run_async (foo ())
1682
1682
1683
+ def test_for_11 (self ):
1684
+ class F :
1685
+ def __aiter__ (self ):
1686
+ return self
1687
+ def __anext__ (self ):
1688
+ return self
1689
+ def __await__ (self ):
1690
+ 1 / 0
1691
+
1692
+ async def main ():
1693
+ async for _ in F ():
1694
+ pass
1695
+
1696
+ with self .assertRaisesRegex (TypeError ,
1697
+ 'an invalid object from __anext__' ) as c :
1698
+ main ().send (None )
1699
+
1700
+ err = c .exception
1701
+ self .assertIsInstance (err .__cause__ , ZeroDivisionError )
1702
+
1703
+ def test_for_12 (self ):
1704
+ class F :
1705
+ def __aiter__ (self ):
1706
+ return self
1707
+ def __await__ (self ):
1708
+ 1 / 0
1709
+
1710
+ async def main ():
1711
+ async for _ in F ():
1712
+ pass
1713
+
1714
+ with self .assertRaisesRegex (TypeError ,
1715
+ 'an invalid object from __aiter__' ) as c :
1716
+ main ().send (None )
1717
+
1718
+ err = c .exception
1719
+ self .assertIsInstance (err .__cause__ , ZeroDivisionError )
1720
+
1683
1721
def test_for_tuple (self ):
1684
1722
class Done (Exception ): pass
1685
1723
Original file line number Diff line number Diff line change @@ -10,6 +10,9 @@ What's New in Python 3.6.1 release candidate 1?
10
10
Core and Builtins
11
11
-----------------
12
12
13
+ - bpo-28893: Set correct __cause__ for errors about invalid awaitables
14
+ returned from __aiter__ and __anext__.
15
+
13
16
- bpo-29683: Fixes to memory allocation in _PyCode_SetExtra. Patch by
14
17
Brian Coleman.
15
18
Original file line number Diff line number Diff line change @@ -1904,13 +1904,13 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
1904
1904
1905
1905
awaitable = _PyCoro_GetAwaitableIter (iter );
1906
1906
if (awaitable == NULL ) {
1907
- SET_TOP (NULL );
1908
- PyErr_Format (
1907
+ _PyErr_FormatFromCause (
1909
1908
PyExc_TypeError ,
1910
1909
"'async for' received an invalid object "
1911
1910
"from __aiter__: %.100s" ,
1912
1911
Py_TYPE (iter )-> tp_name );
1913
1912
1913
+ SET_TOP (NULL );
1914
1914
Py_DECREF (iter );
1915
1915
goto error ;
1916
1916
} else {
@@ -1969,7 +1969,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
1969
1969
1970
1970
awaitable = _PyCoro_GetAwaitableIter (next_iter );
1971
1971
if (awaitable == NULL ) {
1972
- PyErr_Format (
1972
+ _PyErr_FormatFromCause (
1973
1973
PyExc_TypeError ,
1974
1974
"'async for' received an invalid object "
1975
1975
"from __anext__: %.100s" ,
You can’t perform that action at this time.
0 commit comments