Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable() #129354

Closed
vstinner opened this issue Jan 27, 2025 · 4 comments
Closed

Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable() #129354

vstinner opened this issue Jan 27, 2025 · 4 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@vstinner
Copy link
Member

vstinner commented Jan 27, 2025

Python 3.13 added PyErr_FormatUnraisable(format, ...) which allows logging a specific error message, whereas PyErr_WriteUnraisable() always logs the generic "Exception ignored in ..." message.

I propose to use replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable() to log better (more helpful) error messages.

Linked PRs

vstinner added a commit to vstinner/cpython that referenced this issue Jan 27, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
@vstinner
Copy link
Member Author

I wrote a single big PR to show how the code can look like: PR gh-129359.

I propose to use replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable() to log better (more helpful) error messages.

The drawback is that it changes sys.unraisablehook arguments: the object is no longer set. In my PR, I had to update multiple tests to check err_msg (string) instead.

@vstinner
Copy link
Member Author

cc @serhiy-storchaka

@encukou encukou added type-feature A feature request or enhancement interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Jan 27, 2025
vstinner added a commit to vstinner/cpython that referenced this issue Jan 29, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
vstinner added a commit to vstinner/cpython that referenced this issue Jan 29, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
vstinner added a commit to vstinner/cpython that referenced this issue Jan 30, 2025
Replace "on verb+ing" with "when verb+ing".
vstinner added a commit that referenced this issue Jan 30, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
vstinner added a commit to vstinner/cpython that referenced this issue Jan 30, 2025
Replace "on verb+ing" with "while verb+ing".
vstinner added a commit that referenced this issue Jan 31, 2025
Replace "on verb+ing" with "while verb+ing".
vstinner added a commit to vstinner/cpython that referenced this issue Jan 31, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
vstinner added a commit to vstinner/cpython that referenced this issue Jan 31, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
vstinner added a commit to vstinner/cpython that referenced this issue Jan 31, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
vstinner added a commit to vstinner/cpython that referenced this issue Jan 31, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
vstinner added a commit that referenced this issue Jan 31, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
vstinner added a commit to vstinner/cpython that referenced this issue Jan 31, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
vstinner added a commit that referenced this issue Jan 31, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
vstinner added a commit to vstinner/cpython that referenced this issue Jan 31, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
vstinner added a commit to vstinner/cpython that referenced this issue Jan 31, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
vstinner added a commit that referenced this issue Jan 31, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
vstinner added a commit to vstinner/cpython that referenced this issue Jan 31, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().

Update tests:

* test_coroutines
* test_exceptions
* test_generators
* test_struct
vstinner added a commit to vstinner/cpython that referenced this issue Jan 31, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().

Update tests:

* test_coroutines
* test_exceptions
* test_generators
* test_struct
vstinner added a commit to vstinner/cpython that referenced this issue Jan 31, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().

Update test_sqlite3 tests.
vstinner added a commit that referenced this issue Feb 3, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().

Update test_sqlite3 tests.

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
vstinner added a commit to vstinner/cpython that referenced this issue Feb 4, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
vstinner added a commit that referenced this issue Feb 4, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
@vstinner vstinner closed this as completed Feb 5, 2025
vstinner added a commit that referenced this issue Feb 5, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().

Update tests:

* test_coroutines
* test_exceptions
* test_generators
* test_struct
@serhiy-storchaka
Copy link
Member

The flaw of PyErr_WriteUnraisable() is that it prolonges the life of the destroying object by keeping a link to it in the object attribute. So changing it to PyErr_FormatUnraisable() can not only improve the output, but fix some obscure regressions introduced by using PyErr_WriteUnraisable().

@vstinner
Copy link
Member Author

vstinner commented Feb 5, 2025

Right.

The flaw of PyErr_WriteUnraisable() is that it prolonges the life of the destroying object by keeping a link to it in the object attribute.

There is a red warning in the documentation about that: https://docs.python.org/dev/library/sys.html#sys.unraisablehook

The default hook doesn't store exc_value nor object.

vstinner added a commit to vstinner/cpython that referenced this issue Feb 5, 2025
vstinner added a commit to vstinner/cpython that referenced this issue Feb 5, 2025
vstinner added a commit that referenced this issue Feb 5, 2025
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this issue Feb 7, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this issue Feb 7, 2025
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this issue Feb 7, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this issue Feb 7, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this issue Feb 7, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this issue Feb 7, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().

Update test_sqlite3 tests.

Co-authored-by: Erlend E. Aasland <erlend.aasland@protonmail.com>
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this issue Feb 7, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this issue Feb 7, 2025
Replace PyErr_WriteUnraisable() with PyErr_FormatUnraisable().

Update tests:

* test_coroutines
* test_exceptions
* test_generators
* test_struct
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this issue Feb 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants