[issue1230540] sys.excepthook doesn't work in threads

Andrey Vlasovskikh report at bugs.python.org
Sat Aug 4 11:46:54 EDT 2018


Andrey Vlasovskikh <andrey.vlasovskikh at gmail.com> added the comment:

> Would it be possible to modify the default implementation of sys.excepthook to have a different output when it's not called from the main thread? Mimick the current traceback from threads.

I agree it's a good idea to mimic what `_thread` does in case of both handled and unhandled exceptions. Why would you want to do it in `sys.excepthook`? The code for handling unhandled exceptions in `_thread` threads is located in `_threadmodule.c` itself, so there is no dependency from `sys` to `_thread`. By following the same logic, the code for handling unhandled exceptions in `threading` should be located in `threading.py`, like it does now. So the only thing that's missing in the pull request compared to `_thread` is the output:

   Unhandled exception in thread started by <function <lambda> at 0x7f6769ca8e18>


for all the exceptions: not caught by the sys.excepthook **and** caught by the sys.exceptook.

> An alternative would be to introduce a new hook to log exceptions in threads, ex: sys.threadexcepthook. That hoook would take a 4th parameter. But Andrey Vlasovskikh didn't like this idea.

I'm overall OK with this idea, but I would prefer to make the existing `sys.excepthook` API applicable to all the exceptions: for the main thread (works well), for `_thread` threads (works well) and for `threading` threads (doesn't work).

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue1230540>
_______________________________________


More information about the Python-bugs-list mailing list