Skip to content

[3.9] bpo-40847: Consider a line with only a LINECONT a blank line (GH-20769) #20795

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
merged 1 commit into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Lib/test/test_peg_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ def f():
('dict_comp', '{x:1 for x in a}'),
('dict_comp_if', '{x:1+2 for x in a if b}'),
('dict_empty', '{}'),
('empty_line_after_linecont',
r'''
pass
\

pass
'''),
('for',
'''
for i in a:
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,20 @@ def test_kwargs_last3(self):
"iterable argument unpacking follows "
"keyword argument unpacking")

def test_empty_line_after_linecont(self):
# See issue-40847
s = r"""\
pass
\

pass
"""
try:
compile(s, '<string>', 'exec')
except SyntaxError:
self.fail("Empty line after a line continuation character is valid.")


def test_main():
support.run_unittest(SyntaxTestCase)
from test import test_syntax
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix a bug where a line with only a line continuation character is not considered a blank line at tokenizer level.
In such cases, more than a single `NEWLINE` token was emitted. The old parser was working around the issue,
but the new parser threw a :exc:`SyntaxError` for valid input due to this. For example, an empty line following
a line continuation character was interpreted as a :exc:`SyntaxError`.
3 changes: 2 additions & 1 deletion Parser/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1203,8 +1203,9 @@ tok_get(struct tok_state *tok, const char **p_start, const char **p_end)
}
}
tok_backup(tok, c);
if (c == '#' || c == '\n') {
if (c == '#' || c == '\n' || c == '\\') {
/* Lines with only whitespace and/or comments
and/or a line continuation character
shouldn't affect the indentation and are
not passed to the parser as NEWLINE tokens,
except *totally* empty lines in interactive
Expand Down