Skip to content

Commit b6085cd

Browse files
committed
Preserve input failures while scanning string literals
1 parent e74e40b commit b6085cd

3 files changed

Lines changed: 10 additions & 0 deletions

File tree

‎Lib/test/test_tokenize.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2409,6 +2409,12 @@ def test_multiline_readline_chunk_with_unterminated_tail(self):
24092409
)
24102410
self.assertEqual(readline.call_count, 2)
24112411

2412+
def test_readline_memory_error_in_string(self):
2413+
readline = mock.Mock(side_effect=['"""first\n', MemoryError])
2414+
iterator = _tokenize.TokenizerIter(readline, extra_tokens=True)
2415+
with self.assertRaises(MemoryError):
2416+
next(iterator)
2417+
24122418
def test_readline_callback_is_not_read_ahead(self):
24132419
readline = mock.Mock(side_effect=["x\n", "y\n", ""])
24142420
iterator = _tokenize.TokenizerIter(readline, extra_tokens=True)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Correct missing-brace error messages for multiline f-strings and t-strings.
22
Improve tokenization of UTF-8 input split across many readline calls.
33
Preserve source order when reading files with stateful encodings.
4+
Preserve input errors when scanning string literals.

‎Parser/lexer/string.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ _PyLexer_scan_string(struct tok_state *tok, struct token *token, int c)
335335
break;
336336
}
337337
if (c == EOF || (quote_size == 1 && c == '\n')) {
338+
if (tok_failed(tok)) {
339+
return MAKE_TOKEN(ERRORTOKEN);
340+
}
338341
int end_lineno = tok->lineno;
339342
_PyTok_Loc location = tok->start_loc;
340343
const char *line = _PyLexer_BufferPointer(tok, tok->start) - location.byte_col;

0 commit comments

Comments
 (0)