Skip to content

Commit c90a862

Browse files
authored
gh-104866: Tokenize should emit NEWLINE after exiting block with comment (#104870)
1 parent 9d457e1 commit c90a862

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

Lib/test/test_tokenize.py

+17
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,23 @@ async def bar(): pass
10571057
DEDENT '' (6, 12) (6, 12)
10581058
""")
10591059

1060+
def test_newline_after_parenthesized_block_with_comment(self):
1061+
self.check_tokenize('''\
1062+
[
1063+
# A comment here
1064+
1
1065+
]
1066+
''', """\
1067+
OP '[' (1, 0) (1, 1)
1068+
NL '\\n' (1, 1) (1, 2)
1069+
COMMENT '# A comment here' (2, 4) (2, 20)
1070+
NL '\\n' (2, 20) (2, 21)
1071+
NUMBER '1' (3, 4) (3, 5)
1072+
NL '\\n' (3, 5) (3, 6)
1073+
OP ']' (4, 0) (4, 1)
1074+
NEWLINE '\\n' (4, 1) (4, 2)
1075+
""")
1076+
10601077
class GenerateTokensTest(TokenizeTest):
10611078
def check_tokenize(self, s, expected):
10621079
# Format the tokens in s in a table format.

Parser/tokenizer.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -2007,6 +2007,9 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
20072007
tok->atbol = 1;
20082008
if (blankline || tok->level > 0) {
20092009
if (tok->tok_extra_tokens) {
2010+
if (tok->comment_newline) {
2011+
tok->comment_newline = 0;
2012+
}
20102013
p_start = tok->start;
20112014
p_end = tok->cur;
20122015
return MAKE_TOKEN(NL);
@@ -2015,9 +2018,9 @@ tok_get_normal_mode(struct tok_state *tok, tokenizer_mode* current_tok, struct t
20152018
}
20162019
if (tok->comment_newline && tok->tok_extra_tokens) {
20172020
tok->comment_newline = 0;
2018-
p_start = tok->start;
2019-
p_end = tok->cur;
2020-
return MAKE_TOKEN(NL);
2021+
p_start = tok->start;
2022+
p_end = tok->cur;
2023+
return MAKE_TOKEN(NL);
20212024
}
20222025
p_start = tok->start;
20232026
p_end = tok->cur - 1; /* Leave '\n' out of the string */

0 commit comments

Comments
 (0)