mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
gh-133541: Handle SyntaxError raised by the tokenizer on user input (#133606)
This commit is contained in:
parent
6ce469dcba
commit
b2fabce6ab
3 changed files with 24 additions and 0 deletions
|
@ -102,6 +102,8 @@ def gen_colors(buffer: str) -> Iterator[ColorSpan]:
|
|||
for color in gen_colors_from_token_stream(gen, line_lengths):
|
||||
yield color
|
||||
last_emitted = color
|
||||
except SyntaxError:
|
||||
return
|
||||
except tokenize.TokenError as te:
|
||||
yield from recover_unterminated_string(
|
||||
te, line_lengths, last_emitted, buffer
|
||||
|
|
|
@ -497,6 +497,26 @@ class TestReaderInColor(ScreenEqualMixin, TestCase):
|
|||
self.assert_screen_equal(reader, code, clean=True)
|
||||
self.assert_screen_equal(reader, expected)
|
||||
|
||||
def test_syntax_highlighting_indentation_error(self):
|
||||
code = dedent(
|
||||
"""\
|
||||
def unfinished_function():
|
||||
var = 1
|
||||
oops
|
||||
"""
|
||||
)
|
||||
expected = dedent(
|
||||
"""\
|
||||
{k}def{z} {d}unfinished_function{z}{o}({z}{o}){z}{o}:{z}
|
||||
var {o}={z} {n}1{z}
|
||||
oops
|
||||
"""
|
||||
).format(**colors)
|
||||
events = code_to_events(code)
|
||||
reader, _ = handle_all_events(events)
|
||||
self.assert_screen_equal(reader, code, clean=True)
|
||||
self.assert_screen_equal(reader, expected)
|
||||
|
||||
def test_control_characters(self):
|
||||
code = 'flag = "🏳️🌈"'
|
||||
events = code_to_events(code)
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
Inconsistent indentation in user input crashed the new REPL when syntax
|
||||
highlighting was active. This is now fixed.
|
Loading…
Add table
Add a link
Reference in a new issue