changeset: 105405:c9f68150cf90 branch: 3.6 parent: 105393:2dd08b5b5ee6 user: Yury Selivanov date: Thu Dec 01 11:36:22 2016 -0500 files: Lib/test/test_asyncio/test_tasks.py Misc/NEWS Modules/_asynciomodule.c description: Issue #28843: Fix asyncio C Task to handle exceptions __traceback__. diff -r 2dd08b5b5ee6 -r c9f68150cf90 Lib/test/test_asyncio/test_tasks.py --- a/Lib/test/test_asyncio/test_tasks.py Tue Nov 29 09:46:26 2016 -0800 +++ b/Lib/test/test_asyncio/test_tasks.py Thu Dec 01 11:36:22 2016 -0500 @@ -1952,6 +1952,21 @@ 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 -r 2dd08b5b5ee6 -r c9f68150cf90 Misc/NEWS --- a/Misc/NEWS Tue Nov 29 09:46:26 2016 -0800 +++ b/Misc/NEWS Thu Dec 01 11:36:22 2016 -0500 @@ -24,6 +24,8 @@ - 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 -r 2dd08b5b5ee6 -r c9f68150cf90 Modules/_asynciomodule.c --- a/Modules/_asynciomodule.c Tue Nov 29 09:46:26 2016 -0800 +++ b/Modules/_asynciomodule.c Thu Dec 01 11:36:22 2016 -0500 @@ -1042,6 +1042,8 @@ 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 @@ 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() */