This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Returning an exception object from a coroutine triggers implicit exception chaining?!?
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.7, Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: njs, serhiy.storchaka, yselivanov
Priority: normal Keywords:

Created on 2017-02-19 13:44 by njs, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 644 merged yselivanov, 2017-03-12 16:59
Messages (4)
msg288133 - (view) Author: Nathaniel Smith (njs) * (Python committer) Date: 2017-02-19 13:44
The following code prints "KeyError()", which is obviously wrong and rather baffling. Just passing an exception object around *as an object* should not trigger implicit exception chaining!

If you replace the async functions with regular functions then it prints "None", as expected.

Checked on 3.5, 3.6, and more-or-less current master -- bug is present in all 3.

------------

async def f():
    return ValueError()

async def g():
    try:
        raise KeyError
    except:
        value_error = await f()
        print(repr(value_error.__context__))

try:
    g().send(None)
except StopIteration:
    pass
msg289503 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2017-03-12 17:01
What a bizarre bug.  Created a PR with a fix.  

Nathaniel, please put me in a nosy list when you submit issues about generators/coroutines.
msg289577 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-03-14 10:12
I am wondering how much of other uses of PyErr_SetObject() are affected by similar bugs?
msg289583 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2017-03-14 13:38
> I am wondering how much of other uses of PyErr_SetObject() are affected by similar bugs?

Only when we use an exception to carry a return value.  AFAIK StopIteration is the only such case.
History
Date User Action Args
2022-04-11 14:58:43adminsetgithub: 73786
2017-03-14 13:38:14yselivanovsetmessages: + msg289583
2017-03-14 10:12:11serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg289577
2017-03-12 21:04:58yselivanovsetstatus: open -> closed
type: behavior
resolution: fixed
stage: resolved
2017-03-12 17:01:30yselivanovsetnosy: + yselivanov
messages: + msg289503
2017-03-12 16:59:15yselivanovsetpull_requests: + pull_request533
2017-02-19 13:44:49njscreate