Skip to content

Conversation

@mohsinm-dev
Copy link
Contributor

@mohsinm-dev mohsinm-dev commented Nov 15, 2025

Replace generic overflow messages with specific ones that indicate whether the value exceeds maximum or minimum bounds.

Fixes #141578

@mohsinm-dev mohsinm-dev force-pushed the gh-141578-improve-overflow-messages branch from c700fa6 to dc88dc0 Compare November 15, 2025 05:59
@skirpichev
Copy link
Contributor

CC @serhiy-storchaka

}
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.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

In Python/getargs.c, other messages are used, like "signed short integer is less than minimum". I think it is incorrect (signed short integer cannot be less than SHRT_MIN). "integer is less than minimum for C short int" looks more correct.

And some error messages are simply "integer value out of range" because when we use PyLong_AsNativeBytes() we cannot know whether it is too large or too small.

It would be better to produce unified error messages in PyLong_As*(), PyArg_Parse*(), etc.

@skirpichev
Copy link
Contributor

integer is less than minimum for C short int

Maybe it's better.

I.e.: PyErr_Format(..., "Python integer is less than minimum (%ld) for C long", LONG_MIN)

It would be better to produce unified error messages in PyLong_As*(), PyArg_Parse*(), etc.

I think this is out of the scope for this pr.

@chris-eibl
Copy link
Member

I think this is out of the scope for this pr.

Not if we just use

PyErr_SetString(PyExc_OverflowError, "int too big to convert");

like in _PyLong_AsByteArray which is called in PyLong_AsLongLong.

Then at least those would be in sync?

@skirpichev
Copy link
Contributor

Yes, just removing XXX marks is an alternative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide custom error messages for overflows in PyLong_AsLong/PyLong_AsInt()

5 participants