Skip to content

Docs: the Reference says integer literals have no length limit, but some long decimal literals fail to compile #162602

Backlog

Description

@Robertboy18

Documentation

The Reference (https://docs.python.org/dev/reference/lexical_analysis.html#integers) says "There is no limit for the length of integer literals apart from what can be stored in available memory". With the default integer string conversion limit (gh-95778), the 4301-digit decimal literal below doesn't compile, while the longer hexadecimal literal does:

for text in ["1" + "0" * 4299, "1" + "0" * 4300, "0x" + "f" * 5000]:
    try:
        compile(text, "<s>", "eval")
        print(len(text), "characters: compiles")
    except SyntaxError as e:
        print(len(text), "characters: SyntaxError:", e.msg.split(":")[0])

CPython 3.15.2 and main (639c34b):

4300 characters: compiles
4301 characters: SyntaxError: Exceeds the limit (4300 digits) for integer string conversion
5002 characters: compiles

The limit's section says such source "will encounter an error during parsing"; the Reference could say so and link there. I found it while writing a Lean spec of Python.

Activity

  1. added
    AI-needs-explanation
    on Nov 25, 2026
  2. bedevere-app commented on Nov 25, 2026

    @bedevere-app

    Thank you for taking the time to open this issue. Python is a community-driven project, and we want it to be a welcoming place to learn and collaborate.

    This issue appears to have been written with AI assistance. Please explain your issue in your own words and describe how you checked the information you provided. This helps us understand your issue and leaves more time to address other issues.

    Please read our guidelines for using AI tools.

  3. added
    docs
    on Nov 25, 2026
  4. skirpichev commented on Nov 25, 2026

    @skirpichev
    Member

    Not sure that I understand reasons for AI-related tag. Issue looks clear and well written.

    The problem I see is that the language reference is for the Python language, not just CPython. While Integer string conversion length limitation says:

    CPython has a global limit for converting between int and str to mitigate denial of service attacks. This limit only applies to decimal or other non-power-of-two number bases. Hexadecimal, octal, and binary conversions are unlimited. The limit can be configured.

    In this wording the feature might look as CPython-specific. Though, both PyPy and GraalPy implement same API, with same defaults.

    Maybe it should be optional for alternative implementations? With a better bigint library than we have in the CPython internals, say with the GNU GMP - this limitation is not helpful.

  5. removed
    AI-needs-explanation
    on Nov 25, 2026
  6. picnixz commented on Nov 25, 2026

    @picnixz
    Member

    I would have expected that the conversion int<->str has limits as we currently do but hardcoding a literal should not cause any parsing issue (where is the string in my code except that the code itself is parsed as a string).

    That being said, I don't know how we can improve the docs without saying "well the specs say that but CPython and many other implementations do differently for practical reasons, use hexadecimal strings if you need to encode very large integers". I think this doesn't ring well.

  7. asvetlov commented on Nov 25, 2026

    @asvetlov
    Contributor

    @skirpichev ~10 issues in different CPython domains were created by @Robertboy18 (first time contributor) in a batch.
    That's why I've added the label.
    If all issues are valuable -- I have no objections. But please review them carefully.

  8. Robertboy18 commented on Nov 25, 2026

    @Robertboy18
    Author

    @asvetlov @picnixz sorry for the batch! These all came out of a Lean formalization of the language reference I'm working on, and it has been batching them up. I'll review each one more carefully and slow it down from here. Thanks for looking at them, I just wanted to help get these fixed fast :)

  9. picnixz commented on Nov 25, 2026

    @picnixz
    Member

    I just wanted to help get these fixed fast

    This won't likely happena s they are not critical bugs. While maybe trivial to fix by rewording, we have too many PRs to review before we address those (which I believe are rather all low-priority).

  10. skirpichev commented on Nov 25, 2026

    @skirpichev
    Member

    ~10 issues in different CPython domains were created

    Ah, just for a bulk creation. But was any concrete issue unclear or misleading?

    I would have expected that the conversion int<->str has limits as we currently do but hardcoding a literal should not cause any parsing issue (where is the string in my code except that the code itself is parsed as a string).

    So, do you think we can relax limitation here? It's coming from using PyLong_FromString() in Parser/ and looks to be intentional (reflected in docs, in "Caution" mark). But maybe it was here just to simplify the patch...

    I don't see how this constraint helps for security in case of parsing source code. But see that it can complicate docs for no good reason.

  11. asvetlov commented on Nov 25, 2026

    @asvetlov
    Contributor

    @skirpichev don't get me wrong. I didn't say that this batch had misleading or malformed issues.
    My point is closer to what @picnixz wrote.

    We, as Python core developers with commit rights, are responsible for responding to issues and merging pull requests. We should read proposed changes carefully with our own eyes before reacting.

    Automated issues and PRs are very common nowadays, and they are a red flag for me. The proposal may be worthwhile, but a machine can generate these faster than I can analyze them. Each one usually needs review and comments, and eventually acceptance or rejection.

    The strategy that works for outside contributors is called "communication", especially for a first issue. Pick a target, work on it, and talk with the reviewers. Once it's merged, you know a few core devs who will probably help with your next review, and so on.

    Filing 10 issues at once as a newcomer nobody knows yet is a genuine anti-pattern to me.

    Again, please don't get me wrong. I use Claude Code on my daily basis, but I always check its output by my own eyes before posting it to public domain.

    If I review 10 different AI bug reports generated by my local machine -- I spend some time on each before pressing "send" button. That was my source of frustration.

  12. skirpichev commented on Nov 25, 2026

    @skirpichev
    Member

    We, as Python core developers with commit right

    Well, I'm not and may ask dumb questions, do silly thing and even invade Poland! :)

    Automated issues and PRs are very common nowadays, and they are a red flag for me. The proposal may be worthwhile, but a machine can generate these faster than I can analyze them.

    That's a valid concern, but I would like to be interpreting CPython AI guidelines correctly. Rule one:

    Use the AI policy labels for issues and PRs that don’t follow the guidelines for using AI tools. Don’t apply them only because AI tools were used, or because the author disclosed using them.

    Rule two:

    Authors must review the work done by AI tooling in detail to ensure it actually makes sense before proposing it as a PR or filing it as an issue.

    My interpretation:

    1. Just a mass generation of bugreports with AI help is not a sufficient condition to put any AI-* label.
    2. We assume good faith and that issues, posted with AI - actually were reviewed by human being.
    3. Iff we see any signs of violation of pt. 2 - that is a reason to apply labels (or just close issues).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docs

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions