Skip to content

[3.12] gh-126139: Improve error message location for future statement with unknown feature (GH-126140) #126160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,8 @@ def baz():
check('def f():\n global x\n nonlocal x', 2, 3)

# Errors thrown by future.c
check('from __future__ import doesnt_exist', 1, 1)
check('from __future__ import braces', 1, 1)
check('from __future__ import doesnt_exist', 1, 24)
check('from __future__ import braces', 1, 24)
check('x=1\nfrom __future__ import division', 2, 1)
check('foo(1=2)', 1, 5)
check('def f():\n x, y: int', 2, 3)
Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_future_stmt/test_future.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def test_future_multiple_features(self):
def test_badfuture3(self):
with self.assertRaises(SyntaxError) as cm:
from test.test_future_stmt import badsyntax_future3
self.check_syntax_error(cm.exception, "badsyntax_future3", 3)
self.check_syntax_error(cm.exception, "badsyntax_future3", 3, 24)

def test_badfuture4(self):
with self.assertRaises(SyntaxError) as cm:
Expand All @@ -80,12 +80,12 @@ def test_badfuture7(self):
def test_badfuture8(self):
with self.assertRaises(SyntaxError) as cm:
from test.test_future_stmt import badsyntax_future8
self.check_syntax_error(cm.exception, "badsyntax_future8", 3)
self.check_syntax_error(cm.exception, "badsyntax_future8", 3, 24)

def test_badfuture9(self):
with self.assertRaises(SyntaxError) as cm:
from test.test_future_stmt import badsyntax_future9
self.check_syntax_error(cm.exception, "badsyntax_future9", 3)
self.check_syntax_error(cm.exception, "badsyntax_future9", 3, 39)

def test_badfuture10(self):
with self.assertRaises(SyntaxError) as cm:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Provide better error location when attempting to use a :term:`future
statement <__future__>` with an unknown future feature.
12 changes: 10 additions & 2 deletions Python/future.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,20 @@ future_check_features(PyFutureFeatures *ff, stmt_ty s, PyObject *filename)
} else if (strcmp(feature, "braces") == 0) {
PyErr_SetString(PyExc_SyntaxError,
"not a chance");
PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset + 1);
PyErr_RangedSyntaxLocationObject(filename,
name->lineno,
name->col_offset + 1,
name->end_lineno,
name->end_col_offset + 1);
return 0;
} else {
PyErr_Format(PyExc_SyntaxError,
UNDEFINED_FUTURE_FEATURE, feature);
PyErr_SyntaxLocationObject(filename, s->lineno, s->col_offset + 1);
PyErr_RangedSyntaxLocationObject(filename,
name->lineno,
name->col_offset + 1,
name->end_lineno,
name->end_col_offset + 1);
return 0;
}
}
Expand Down
Loading