python / cpython

The Python programming language
https://www.python.org
Other
63.68k stars 30.51k forks source link

Call sys.unraisablehook in the REPL when sys.excepthook is broken #87314

Open 8a0c69e7-f5ea-4ea3-8880-4903ed9740d7 opened 3 years ago

8a0c69e7-f5ea-4ea3-8880-4903ed9740d7 commented 3 years ago
BPO 43148
Nosy @vstinner, @cfbolz, @Julian, @Julian

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields: ```python assignee = None closed_at = None created_at = labels = ['3.10'] title = 'Call sys.unraisablehook in the REPL when sys.excepthook is broken' updated_at = user = 'https://github.com/Julian' ``` bugs.python.org fields: ```python activity = actor = 'Julian2' assignee = 'none' closed = False closed_date = None closer = None components = [] creation = creator = 'Julian' dependencies = [] files = [] hgrepos = [] issue_num = 43148 keywords = [] message_count = 3.0 messages = ['386569', '386703', '387026'] nosy_count = 4.0 nosy_names = ['vstinner', 'Carl.Friedrich.Bolz', 'Julian', 'Julian2'] pr_nums = [] priority = 'normal' resolution = None stage = None status = 'open' superseder = None type = None url = 'https://bugs.python.org/issue43148' versions = ['Python 3.10'] ```

8a0c69e7-f5ea-4ea3-8880-4903ed9740d7 commented 3 years ago

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.

vstinner commented 3 years ago

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.

8a0c69e7-f5ea-4ea3-8880-4903ed9740d7 commented 3 years ago

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\