python / python/cpython

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

Open
#141,578 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core topic-C-API type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Feature or enhancement

Proposal:

Both functions have old TODO comments:
https://github.com/python/cpython/blob/453d886f8592d2f4346d5621b1e4ff31c24338d5/Objects/longobject.c#L671-L676
https://github.com/python/cpython/blob/453d886f8592d2f4346d5621b1e4ff31c24338d5/Objects/longobject.c#L688-L694

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
  • gh-141585

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in Objects/longobject.c at the TODO comments linked in the issue, and review linked PR gh-141585 for the work already underway. Done means either removing those TODOs or providing distinct overflow messages for PyLong_AsLong() and PyLong_AsInt().

Written by the indexing model from the issue text.

Assessment

Tech stack
c, python
Domain
api
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.