Skip to content

Commit 5a0ab76

Browse files
committed
gh-156047: Handle incomplete token streams
1 parent 03d5c04 commit 5a0ab76

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

Lib/traceback.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,11 +1459,15 @@ def _find_keyword_typos(self):
14591459
return # Original code compiles or is incomplete - can't validate fixes
14601460

14611461
error_lines = error_code.splitlines()
1462-
tokens = [
1463-
token
1464-
for token in tokenize.generate_tokens(io.StringIO(error_code).readline)
1465-
if token.type == tokenize.NAME
1466-
]
1462+
tokens = []
1463+
token_stream = tokenize.generate_tokens(io.StringIO(error_code).readline)
1464+
try:
1465+
for token in token_stream:
1466+
if token.type == tokenize.NAME:
1467+
tokens.append(token)
1468+
except tokenize.TokenError:
1469+
# Incomplete input can still contain useful tokens.
1470+
pass
14671471
# Look for typos on the reported error line first. If the parser only
14681472
# fails after an earlier typo, the remaining lines are still searched.
14691473
the_end = end_line if line == 0 else end_line + 1

0 commit comments

Comments
 (0)