Skip to content

gh-150663: Document that PyErr_SetRaisedException() accepts NULL#150705

Open
philthompson10 wants to merge 4 commits into
python:mainfrom
philthompson10:issue/150663
Open

gh-150663: Document that PyErr_SetRaisedException() accepts NULL#150705
philthompson10 wants to merge 4 commits into
python:mainfrom
philthompson10:issue/150663

Conversation

@philthompson10
Copy link
Copy Markdown

@philthompson10 philthompson10 commented Jun 1, 2026

Documented that PyErr_SetRaisedException() accepts NULL as the value of the exception being set.

Documented that `PyErr_SetRaisedException()` accepts `NULL` as the value of
the exception being set.
@bedevere-app bedevere-app Bot added docs Documentation in the Doc dir skip news labels Jun 1, 2026
@python-cla-bot
Copy link
Copy Markdown

python-cla-bot Bot commented Jun 1, 2026

All commit authors signed the Contributor License Agreement.

CLA signed

@read-the-docs-community
Copy link
Copy Markdown

read-the-docs-community Bot commented Jun 1, 2026

Comment thread Doc/c-api/exceptions.rst Outdated
.. warning::

This call steals a reference to *exc*, which must be a valid exception.
*exc* must be a valid exception (in which case a reference is stolen) or
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.

The fact that it can be NULL should not be in a warning.

Comment thread Doc/c-api/exceptions.rst
Comment on lines +501 to +502
A stolen reference to *exc* is set as the exception currently being raised,
clearing the existing exception if one is set. *exc* may be ``NULL`` in
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.

Keep the warning, but only use it for the note about reference stealing. The sentence about exc being NULL should go in the body.

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 think that it is better to use note instead of warning, like in PyList_SetItem and many other functions that steal references.

I suggest to add "If exc is NULL, just clear the existing exception." to the body and replace warning with note.

@philthompson10
Copy link
Copy Markdown
Author

philthompson10 commented Jun 3, 2026 via email

@ZeroIntensity
Copy link
Copy Markdown
Member

  1. It was there before and not causing any problems, so I don't see why we need to change it here. Doing so just makes merging this more prone to friction.
  2. More generally, I don't think it's a good idea to base documentation decisions about PyErr_SetRaisedException on PyErr_Restore. By design, PyErr_SetRaisedException is much easier to use without a call to PyErr_GetRaisedException; in contrast, PyErr_Restore is almost exclusively called with a pair to PyErr_Fetch (because it's hard to get the exception triple from an arbitrary exception object), which makes it clearer that stealing is the intended choice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review docs Documentation in the Doc dir skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

3 participants