Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-33305: Improve SyntaxError for invalid numerical literals. #6517

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

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Apr 18, 2018

Copy link
Contributor

@SylvainDe SylvainDe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to add the value of c in this message as well ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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[]?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know, I'd expect "invalid character '%c' in octal literal" to be useful in all cases.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ho, indeed, I didn't think about all these issues...

@serhiy-storchaka
Copy link
Member Author

I'm going to merge this PR if there are no other suggestions.

Copy link
Member

@mdickinson mdickinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new messages look great to me; I haven't reviewed the C code changes in detail.

@serhiy-storchaka serhiy-storchaka merged commit cf7303e into python:master Jul 9, 2018
@serhiy-storchaka serhiy-storchaka deleted the compiler-invalid-numbers-errors branch July 9, 2018 12:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants