Skip to content

Commit e1525a5

Browse files
brianschubertpull[bot]
authored andcommitted
gh-126139: Improve error message location for future statement with unknown feature (#126140)
1 parent faeaade commit e1525a5

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

Lib/test/test_exceptions.py

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

321321
# Errors thrown by future.c
322-
check('from __future__ import doesnt_exist', 1, 1)
323-
check('from __future__ import braces', 1, 1)
322+
check('from __future__ import doesnt_exist', 1, 24)
323+
check('from __future__ import braces', 1, 24)
324324
check('x=1\nfrom __future__ import division', 2, 1)
325325
check('foo(1=2)', 1, 5)
326326
check('def f():\n x, y: int', 2, 3)

Lib/test/test_future_stmt/test_future.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def test_unknown_future_flag(self):
8888
"""
8989
self.assertSyntaxError(
9090
code, lineno=2,
91-
message='future feature rested_snopes is not defined',
91+
message='future feature rested_snopes is not defined', offset=24,
9292
)
9393

9494
def test_future_import_not_on_top(self):
@@ -137,19 +137,19 @@ def test_future_import_star(self):
137137
code = """
138138
from __future__ import *
139139
"""
140-
self.assertSyntaxError(code, message='future feature * is not defined')
140+
self.assertSyntaxError(code, message='future feature * is not defined', offset=24)
141141

142142
def test_future_import_braces(self):
143143
code = """
144144
from __future__ import braces
145145
"""
146146
# Congrats, you found an easter egg!
147-
self.assertSyntaxError(code, message='not a chance')
147+
self.assertSyntaxError(code, message='not a chance', offset=24)
148148

149149
code = """
150150
from __future__ import nested_scopes, braces
151151
"""
152-
self.assertSyntaxError(code, message='not a chance')
152+
self.assertSyntaxError(code, message='not a chance', offset=39)
153153

154154
def test_module_with_future_import_not_on_top(self):
155155
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
@@ -41,12 +41,20 @@ future_check_features(_PyFutureFeatures *ff, stmt_ty s, PyObject *filename)
4141
} else if (strcmp(feature, "braces") == 0) {
4242
PyErr_SetString(PyExc_SyntaxError,
4343
"not a chance");
44-
PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset + 1);
44+
PyErr_RangedSyntaxLocationObject(filename,
45+
name->lineno,
46+
name->col_offset + 1,
47+
name->end_lineno,
48+
name->end_col_offset + 1);
4549
return 0;
4650
} else {
4751
PyErr_Format(PyExc_SyntaxError,
4852
UNDEFINED_FUTURE_FEATURE, feature);
49-
PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset + 1);
53+
PyErr_RangedSyntaxLocationObject(filename,
54+
name->lineno,
55+
name->col_offset + 1,
56+
name->end_lineno,
57+
name->end_col_offset + 1);
5058
return 0;
5159
}
5260
}

0 commit comments

Comments
 (0)