8000 gh-102778: Add sys.last_exc, deprecate sys.last_type, sys.last_value,sys.last_traceback by iritkatriel · Pull Request #102779 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-102778: Add sys.last_exc, deprecate sys.last_type, sys.last_value,sys.last_traceback #102779

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 9 commits into from
Mar 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
gh-102778: Add sys.last_exc, deprecate sys.last_type, sys.last_value,…
… sys.last_traceback
  • Loading branch information
iritkatriel committed Mar 17, 2023
commit 75e1496d4bbea6ab61c9cb5dd1917609b45f70d7
25 changes: 14 additions & 11 deletions Doc/library/sys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1102,22 +1102,25 @@ always available.

.. versionadded:: 3.5

.. data:: last_exc

This variable is not always defined; it is set to the exception instance
when an exception is not handled and the interpreter prints an error message
and a stack traceback. Its intended use is to allow an interactive user to
import a debugger module and engage in post-mortem debugging without having
to re-execute the command that caused the error. (Typical use is
``import pdb; pdb.pm()`` to enter the post-mortem debugger; see :mod:`pdb`
module for more information.)

.. versionadded:: 3.12

.. data:: last_type
last_value
last_traceback

These three variables are not always defined; they are set when an exception is
not handled and the interpreter prints an error message and a stack traceback.
Their intended use is to allow an interactive user to import a debugger module
and engage in post-mortem debugging without having to re-execute the command
that caused the error. (Typical use is ``import pdb; pdb.pm()`` to enter the
post-mortem debugger; see :mod:`pdb` module for
more information.)

The meaning of the variables is the same as that of the return values from
:func:`exc_info` above.

These three variables are deprecated, use ``sys.last_exc`` instead.
They hold the legacy representation of ``sys.last_exc``, as returned
from :func:`exc_info` above.

.. data:: maxsize

Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_global_objects_fini_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Include/internal/pycore_global_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ struct _Py_global_strings {
STRUCT_FOR_ID(kw2)
STRUCT_FOR_ID(lambda)
STRUCT_FOR_ID(last)
STRUCT_FOR_ID(last_exc)
STRUCT_FOR_ID(last_node)
STRUCT_FOR_ID(last_traceback)
STRUCT_FOR_ID(last_type)
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_runtime_init_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Include/internal/pycore_unicodeobject_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Lib/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ def showsyntaxerror(self, filename=None):

"""
type, value, tb = sys.exc_info()
sys.last_exc = value
sys.last_type = type
sys.last_value = value
sys.last_traceback = tb
Expand Down Expand Up @@ -138,6 +139,7 @@ def showtraceback(self):
"""
sys.last_type, sys.last_value, last_tb = ei = sys.exc_info()
sys.last_traceback = last_tb
sys.last_exc = ei[1]
try:
lines = traceback.format_exception(ei[0], ei[1], last_tb.tb_next)
if sys.excepthook is sys.__excepthook__:
Expand Down
2 changes: 1 addition & 1 deletion Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,7 @@ finalize_modules_delete_special(PyThreadState *tstate, int verbose)
{
// List of names to clear in sys
static const char * const sys_deletes[] = {
"path", "argv", "ps1", "ps2",
"path", "argv", "ps1", "ps2", "last_exc",
"last_type", "last_value", "last_traceback",
"__interactivehook__",
// path_hooks and path_importer_cache are cleared
Expand Down
4 changes: 4 additions & 0 deletions Python/pythonrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,10 @@ _PyErr_PrintEx(PyThreadState *tstate, int set_sys_last_vars)
}

if (set_sys_last_vars) {
if (_PySys_SetAttr(&_Py_ID(last_exc), exc) < 0) {
_PyErr_Clear(tstate);
}
/* Legacy version: */
if (_PySys_SetAttr(&_Py_ID(last_type), typ) < 0) {
_PyErr_Clear(tstate);
}
Expand Down
6 changes: 4 additions & 2 deletions Python/sysmodule.c
6E66
Original file line number Diff line number Diff line change
Expand Up @@ -2670,11 +2670,13 @@ stderr -- standard error object; used for error messages\n\
By assigning other file objects (or objects that behave like files)\n\
to these, it is possible to redirect all of the interpreter's I/O.\n\
\n\
last_exc - the last uncaught exception\n\
Only available in an interactive session after a\n\
traceback has been printed.\n\
last_type -- type of last uncaught exception\n\
last_value -- value of last uncaught exception\n\
last_traceback -- traceback of last uncaught exception\n\
These three are only available in an interactive session after a\n\
traceback has been printed.\n\
These three are the (deprecated) legacy representation of last_exc.\n\
"
)
/* concatenating string here */
Expand Down
0