New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PyLong_FromString documentation wrong on numbers with leading zero and base=0 #73937
Comments
Calling PyLong_FromString(str, NULL, 0) fails, if str is a string containing a decimal number with leading zeros, even though such strings should be parsed as decimal numbers according to the documentation: "If base is 0, the radix will be determined based on the leading characters of str: if str starts with '0x' or '0X', radix 16 will be used; if str starts with '0o' or '0O', radix 8 will be used; if str starts with '0b' or '0B', radix 2 will be used; otherwise radix 10 will be used" Examples: Version information: |
My guess is this is supposed to emulate (or is actually the implementation of) the "int" constructor and the Python syntax. In these cases, numbers with leading zeros are disallowed. This was to help with Python 2 porting, where a leading zero specified an octal number. >>> 010
010
^
SyntaxError: invalid token
>>> int("010", 0)
ValueError: invalid literal for int() with base 0: '010' Maybe it is better to fix the documentation. |
Yes, PyLong_FromString is directly used by the implementation of int, and is also used in parsing of numeric integer literals in source: Line 4084 in cb41b27
So I agree that this is a documentation bug. There's also no mention of the support for underscores in the documentation. |
I have a pull request ready for the documentation, but I didn't understand the underscore usage, so I couldn't add that. If you explain it, then I can try to add it. |
String arguments to int are quoted int literals. From For your patch, I would summarize this by expanding 'Leading spaces are ignored.' to the following (in patch comment also). |
Thank you. I've added that change. For the backporting, I think that would only be applicable to 3.6 and 3.7? |
Underscores are only applicable to 3.6+, but the original concern about leading zeros applies to 3.5. On Git Hub I suggested dropping the details and just referring to the Lexical Analysis section <https://docs.python.org/3.5/reference/lexical_analysis.html#integer-literals\> for the details. FWIW here is my understanding of integer literals (with base=0):
If you want to spell out the rules, in my mind there are four special prefixes, 0x, 0b, 0o and 0, and the default is decimal if none of those prefixes apply. |
I merged the PR, backported it to 3.5 and 3.6, and added Cheryl to Misc/ACKS. Thanks everyone :) |
Oh, I didn't expect that. That is so cool! Thanks Mariatta. :-) |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: