Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Improve overflow error messages in :c:func:`PyLong_AsLong` and :c:func:`PyLong_AsInt`
to indicate whether the value exceeds the maximum or minimum bounds, instead of
showing generic overflow messages.
32 changes: 24 additions & 8 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -669,10 +669,18 @@ PyLong_AsLong(PyObject *obj)
int overflow;
long result = PyLong_AsLongAndOverflow(obj, &overflow);
if (overflow) {
/* XXX: could be cute and give a different
message for overflow == -1 */
PyErr_SetString(PyExc_OverflowError,
"Python int too large to convert to C long");
if (overflow > 0) {
PyErr_Format(PyExc_OverflowError,
"Python int too large to convert to C long: "
"value would be greater than %ld",
LONG_MAX);
}
else {
PyErr_Format(PyExc_OverflowError,
"Python int too large to convert to C long: "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe 'too small' reads better here (and below)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you be more precise? Replacing "too large" with "too small" will be just plain wrong right here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are in the negative case here and "too large" combined with "less than" rings a bell for me. Maybe just splitting hairs (or I am completely mistaken).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My english is bad, so I trust your judgement.

Though, I don't see a problem so far. This exception supposed to
mean: value too large (by magnitude) for long, it would be < LONG_MIN.

Suggested change
"Python int too large to convert to C long: "
"Python int magnitude too large to convert to C long: "

?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See Serhiy's comment below. I agree it would be better to just have simpler and unified messages.

"value would be less than %ld",
LONG_MIN);
}
}
return result;
}
Expand All @@ -686,10 +694,18 @@ PyLong_AsInt(PyObject *obj)
int overflow;
long result = PyLong_AsLongAndOverflow(obj, &overflow);
if (overflow || result > INT_MAX || result < INT_MIN) {
/* XXX: could be cute and give a different
message for overflow == -1 */
PyErr_SetString(PyExc_OverflowError,
"Python int too large to convert to C int");
if (overflow > 0 || result > INT_MAX) {
PyErr_Format(PyExc_OverflowError,
"Python int too large to convert to C int: "
"value would be greater than %d",
INT_MAX);
}
else {
PyErr_Format(PyExc_OverflowError,
"Python int too large to convert to C int: "
"value would be less than %d",
INT_MIN);
}
return -1;
}
return (int)result;
Expand Down
Loading