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.

Title: raise an exception from multiple positions break the traceback frames
Type: behavior Stage: resolved
Components: Versions: Python 3.6
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: hubo1016, iritkatriel
Priority: normal Keywords:

Created on 2018-03-13 09:19 by hubo1016, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit hubo1016, 2018-03-13 09:19 demonstration script
Messages (3)
msg313735 - (view) Author: hubo (hubo1016) * Date: 2018-03-13 09:19
The attachment is a script that demonstrates the behavior. The simple unittest script should exit very quickly, but in fact, it runs indefinitely.

It uses asyncio to reproduce the result, but other concurrent technologies are also affected.

In Python 3, traceback of an exception is mutable. When the exception is re-raised, current frames are appended to the traceback. But when the same exception object is raised in multiple position (e.g. passed to different coroutines with futures), the frames are appended in the same list, so the tracebacks are mixed together.

assertRaises in unittest calls traceback.clear_frames internally. When the tracebacks are mixed, it may clear a running frame (in a generator), producing strange behaviors.
msg382690 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-12-07 22:27
When an exception has a __traceback__ and you raise it, the current frame is added to that traceback. What do you expect the behavior to be when you raise the same exception instance twice, from two different locations?
msg384969 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-01-12 18:04
See this documentation of with_traceback() regarding an API to set the traceback on an exception object.
Date User Action Args
2022-04-11 14:58:58adminsetgithub: 77247
2021-01-12 18:04:50iritkatrielsetstatus: open -> closed
resolution: not a bug
messages: + msg384969

stage: resolved
2020-12-07 22:27:24iritkatrielsetnosy: + iritkatriel
messages: + msg382690
2018-03-13 09:19:42hubo1016create