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.7.0 alpha 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 @@ -1855,13 +1855,13 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
1855
1855
1856
1856
awaitable = _PyCoro_GetAwaitableIter (iter );
1857
1857
if (awaitable == NULL ) {
1858
- SET_TOP (NULL );
1859
- PyErr_Format (
1858
+ _PyErr_FormatFromCause (
1860
1859
PyExc_TypeError ,
1861
1860
"'async for' received an invalid object "
1862
1861
"from __aiter__: %.100s" ,
1863
1862
Py_TYPE (iter )-> tp_name );
1864
1863
1864
+ SET_TOP (NULL );
1865
1865
Py_DECREF (iter );
1866
1866
goto error ;
1867
1867
} else {
@@ -1920,7 +1920,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
1920
1920
1921
1921
awaitable = _PyCoro_GetAwaitableIter (next_iter );
1922
1922
if (awaitable == NULL ) {
1923
- PyErr_Format (
1923
+ _PyErr_FormatFromCause (
1924
1924
PyExc_TypeError ,
1925
1925
"'async for' received an invalid object "
1926
1926
"from __anext__: %.100s" ,
You can’t perform that action at this time.
0 commit comments