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

bpo-29587: Enable exception chaining for gen.throw() with "yield from" by cjerdonek · Pull Request #19858 · 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  (2) All 2 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
27 changes: 27 additions & 0 deletions Lib/test/test_asyncio/test_tasks.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 @@ -466,6 +466,33 @@ async def inner2():
t = outer()
self.assertEqual(self.loop.run_until_complete(t), 1042)

def test_exception_chaining_after_await(self):
# Test that when awaiting on a task when an exception is already
# active, if the task raises an exception it will be chained
# with the original.
loop = asyncio.new_event_loop()
self.set_event_loop(loop)

async def raise_error():
raise ValueError

async def run():
try:
raise KeyError(3)
except Exception as exc:
task = self.new_task(loop, raise_error())
try:
await task
except Exception as exc:
self.assertEqual(type(exc), ValueError)
chained = exc.__context__
self.assertEqual((type(chained), chained.args),
(KeyError, (3,)))

task = self.new_task(loop, run())
loop.run_until_complete(task)
loop.close()

def test_cancel(self):

def gen():
Expand Down
19 changes: 18 additions & 1 deletion Lib/test/test_generators.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 @@ -318,7 +318,7 @@ def g():

class GeneratorThrowTest(unittest.TestCase):

def test_exception_context_set(self):
def test_exception_context_with_yield(self):
def f():
try:
raise KeyError('a')
Expand All @@ -332,6 +332,23 @@ def f():
context = cm.exception.__context__
self.assertEqual((type(context), context.args), (KeyError, ('a',)))

def test_exception_context_with_yield_from(self):
def f():
yield

def g():
try:
raise KeyError('a')
except Exception:
yield from f()

gen = g()
gen.send(None)
with self.assertRaises(ValueError) as cm:
gen.throw(ValueError)
context = cm.exception.__context__
self.assertEqual((type(context), context.args), (KeyError, ('a',)))

def test_throw_after_none_exc_type(self):
def g():
try:
Expand Down
22 changes: 12 additions & 10 deletions Objects/genobject.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 @@ -217,6 +217,18 @@ gen_send_ex(PyGenObject *gen, PyObject *arg, int exc, int closing)
assert(f->f_back == NULL);
f->f_back = tstate->frame;

_PyErr_StackItem *gi_exc_state = &gen->gi_exc_state;
if (exc && gi_exc_state->exc_type != NULL &&
gi_exc_state->exc_type != Py_None)
{
Py_INCREF(gi_exc_state->exc_type);
Py_XINCREF(gi_exc_state->exc_value);
Py_XINCREF(gi_exc_state->exc_traceback);
_PyErr_ChainExceptions(gi_exc_state->exc_type,
gi_exc_state->exc_value,
gi_exc_state->exc_traceback);
}

gen->gi_running = 1;
gen->gi_exc_state.previous_item = tstate->exc_info;
tstate->exc_info = &gen->gi_exc_state;
Expand Down Expand Up @@ -512,16 +524,6 @@ _gen_throw(PyGenObject *gen, int close_on_genexit,
}

PyErr_Restore(typ, val, tb);

_PyErr_StackItem *gi_exc_state = &gen->gi_exc_state;
if (gi_exc_state->exc_type != NULL && gi_exc_state->exc_type != Py_None) {
Py_INCREF(gi_exc_state->exc_type);
Py_XINCREF(gi_exc_state->exc_value);
Py_XINCREF(gi_exc_state->exc_traceback);
_PyErr_ChainExceptions(gi_exc_state->exc_type,
gi_exc_state->exc_value,
gi_exc_state->exc_traceback);
}
return gen_send_ex(gen, Py_None, 1, 0);

failed_throw:
Expand Down

Back | FazBrowse Home | New Git URL