Skip to content

bpo-25381: Update explanation of exceptions in C. #26838

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

Merged
merged 3 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions Doc/extending/extending.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,11 @@ Intermezzo: Errors and Exceptions

An important convention throughout the Python interpreter is the following: when
a function fails, it should set an exception condition and return an error value
(usually a ``NULL`` pointer). Exceptions are stored in a static global variable
inside the interpreter; if this variable is ``NULL`` no exception has occurred. A
second global variable stores the "associated value" of the exception (the
second argument to :keyword:`raise`). A third variable contains the stack
traceback in case the error originated in Python code. These three variables
are the C equivalents of the result in Python of :meth:`sys.exc_info` (see the
section on module :mod:`sys` in the Python Library Reference). It is important
(usually ``-1`` or a ``NULL`` pointer). Exception information is stored in
three members of the interpreter's thread state. These are ``NULL`` if
there is no exception. Otherwise they are the C equivalents of the members
Copy link
Member

Choose a reason for hiding this comment

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

Actually the second can be NULL or None and the third can be NULL in case of non-normalized exception.

The first is the exception type. NULL if no exception.

The second is either the instance of exception, or argument(s) which will be passed to exception constructor on normalization. NULL or None means no arguments. Tuple means several arguments. Otherwise it is a single argument.

The third is a traceback. Can be NULL for non-normalized exceptions.

Copy link
Member

Choose a reason for hiding this comment

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

See also PyErr_Fetch, PyErr_Restore and PyErr_NormalizeException. Terminology should be consistent.

Copy link
Member Author

Choose a reason for hiding this comment

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

Are you saying that a Python user of sys.exc_info might see these other value, or that the existing claim of C equivalents is incomplete? I presume the latter. If so, how about this, instead of These are....

If there is no exception, these are NULL. For a normalized exception , these are the C equivalents of the members of the Python tuple returned by :meth:sys.exc_info: the exception type, exception instance, and a traceback object. For a non-normalized exception, the second will be an argument or tuple of arguments that will be passed to the exception on normalization, or NULL or None, meaning "no arguments". The third argument may then be NULL.

Copy link
Contributor

Choose a reason for hiding this comment

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

This seems resolved now.

of the Python tuple returned by :meth:`sys.exc_info`. These are the
exception type, exception instance, and a traceback object. It is important
to know about them to understand how errors are passed around.

The Python API defines a number of functions to set various types of exceptions.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
In the extending chapter of the extending doc, update a paragraph about the
global variables containing exception information.