Skip to content

gh-89373: Document that error indicator may be set in tp_dealloc#28358

Merged
vstinner merged 9 commits into
python:mainfrom
ezyang:tp-dealloc-error-indicator
Jun 9, 2025
Merged

gh-89373: Document that error indicator may be set in tp_dealloc#28358
vstinner merged 9 commits into
python:mainfrom
ezyang:tp-dealloc-error-indicator

Conversation

@ezyang

@ezyang ezyang commented Sep 15, 2021

Copy link
Copy Markdown
Contributor

Signed-off-by: Edward Z. Yang <ezyang@fb.com>
@the-knights-who-say-ni

Copy link
Copy Markdown

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

Recognized GitHub username

We couldn't find a bugs.python.org (b.p.o) account corresponding to the following GitHub usernames:

@ezyang

This might be simply due to a missing "GitHub Name" entry in one's b.p.o account settings. This is necessary for legal reasons before we can look at this contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

@bedevere-bot bedevere-bot added awaiting review docs Documentation in the Doc dir labels Sep 15, 2021
@ezyang

ezyang commented Sep 15, 2021

Copy link
Copy Markdown
Contributor Author

just signed CLA

Signed-off-by: Edward Z. Yang <ezyang@fb.com>
@JelleZijlstra

Copy link
Copy Markdown
Member

There is a merge conflict, could you take a look?

@ezyang

ezyang commented Apr 3, 2022

Copy link
Copy Markdown
Contributor Author

done

@JelleZijlstra JelleZijlstra left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. This note seems useful, but I am not that comfortable with the C API, and on the bug @vstinner seems to prefer a different direction, so this will need more discussion.

Comment thread Doc/c-api/typeobj.rst Outdated
:c:func:`PyObject_GC_Del` if the instance was allocated using
:c:func:`PyObject_GC_New` or :c:func:`PyObject_GC_NewVar`.

If you may call functions that may set the error indicator, you must

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand it, very little of the C API is safe to call with an active error set. So maybe this should be a stronger message?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, the intent here was not to discuss C API functions, but if you were calling into userland (which happens if you've got some complicated cleanup functions; at least that's what bit us here.)

@ezyang

ezyang commented Apr 3, 2022

Copy link
Copy Markdown
Contributor Author

I don't know enough about CPython to know if @vstinner's suggestion to enforce deallocation is never called when error is set is feasible. Seems... difficult to enforce.

Comment thread Doc/c-api/typeobj.rst Outdated
If you may call functions that may set the error indicator, you must
use :c:func:`PyErr_Fetch` and :c:func:`PyErr_Restore` to ensure you
don't clobber a preexisting error indicator (the deallocation could
have occurred while processing a different error):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add that the function must not raise an exception. It can use PyErr_WriteUnraisable() to log (and clear) an "unraisable" exception.

By the way, I'm surprised that _Py_Dealloc() doesn't ensure in debug mode (Py_DEBUG) that tp_dealloc does not raise a new exception. See also _Py_CheckSlotResult() and _Py_CheckFunctionResult().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

MaxwellDupre

This comment was marked as off-topic.

@python-cla-bot

python-cla-bot Bot commented Apr 6, 2025

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@ezyang

ezyang commented Jun 6, 2025

Copy link
Copy Markdown
Contributor Author

Merge conflicts fixed LOL, three years later

Comment thread Doc/c-api/typeobj.rst Outdated
Signed-off-by: Edward Z. Yang <ezyang@meta.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
@vstinner

vstinner commented Jun 7, 2025

Copy link
Copy Markdown
Member

@ezyang: Oh, the last blocker point is that you didnd't sign the CLA with your email, see: #28358 (comment)

@ezyang

ezyang commented Jun 9, 2025

Copy link
Copy Markdown
Contributor Author

Too many emails lol. Fixed.

Comment thread Doc/c-api/typeobj.rst Outdated

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vstinner vstinner enabled auto-merge (squash) June 9, 2025 08:51
@picnixz picnixz changed the title bpo-45210: Document that error indicator may be set in tp_dealloc gh-89373: Document that error indicator may be set in tp_dealloc Jun 9, 2025
@vstinner vstinner merged commit 8441b26 into python:main Jun 9, 2025
27 of 28 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in Docs PRs Jun 9, 2025
@vstinner vstinner added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Jun 9, 2025
@miss-islington-app

Copy link
Copy Markdown

Thanks @ezyang for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@miss-islington-app

Copy link
Copy Markdown

Thanks @ezyang for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Sorry, @ezyang and @vstinner, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 8441b263af964f353bf02d56c32a4fc547cdc330 3.13

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 9, 2025
…thonGH-28358)

(cherry picked from commit 8441b26)

Co-authored-by: Edward Z. Yang <ezyang@mit.edu>
Signed-off-by: Edward Z. Yang <ezyang@fb.com>
Signed-off-by: Edward Z. Yang <ezyang@meta.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
@bedevere-app

bedevere-app Bot commented Jun 9, 2025

Copy link
Copy Markdown

GH-135298 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Jun 9, 2025
@vstinner vstinner removed the needs backport to 3.13 bugs and security fixes label Jun 9, 2025
@vstinner

vstinner commented Jun 9, 2025

Copy link
Copy Markdown
Member

Merged, thanks @ezyang. Better late than never :-)

vstinner added a commit that referenced this pull request Jun 9, 2025
…loc (GH-28358) (#135298)

bpo-45210: Document that error indicator may be set in tp_dealloc (GH-28358)
(cherry picked from commit 8441b26)

Signed-off-by: Edward Z. Yang <ezyang@fb.com>
Signed-off-by: Edward Z. Yang <ezyang@meta.com>
Co-authored-by: Edward Z. Yang <ezyang@mit.edu>
Co-authored-by: Victor Stinner <vstinner@python.org>
lkollar pushed a commit to lkollar/cpython that referenced this pull request Jun 19, 2025
…thon#28358)

Signed-off-by: Edward Z. Yang <ezyang@fb.com>
Signed-off-by: Edward Z. Yang <ezyang@meta.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
…thon#28358)

Signed-off-by: Edward Z. Yang <ezyang@fb.com>
Signed-off-by: Edward Z. Yang <ezyang@meta.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request Aug 4, 2025
…thon#28358)

Signed-off-by: Edward Z. Yang <ezyang@fb.com>
Signed-off-by: Edward Z. Yang <ezyang@meta.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
Agent-Hellboy pushed a commit to Agent-Hellboy/cpython that referenced this pull request Aug 19, 2025
…thon#28358)

Signed-off-by: Edward Z. Yang <ezyang@fb.com>
Signed-off-by: Edward Z. Yang <ezyang@meta.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation in the Doc dir

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

8 participants