Skip to content

Commit

Permalink
A little more verbose to avoid PyErr_Occurred.
Browse files Browse the repository at this point in the history
We don't want examples to encourage PyErr_Occurred on modern APIs.
  • Loading branch information
gpshead committed Feb 21, 2024
1 parent 71d527f commit b04aa97
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions Doc/c-api/long.rst
Original file line number Diff line number Diff line change
Expand Up @@ -395,11 +395,13 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
}
// Safely get the entire value.
Py_ssize_t bytes = PyLong_AsNativeBits(pylong, bignum, expected, -1);
if (bytes < 0 || bytes > expected) { // Be safe, should not be possible.
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_RuntimeError,
"Unexpected bignum truncation after a size check.");
}
if (bytes < 0) { // Exception set.
free(bignum);
return NULL;
}
else if (bytes > expected) { // Be safe, should not be possible.
PyErr_SetString(PyExc_RuntimeError,
"Unexpected bignum truncation after a size check.");
free(bignum);
return NULL;
}
Expand Down

0 comments on commit b04aa97

Please sign in to comment.