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: Call sys.unraisablehook in the REPL when sys.excepthook is broken
Type: Stage:
Components: Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Carl.Friedrich.Bolz, Julian, Julian2, vstinner
Priority: normal Keywords:

Created on 2021-02-06 20:55 by Julian, last changed 2022-04-11 14:59 by admin.

Messages (3)
msg386569 - (view) Author: Julian Berman (Julian) * Date: 2021-02-06 20:55
At the REPL, when sys.excepthook is broken (below by setting it to a non-callable), one sees:

```
⊙  python3.9                                                                                                                                                                        julian@Airm
Python 3.9.1 (default, Feb  2 2021, 22:54:59) 
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys; sys.excepthook = object(); asdf
Error in sys.excepthook:
TypeError: 'object' object is not callable

Original exception was:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'asdf' is not defined
```

The implementation (https://github.com/python/cpython/blob/5f18c223391eef8c7d01241b51a7b2429609dd84/Python/pythonrun.c#L805-L817) seems to do so by reimplementing something like 3.8+'s sys.unraisablehook (well, technically it predates sys.unraisablehook by quite a while).

Seems like now that it exists, that code should now call sys.unraisablehook.
msg386703 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-02-09 10:38
It makes sense to call _PyErr_WriteUnraisableMsg() when sys.excepthook raise a new exception. Do you want to propose a PR for that?

sys.excepthook documentation mentions sys.unraisablehook:
https://docs.python.org/dev/library/sys.html#sys.excepthook

threading._make_invoke_excepthook logs threading.excepthook failure using sys.excepthook.

Note: Carl also suggested to add a way to invoke sys.unraisablehook explicitly, not only for testing purpose. It would help in some cases.
msg387026 - (view) Author: Julian Berman (Julian2) Date: 2021-02-15 16:58
Thanks Victor. Yes likely happy to send a PR (have to clear a few things
off the yak stack first)

On Tue, Feb 9, 2021 at 5:38 AM STINNER Victor <report@bugs.python.org>
wrote:

>
> STINNER Victor <vstinner@python.org> added the comment:
>
> It makes sense to call _PyErr_WriteUnraisableMsg() when sys.excepthook
> raise a new exception. Do you want to propose a PR for that?
>
> sys.excepthook documentation mentions sys.unraisablehook:
> https://docs.python.org/dev/library/sys.html#sys.excepthook
>
> threading._make_invoke_excepthook logs threading.excepthook failure using
> sys.excepthook.
>
> Note: Carl also suggested to add a way to invoke sys.unraisablehook
> explicitly, not only for testing purpose. It would help in some cases.
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <https://bugs.python.org/issue43148>
> _______________________________________
>
History
Date User Action Args
2022-04-11 14:59:41adminsetgithub: 87314
2021-02-15 16:58:07Julian2setnosy: + Julian2
messages: + msg387026
2021-02-09 10:38:31vstinnersetmessages: + msg386703
2021-02-06 20:58:32Carl.Friedrich.Bolzsetnosy: + Carl.Friedrich.Bolz
2021-02-06 20:55:56Juliancreate