FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-28893: Set __cause__ for errors in async iteration protocol by 1st1 · Pull Request #407 · python/cpython · GitHub

Repository navigation

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

Filter by extension

Filter by extension .c  (1) .py  (1) No extension  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
38 changes: 38 additions & 0 deletions Lib/test/test_coroutines.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1680,6 +1680,44 @@ async def foo():
warnings.simplefilter("error")
run_async(foo())

def test_for_11(self):
class F:
def __aiter__(self):
return self
def __anext__(self):
return self
def __await__(self):
1 / 0

async def main():
async for _ in F():
pass

with self.assertRaisesRegex(TypeError,
'an invalid object from __anext__') as c:
main().send(None)

err = c.exception
self.assertIsInstance(err.__cause__, ZeroDivisionError)

def test_for_12(self):
class F:
def __aiter__(self):
return self
def __await__(self):
1 / 0

async def main():
async for _ in F():
pass

with self.assertRaisesRegex(TypeError,
'an invalid object from __aiter__') as c:
main().send(None)

err = c.exception
self.assertIsInstance(err.__cause__, ZeroDivisionError)

def test_for_tuple(self):
class Done(Exception): pass

Expand Down
3 changes: 3 additions & 0 deletions Misc/NEWS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ What's New in Python 3.7.0 alpha 1?
Core and Builtins
-----------------

- bpo-28893: Set correct __cause__ for errors about invalid awaitables
returned from __aiter__ and __anext__.

- bpo-29683: Fixes to memory allocation in _PyCode_SetExtra. Patch by
Brian Coleman.

Expand Down
6 changes: 3 additions & 3 deletions Python/ceval.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1855,13 +1855,13 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)

awaitable = _PyCoro_GetAwaitableIter(iter);
if (awaitable == NULL) {
SET_TOP(NULL);
PyErr_Format(
_PyErr_FormatFromCause(
PyExc_TypeError,
"'async for' received an invalid object "
"from __aiter__: %.100s",
Py_TYPE(iter)->tp_name);

SET_TOP(NULL);
Py_DECREF(iter);
goto error;
} else {
Expand Down Expand Up @@ -1920,7 +1920,7 @@ _PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)

awaitable = _PyCoro_GetAwaitableIter(next_iter);
if (awaitable == NULL) {
PyErr_Format(
_PyErr_FormatFromCause(
PyExc_TypeError,
"'async for' received an invalid object "
"from __anext__: %.100s",
Expand Down

Back | FazBrowse Home | New Git URL