bpo-33305: Improve SyntaxError for invalid numerical literals.#6517
Conversation
SylvainDe
left a comment
There was a problem hiding this comment.
This could be great.
Out of curiosity, would it make sense to add the character causing troubles in all cases ?
| "invalid digit '%c' in octal literal", c); | ||
| } | ||
| else { | ||
| return syntaxerror(tok, "invalid octal literal"); |
There was a problem hiding this comment.
Would it be possible to add the value of c in this message as well ?
There was a problem hiding this comment.
This error is raised in the case if an underscore or 0o is not followed by a digit. What error messages could be helpful for 0o+2, 0o + 2, (2+0o), 0or[]?
There was a problem hiding this comment.
I don't know, I'd expect "invalid character '%c' in octal literal" to be useful in all cases.
There was a problem hiding this comment.
It is easy to report only if an invalid digit (in the range 2-9 or 8-9) is occurred. In general case there are much subtle details, handling them will complicate the code too much:
- Not always an invalid character exists. This error can be raised at the end of the input.
- It can be non-ASCII. In this case we need to decode a multibyte UTF-8 for getting a character.
- It can be non-printable.
- Even if it is printable from the Unicode's point of view, it can look indistinguishably from other characters. For example, non-breakable space character looks like an ordinary space for humans, but not for the Python parser.
- Even in ASCII there are non-printable characters, or characters that need special handling: tab, newline, single quote, backslash, ...
It may be worth to produce more specialized error message for some cases, but just reporting the next invalid character is no a way.
There was a problem hiding this comment.
Ho, indeed, I didn't think about all these issues...
|
I'm going to merge this PR if there are no other suggestions. |
mdickinson
left a comment
There was a problem hiding this comment.
The new messages look great to me; I haven't reviewed the C code changes in detail.
https://bugs.python.org/issue33305