Skip to content

Commit e5c7543

Browse files
[3.12] gh-126139: Improve error message location for future statement with unknown feature (GH-126140) (#126160)
(cherry picked from commit 224c370)
1 parent 7812dc3 commit e5c7543

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

Lib/test/test_exceptions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,8 @@ def baz():
316316
check('def f():\n global x\n nonlocal x', 2, 3)
317317

318318
# Errors thrown by future.c
319-
check('from __future__ import doesnt_exist', 1, 1)
320-
check('from __future__ import braces', 1, 1)
319+
check('from __future__ import doesnt_exist', 1, 24)
320+
check('from __future__ import braces', 1, 24)
321321
check('x=1\nfrom __future__ import division', 2, 1)
322322
check('foo(1=2)', 1, 5)
323323
check('def f():\n x, y: int', 2, 3)

Lib/test/test_future_stmt/test_future.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def test_future_multiple_features(self):
5555
def test_badfuture3(self):
5656
with self.assertRaises(SyntaxError) as cm:
5757
from test.test_future_stmt import badsyntax_future3
58-
self.check_syntax_error(cm.exception, "badsyntax_future3", 3)
58+
self.check_syntax_error(cm.exception, "badsyntax_future3", 3, 24)
5959

6060
def test_badfuture4(self):
6161
with self.assertRaises(SyntaxError) as cm:
@@ -80,12 +80,12 @@ def test_badfuture7(self):
8080
def test_badfuture8(self):
8181
with self.assertRaises(SyntaxError) as cm:
8282
from test.test_future_stmt import badsyntax_future8
83-
self.check_syntax_error(cm.exception, "badsyntax_future8", 3)
83+
self.check_syntax_error(cm.exception, "badsyntax_future8", 3, 24)
8484

8585
def test_badfuture9(self):
8686
with self.assertRaises(SyntaxError) as cm:
8787
from test.test_future_stmt import badsyntax_future9
88-
self.check_syntax_error(cm.exception, "badsyntax_future9", 3)
88+
self.check_syntax_error(cm.exception, "badsyntax_future9", 3, 39)
8989

9090
def test_badfuture10(self):
9191
with self.assertRaises(SyntaxError) as cm:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Provide better error location when attempting to use a :term:`future
2+
statement <__future__>` with an unknown future feature.

Python/future.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,20 @@ future_check_features(PyFutureFeatures *ff, stmt_ty s, PyObject *filename)
3939
} else if (strcmp(feature, "braces") == 0) {
4040
PyErr_SetString(PyExc_SyntaxError,
4141
"not a chance");
42-
PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset + 1);
42+
PyErr_RangedSyntaxLocationObject(filename,
43+
name->lineno,
44+
name->col_offset + 1,
45+
name->end_lineno,
46+
name->end_col_offset + 1);
4347
return 0;
4448
} else {
4549
PyErr_Format(PyExc_SyntaxError,
4650
UNDEFINED_FUTURE_FEATURE, feature);
47-
PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset + 1);
51+
PyErr_RangedSyntaxLocationObject(filename,
52+
name->lineno,
53+
name->col_offset + 1,
54+
name->end_lineno,
55+
name->end_col_offset + 1);
4856
return 0;
4957
}
5058
}

0 commit comments

Comments
 (0)