Skip to content

Commit b2ec37a

Browse files
authored
bpo-44063: set the missing end locations on the compiler (GH-25956)
1 parent 4a2d98a commit b2ec37a

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

Lib/test/test_syntax.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,13 @@ def case(x):
14221422
case(34)
14231423
"""
14241424
compile(code, "<string>", "exec")
1425+
1426+
def test_multiline_compiler_error_points_to_the_end(self):
1427+
self._check_error(
1428+
"call(\na=1,\na=1\n)",
1429+
"keyword argument repeated",
1430+
lineno=3
1431+
)
14251432

14261433

14271434
def test_main():

Python/compile.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4315,7 +4315,7 @@ validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
43154315
for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
43164316
keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
43174317
if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
4318-
c->u->u_col_offset = other->col_offset;
4318+
SET_LOC(c, other);
43194319
compiler_error(c, "keyword argument repeated: %U", key->arg);
43204320
return -1;
43214321
}
@@ -5368,11 +5368,15 @@ static int
53685368
compiler_visit_expr(struct compiler *c, expr_ty e)
53695369
{
53705370
int old_lineno = c->u->u_lineno;
5371+
int old_end_lineno = c->u->u_end_lineno;
53715372
int old_col_offset = c->u->u_col_offset;
5373+
int old_end_col_offset = c->u->u_end_col_offset;
53725374
SET_LOC(c, e);
53735375
int res = compiler_visit_expr1(c, e);
53745376
c->u->u_lineno = old_lineno;
5377+
c->u->u_end_lineno = old_end_lineno;
53755378
c->u->u_col_offset = old_col_offset;
5379+
c->u->u_end_col_offset = old_end_col_offset;
53765380
return res;
53775381
}
53785382

@@ -5383,7 +5387,9 @@ compiler_augassign(struct compiler *c, stmt_ty s)
53835387
expr_ty e = s->v.AugAssign.target;
53845388

53855389
int old_lineno = c->u->u_lineno;
5390+
int old_end_lineno = c->u->u_end_lineno;
53865391
int old_col_offset = c->u->u_col_offset;
5392+
int old_end_col_offset = c->u->u_end_col_offset;
53875393
SET_LOC(c, e);
53885394

53895395
switch (e->kind) {
@@ -5413,7 +5419,9 @@ compiler_augassign(struct compiler *c, stmt_ty s)
54135419
}
54145420

54155421
c->u->u_lineno = old_lineno;
5422+
c->u->u_end_lineno = old_end_lineno;
54165423
c->u->u_col_offset = old_col_offset;
5424+
c->u->u_end_col_offset = old_end_col_offset;
54175425

54185426
VISIT(c, expr, s->v.AugAssign.value);
54195427
ADDOP(c, inplace_binop(s->v.AugAssign.op));
@@ -5934,14 +5942,14 @@ validate_kwd_attrs(struct compiler *c, asdl_identifier_seq *attrs, asdl_pattern_
59345942
Py_ssize_t nattrs = asdl_seq_LEN(attrs);
59355943
for (Py_ssize_t i = 0; i < nattrs; i++) {
59365944
identifier attr = ((identifier)asdl_seq_GET(attrs, i));
5937-
c->u->u_col_offset = ((pattern_ty) asdl_seq_GET(patterns, i))->col_offset;
5945+
SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, i)));
59385946
if (forbidden_name(c, attr, Store)) {
59395947
return -1;
59405948
}
59415949
for (Py_ssize_t j = i + 1; j < nattrs; j++) {
59425950
identifier other = ((identifier)asdl_seq_GET(attrs, j));
59435951
if (!PyUnicode_Compare(attr, other)) {
5944-
c->u->u_col_offset = ((pattern_ty) asdl_seq_GET(patterns, j))->col_offset;
5952+
SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, j)));
59455953
compiler_error(c, "attribute name repeated in class pattern: %U", attr);
59465954
return -1;
59475955
}
@@ -5972,7 +5980,7 @@ compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc)
59725980
}
59735981
if (nattrs) {
59745982
RETURN_IF_FALSE(!validate_kwd_attrs(c, kwd_attrs, kwd_patterns));
5975-
c->u->u_col_offset = p->col_offset; // validate_kwd_attrs moves this
5983+
SET_LOC(c, p);
59765984
}
59775985
VISIT(c, expr, p->v.MatchClass.cls);
59785986
PyObject *attr_names;
@@ -6056,7 +6064,7 @@ compiler_pattern_mapping(struct compiler *c, pattern_ty p, pattern_context *pc)
60566064
if (key == NULL) {
60576065
const char *e = "can't use NULL keys in MatchMapping "
60586066
"(set 'rest' parameter instead)";
6059-
c->u->u_col_offset = ((pattern_ty) asdl_seq_GET(patterns, i))->col_offset;
6067+
SET_LOC(c, ((pattern_ty) asdl_seq_GET(patterns, i)));
60606068
return compiler_error(c, e);
60616069
}
60626070
if (!MATCH_VALUE_EXPR(key)) {

0 commit comments

Comments
 (0)