Skip to content

Commit 9d023a8

Browse files
committed
gh-153569: handle carriage returns after comments
1 parent b0926fb commit 9d023a8

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

Lib/test/test_tokenize.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2580,6 +2580,16 @@ def test_carriage_return_after_debug_comment(self):
25802580
tokens = self._get_tokens(f"{prefix}'''{{x=# comment\r}}'''")
25812581
self.assertEqual(tokens[4].string, "# comment\r}")
25822582

2583+
for extra_tokens in (False, True):
2584+
with self.assertRaises(tokenize.TokenError) as caught:
2585+
self._get_tokens(
2586+
f"{prefix}'{{#\r!", extra_tokens=extra_tokens
2587+
)
2588+
self.assertEqual(
2589+
caught.exception.args,
2590+
("unexpected EOF in multi-line statement", (1, 7)),
2591+
)
2592+
25832593
def test_escaped_fstring_brace_has_a_position_gap(self):
25842594
tokens = self._get_tokens('f"a{{"', extra_tokens=True)
25852595
self.assertEqual(

Parser/lexer/lexer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ _PyLexer_get_normal(struct tok_state *tok, ftstring_state *current, struct token
338338

339339
if (current != NULL) {
340340
const char *comment_end = tok->cur;
341-
if (c == '\n') {
341+
if (c == '\n' || c == '\r') {
342342
comment_end--;
343343
}
344344
if (_PyLexer_record_ftstring_comment(

0 commit comments

Comments
 (0)