gh-95778: Use a note for the max digits error message by vstinner · Pull Request #96878 · python/cpython · GitHub
Skip to content
Closed
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
6 changes: 4 additions & 2 deletions Doc/library/stdtypes.rst
5 changes: 5 additions & 0 deletions Include/internal/pycore_pyerrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ PyAPI_FUNC(PyObject *) _PyExc_PrepReraiseStar(

PyAPI_FUNC(int) _PyErr_CheckSignalsTstate(PyThreadState *tstate);

// Call add_note(note) on the current exception.
// Return -1 on error, or 0 on success.
// Save and restore the current exception.

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.

Might be good to mention in the comment that there must be a current exception. I think otherwise it crashes or some assertion fails, not sure.

PyAPI_FUNC(int) _PyErr_AddNote(const char *note);

PyAPI_FUNC(void) _Py_DumpExtensionModules(int fd, PyInterpreterState *interp);

extern PyObject* _Py_Offer_Suggestions(PyObject* exception);
Expand Down
36 changes: 36 additions & 0 deletions Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "pycore_exceptions.h" // struct _Py_exc_state
#include "pycore_initconfig.h"
#include "pycore_object.h"
#include "pycore_pyerrors.h" // _PyErr_Restore()
#include "structmember.h" // PyMemberDef
#include "osdefs.h" // SEP

Expand Down Expand Up @@ -3833,3 +3834,38 @@ _PyErr_TrySetFromCause(const char *format, ...)
PyErr_Restore(new_exc, new_val, new_tb);
return new_val;
}


int
_PyErr_AddNote(const char *note)
{
int err = -1;
PyObject *str = NULL, *res = NULL;

PyThreadState *tstate = _PyThreadState_GET();
PyObject *exc_type, *exc_value, *exc_tb;
_PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb);
PyErr_NormalizeException(&exc_type, &exc_value, &exc_tb);

if (exc_value == NULL) {
goto exit;
}

str = PyUnicode_FromString(note);
if (str == NULL) {
goto exit;
}

res = BaseException_add_note(exc_value, str);
if (res == NULL) {
goto exit;
}
err = 0;

exit:
Py_DECREF(str);
Py_XDECREF(res);

_PyErr_Restore(tstate, exc_type, exc_value, exc_tb);
return err;
}
17 changes: 15 additions & 2 deletions Objects/longobject.c