Skip to content

Commit 336a69d

Browse files
committed
gh-137078: Fix keyword typo recognition when executed over files
1 parent 85ec3b3 commit 336a69d

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

Grammar/python.gram

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,12 @@ func_type[mod_ty]: '(' a=[type_expressions] ')' '->' b=expression NEWLINE* ENDMA
9494
# GENERAL STATEMENTS
9595
# ==================
9696

97-
statements[asdl_stmt_seq*]: a=statement+ { _PyPegen_register_stmts(p, (asdl_stmt_seq*)_PyPegen_seq_flatten(p, a)) }
97+
statements[asdl_stmt_seq*]: a=statement+ { (asdl_stmt_seq*)_PyPegen_seq_flatten(p, a) }
9898

9999
statement[asdl_stmt_seq*]:
100-
| a=compound_stmt { (asdl_stmt_seq*)_PyPegen_singleton_seq(p, a) }
100+
| a=compound_stmt { _PyPegen_register_stmts(p ,
101+
(asdl_stmt_seq*)_PyPegen_singleton_seq(p, a)
102+
) }
101103
| a[asdl_stmt_seq*]=simple_stmts { a }
102104

103105
single_compound_stmt[asdl_stmt_seq*]:

Lib/traceback.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,7 +1310,6 @@ def _find_keyword_typos(self):
13101310
lines = source.splitlines()
13111311

13121312
error_code = lines[line -1 if line > 0 else 0:end_line]
1313-
error_code[0] = error_code[0][offset:]
13141313
error_code = textwrap.dedent('\n'.join(error_code))
13151314

13161315
# Do not continue if the source is too large
@@ -1326,7 +1325,8 @@ def _find_keyword_typos(self):
13261325
if token.type != tokenize.NAME:
13271326
continue
13281327
# Only consider NAME tokens on the same line as the error
1329-
if from_filename and token.start[0]+line != end_line+1:
1328+
the_end = end_line if line == 0 else end_line + 1
1329+
if from_filename and token.start[0]+line != the_end:
13301330
continue
13311331
wrong_name = token.string
13321332
if wrong_name in keyword.kwlist:

Parser/action_helpers.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,6 +1936,9 @@ _PyPegen_register_stmts(Parser *p, asdl_stmt_seq* stmts) {
19361936
return stmts;
19371937
}
19381938
stmt_ty last_stmt = asdl_seq_GET(stmts, len - 1);
1939+
if (p->last_stmt_location.lineno > last_stmt->lineno) {
1940+
return stmts;
1941+
}
19391942
p->last_stmt_location.lineno = last_stmt->lineno;
19401943
p->last_stmt_location.col_offset = last_stmt->col_offset;
19411944
p->last_stmt_location.end_lineno = last_stmt->end_lineno;

Parser/parser.c

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)