diff options
author | Gregory P. Smith <greg@krypto.org> | 2022-11-03 23:18:38 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-03 23:18:38 (GMT) |
commit | 4c4b5ce2e529a1279cd287e2d2d73ffcb6cf2ead (patch) | |
tree | 738e223c212b3917afe011e2a8a21f4b78ebb503 /Objects/longobject.c | |
parent | bee107028922adc55421611b4bf7da14b8a64010 (diff) | |
download | cpython-4c4b5ce2e529a1279cd287e2d2d73ffcb6cf2ead.zip cpython-4c4b5ce2e529a1279cd287e2d2d73ffcb6cf2ead.tar.gz cpython-4c4b5ce2e529a1279cd287e2d2d73ffcb6cf2ead.tar.bz2 |
gh-90716: bugfixes and more tests for _pylong. (#99073)
* Properly decref on _pylong import error.
* Improve the error message on _pylong TypeError.
* Fix the assertion error in pydebug builds to be a TypeError.
* Tie the return value comments together.
These are minor followups to issues not caught among the reviewers on
https://github.com/python/cpython/pull/96673.
Diffstat (limited to 'Objects/longobject.c')
-rw-r--r-- | Objects/longobject.c | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/Objects/longobject.c b/Objects/longobject.c index cf859cc..a872938 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -1753,7 +1753,11 @@ pylong_int_to_decimal_string(PyObject *aa, if (s == NULL) { goto error; } - assert(PyUnicode_Check(s)); + if (!PyUnicode_Check(s)) { + PyErr_SetString(PyExc_TypeError, + "_pylong.int_to_decimal_string did not return a str"); + goto error; + } if (writer) { Py_ssize_t size = PyUnicode_GET_LENGTH(s); if (_PyUnicodeWriter_Prepare(writer, size, '9') == -1) { @@ -2362,6 +2366,7 @@ pylong_int_from_string(const char *start, const char *end, PyLongObject **res) } PyObject *s = PyUnicode_FromStringAndSize(start, end-start); if (s == NULL) { + Py_DECREF(mod); goto error; } PyObject *result = PyObject_CallMethod(mod, "int_from_string", "O", s); @@ -2371,14 +2376,15 @@ pylong_int_from_string(const char *start, const char *end, PyLongObject **res) goto error; } if (!PyLong_Check(result)) { - PyErr_SetString(PyExc_TypeError, "an integer is required"); + PyErr_SetString(PyExc_TypeError, + "_pylong.int_from_string did not return an int"); goto error; } *res = (PyLongObject *)result; return 0; error: *res = NULL; - return 0; + return 0; // See the long_from_string_base() API comment. } #endif /* WITH_PYLONG_MODULE */ @@ -2617,7 +2623,8 @@ long_from_non_binary_base(const char *start, const char *end, Py_ssize_t digits, * Return values: * * - Returns -1 on syntax error (exception needs to be set, *res is untouched) - * - Returns 0 and sets *res to NULL for MemoryError/OverflowError. + * - Returns 0 and sets *res to NULL for MemoryError, OverflowError, or + * _pylong.int_from_string() errors. * - Returns 0 and sets *res to an unsigned, unnormalized PyLong (success!). * * Afterwards *str is set to point to the first non-digit (which may be *str!). |