@@ -1753,7 +1753,11 @@ pylong_int_to_decimal_string(PyObject *aa,
17531753if (s == NULL) {
17541754 goto error;
17551755 }
1756-assert(PyUnicode_Check(s));
1756+if (!PyUnicode_Check(s)) {
1757+PyErr_SetString(PyExc_TypeError,
1758+"_pylong.int_to_decimal_string did not return a str");
1759+ goto error;
1760+ }
17571761if (writer) {
17581762Py_ssize_t size = PyUnicode_GET_LENGTH(s);
17591763if (_PyUnicodeWriter_Prepare(writer, size, '9') == -1) {
@@ -2362,6 +2366,7 @@ pylong_int_from_string(const char *start, const char *end, PyLongObject **res)
23622366 }
23632367PyObject *s = PyUnicode_FromStringAndSize(start, end-start);
23642368if (s == NULL) {
2369+Py_DECREF(mod);
23652370 goto error;
23662371 }
23672372PyObject *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)
23712376 goto error;
23722377 }
23732378if (!PyLong_Check(result)) {
2374-PyErr_SetString(PyExc_TypeError, "an integer is required");
2379+PyErr_SetString(PyExc_TypeError,
2380+"_pylong.int_from_string did not return an int");
23752381 goto error;
23762382 }
23772383*res = (PyLongObject *)result;
23782384return 0;
23792385error:
23802386*res = NULL;
2381-return 0;
2387+return 0; // See the long_from_string_base() API comment.
23822388}
23832389#endif /* WITH_PYLONG_MODULE */
23842390@@ -2617,7 +2623,8 @@ long_from_non_binary_base(const char *start, const char *end, Py_ssize_t digits,
26172623 * Return values:
26182624 *
26192625 * - Returns -1 on syntax error (exception needs to be set, *res is untouched)
2620- * - Returns 0 and sets *res to NULL for MemoryError/OverflowError.
2626+ * - Returns 0 and sets *res to NULL for MemoryError, OverflowError, or
2627+ * _pylong.int_from_string() errors.
26212628 * - Returns 0 and sets *res to an unsigned, unnormalized PyLong (success!).
26222629 *
26232630 * Afterwards *str is set to point to the first non-digit (which may be *str!).