]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-126139: Improve error message location for future statement with unknown...
authorBrian Schubert <brianm.schubert@gmail.com>
Wed, 30 Oct 2024 00:54:32 +0000 (20:54 -0400)
committerGitHub <noreply@github.com>
Wed, 30 Oct 2024 00:54:32 +0000 (00:54 +0000)
(cherry picked from commit 224c370a3680132997f1e43d20a3b4ca95a060ab)

Lib/test/test_exceptions.py
Lib/test/test_future_stmt/test_future.py
Misc/NEWS.d/next/Core_and_Builtins/2024-10-29-15-17-31.gh-issue-126139.B4OQ8a.rst [new file with mode: 0644]
Python/future.c

index c5f4b892efb50f39876ddc8243fe089cf5dc946a..72c86eecae25393ae1533c8770307b502c976f91 100644 (file)
@@ -316,8 +316,8 @@ class ExceptionTests(unittest.TestCase):
         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)
index 5a85e08e19b7b65f4e5c165de157d143e14a2013..a06e0510f12dde5ef1da8e54c8d31dda9abe6a21 100644 (file)
@@ -55,7 +55,7 @@ class FutureTest(unittest.TestCase):
     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:
@@ -80,12 +80,12 @@ class FutureTest(unittest.TestCase):
     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:
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2024-10-29-15-17-31.gh-issue-126139.B4OQ8a.rst b/Misc/NEWS.d/next/Core_and_Builtins/2024-10-29-15-17-31.gh-issue-126139.B4OQ8a.rst
new file mode 100644 (file)
index 0000000..278971b
--- /dev/null
@@ -0,0 +1,2 @@
+Provide better error location when attempting to use a :term:`future
+statement <__future__>` with an unknown future feature.
index d56f7330964684222ecacdb1165be1a342d75e33..998a7f740eac01b14691772df6f6638463a64b1a 100644 (file)
@@ -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;
         }
     }