Skip to content

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

Merged

Conversation

serhiy-storchaka
Copy link
Member

'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.

…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.
Copy link
Member

@ericvsmith ericvsmith left a 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')) {
Copy link
Member

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'?

Copy link
Member Author

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.

@@ -279,6 +279,7 @@ def test_missing_expression(self):
"f'{ !r}'",
"f'{10:{ }}'",
"f' { } '",
"f'''{\t\x0c\r\n}'''",
Copy link
Member

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.

Copy link
Member Author

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'".

@@ -300,6 +301,9 @@ def test_missing_expression(self):
"f'{:x'",
])

self.assertAllRaise(SyntaxError, 'invalid character in identifier',
Copy link
Member

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?

Copy link
Member Author

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++) {
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@serhiy-storchaka serhiy-storchaka merged commit 2e9cd58 into python:master Jun 8, 2017
@serhiy-storchaka serhiy-storchaka deleted the f-string-whitespaces branch June 8, 2017 20:43
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this pull request Jun 8, 2017
…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)
serhiy-storchaka added a commit that referenced this pull request Jun 8, 2017
…pressions. (GH-1888) (#2013)

'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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants