summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYury Selivanov <yury@magic.io>2016-12-01 11:36:22 -0500
committerYury Selivanov <yury@magic.io>2016-12-01 11:36:22 -0500
commit720946323027f5bfb31c006cc9b3ce983af8291f (patch)
tree8911d639cdb6953ca6815e6efb86febcb119b63e
parentfdb3738d2cd72d24ed759145de262d2ce373a02b (diff)
downloadcpython-720946323027f5bfb31c006cc9b3ce983af8291f.tar.gz
Issue #28843: Fix asyncio C Task to handle exceptions __traceback__.
-rw-r--r--Lib/test/test_asyncio/test_tasks.py15
-rw-r--r--Misc/NEWS2
-rw-r--r--Modules/_asynciomodule.c5
3 files changed, 22 insertions, 0 deletions
diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py
index e048380e98..a18d49ae37 100644
--- a/Lib/test/test_asyncio/test_tasks.py
+++ b/Lib/test/test_asyncio/test_tasks.py
@@ -1952,6 +1952,21 @@ class BaseTaskTests:
self.assertFalse(gather_task.cancelled())
self.assertEqual(gather_task.result(), [42])
+ def test_exception_traceback(self):
+ # See http://bugs.python.org/issue28843
+
+ @asyncio.coroutine
+ def foo():
+ 1 / 0
+
+ @asyncio.coroutine
+ def main():
+ task = self.new_task(self.loop, foo())
+ yield # skip one loop iteration
+ self.assertIsNotNone(task.exception().__traceback__)
+
+ self.loop.run_until_complete(main())
+
@mock.patch('asyncio.base_events.logger')
def test_error_in_call_soon(self, m_log):
def call_soon(callback, *args):
diff --git a/Misc/NEWS b/Misc/NEWS
index 50a446ad9d..97b159722e 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -24,6 +24,8 @@ Library
- Issue #24142: Reading a corrupt config file left configparser in an
invalid state. Original patch by Florian Höch.
+- Issue #28843: Fix asyncio C Task to handle exceptions __traceback__.
+
Tools/Demos
-----------
diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c
index b65fc02ebd..4e8f74a3c9 100644
--- a/Modules/_asynciomodule.c
+++ b/Modules/_asynciomodule.c
@@ -1042,6 +1042,8 @@ FutureIter_throw(futureiterobject *self, PyObject *args)
if (PyExceptionClass_Check(type)) {
PyErr_NormalizeException(&type, &val, &tb);
+ /* No need to call PyException_SetTraceback since we'll be calling
+ PyErr_Restore for `type`, `val`, and `tb`. */
} else if (PyExceptionInstance_Check(type)) {
if (val) {
PyErr_SetString(PyExc_TypeError,
@@ -2003,6 +2005,9 @@ task_step_impl(TaskObj *task, PyObject *exc)
if (!ev || !PyObject_TypeCheck(ev, (PyTypeObject *) et)) {
PyErr_NormalizeException(&et, &ev, &tb);
}
+ if (tb != NULL) {
+ PyException_SetTraceback(ev, tb);
+ }
o = future_set_exception((FutureObj*)task, ev);
if (!o) {
/* An exception in Task.set_exception() */