这是indexloc提供的服务,不要输入任何密码
Skip to content

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

@skirpichev

Description

@skirpichev

Feature or enhancement

Proposal:

Both functions have old TODO comments:

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 || 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");
return -1;
}

Lets either remove these marks or provide different error messages, for example for longs:

if (overflow > 0) {
    PyErr_Format(PyExc_OverflowError,
                 "Python int too large to convert to C long, value > %ld",
                 LONG_MAX);
    return -1;
}
else if (overflow < 0) {
    PyErr_Format(PyExc_OverflowError,
                 "Python int too large to convert to C long, value < %ld",
                 LONG_MIN);
    return -1;
}

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions