-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
bpo-30529: Fix errors for invalid whitespaces in f-string subexpressions. #1888
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
bpo-30529: Fix errors for invalid whitespaces in f-string subexpressions. #1888
Conversation
…ons. 'invalid character in identifier' now is raised instead of 'f-string: empty expression not allowed' if a subexpression contains only whitespaces and they are not accepted by Python parser.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general, it looks good, although I think it could be improved with the few tweaks I've mentioned. Not needing to convert from UTF-8 is definitely an improvement. Thanks!
Python/ast.c
Outdated
all_whitespace = 0; | ||
for (s = expr_start; s != expr_end; s++) { | ||
char c = *s; | ||
if (!(c == ' ' || c == '\t' || c == '\n' || c == '\014')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't '\014' (formfeed) be better written as '\f'?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Python parser uses '\014'
. I prefer writing it as '\f'
too.
Lib/test/test_fstring.py
Outdated
@@ -279,6 +279,7 @@ def test_missing_expression(self): | |||
"f'{ !r}'", | |||
"f'{10:{ }}'", | |||
"f' { } '", | |||
"f'''{\t\x0c\r\n}'''", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
\x0c is \f, isn't it? I'd use that, instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. For unknown reasons the repr of '\f'
is "'\x0c'"
.
Lib/test/test_fstring.py
Outdated
@@ -300,6 +301,9 @@ def test_missing_expression(self): | |||
"f'{:x'", | |||
]) | |||
|
|||
self.assertAllRaise(SyntaxError, 'invalid character in identifier', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment here that says that eval("\xa0") raises SyntaxError with the "invalid character in identifier" text? I think that it's important to know that what you're doing here is trying to match the Python parser. (I realize that, with your change, you're "matching" by actually using the Python parser.)
Or, maybe also add 'eval("\xa0")' to the array to make it obvious that we want the same error raised in both cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
for (i = 0; i < len; i++) { | ||
if (!Py_UNICODE_ISSPACE(PyUnicode_READ(kind, data, i))) { | ||
all_whitespace = 0; | ||
for (s = expr_start; s != expr_end; s++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment here that you're skipping the same whitespace that the Python parser recognizes, and that's why we're specifically not calling Py_UNICODE_ISSPACE? I can see someone (might be me!) and not realizing that Python itself doesn't use Py_UNICODE_ISSPACE and trying to add it back.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
…pressions. (pythonGH-1888) 'invalid character in identifier' now is raised instead of 'f-string: empty expression not allowed' if a subexpression contains only whitespaces and they are not accepted by Python parser. (cherry picked from commit 2e9cd58)
'invalid character in identifier' now is raised instead of
'f-string: empty expression not allowed' if a subexpression contains
only whitespaces and they are not accepted by Python parser.