From b04aa97c91a4a901baec692321c1717dc0866bab Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith [Google LLC]" Date: Tue, 20 Feb 2024 16:51:43 -0800 Subject: [PATCH] A little more verbose to avoid PyErr_Occurred. We don't want examples to encourage PyErr_Occurred on modern APIs. --- Doc/c-api/long.rst | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Doc/c-api/long.rst b/Doc/c-api/long.rst index d36d0b0eac7d6e..1fdd0280133b16 100644 --- a/Doc/c-api/long.rst +++ b/Doc/c-api/long.rst @@ -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; }