https://github.com/python/cpython/commit/487a51a1b981211c091caa2026da81cc0df7096c
commit: 487a51a1b981211c091caa2026da81cc0df7096c
branch: 3.12
author: Nice Zombies <[email protected]>
committer: pablogsal <[email protected]>
date: 2024-12-11T09:09:34Z
summary:

[3.12] gh-111609: `end_offset` is ignored in subclasses of SyntaxError (#127554)

* `end_offset` is ignored in subclasses of SyntaxError

* 📜🤖 Added by blurb_it.

* Add test

---------

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2024-12-03-12-17-36.gh-issue-111609.UHpQY9.rst
M Lib/test/test_exceptions.py
M Python/pythonrun.c

diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index 72c86eecae2539..f4f2011a51aed7 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -2195,6 +2195,22 @@ def test_range_of_offsets(self):
                     self.assertIn(expected, err.getvalue())
                     the_exception = exc
 
+    def test_subclass(self):
+        class MySyntaxError(SyntaxError):
+            pass
+
+        try:
+            raise MySyntaxError("bad bad", ("bad.py", 1, 2, "abcdefg", 1, 7))
+        except SyntaxError as exc:
+            with support.captured_stderr() as err:
+                sys.__excepthook__(*sys.exc_info())
+            self.assertIn("""
+  File "bad.py", line 1
+    abcdefg
+     ^^^^^
+""", err.getvalue())
+
+
     def test_encodings(self):
         self.addCleanup(unlink, TESTFN)
         source = (
diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2024-12-03-12-17-36.gh-issue-111609.UHpQY9.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2024-12-03-12-17-36.gh-issue-111609.UHpQY9.rst
new file mode 100644
index 00000000000000..1f63a6f510cff3
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2024-12-03-12-17-36.gh-issue-111609.UHpQY9.rst
@@ -0,0 +1 @@
+Respect *end_offset* in :exc:`SyntaxError` subclasses.
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 5f3d249df45814..cf84573a8e6147 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -538,43 +538,37 @@ parse_syntax_error(PyObject *err, PyObject **message, 
PyObject **filename,
         *offset = hold;
     }
 
-    if (Py_TYPE(err) == (PyTypeObject*)PyExc_SyntaxError) {
-        v = PyObject_GetAttr(err, &_Py_ID(end_lineno));
-        if (!v) {
-            PyErr_Clear();
-            *end_lineno = *lineno;
-        }
-        else if (v == Py_None) {
-            *end_lineno = *lineno;
-            Py_DECREF(v);
-        } else {
-            hold = PyLong_AsSsize_t(v);
-            Py_DECREF(v);
-            if (hold < 0 && PyErr_Occurred())
-                goto finally;
-            *end_lineno = hold;
-        }
-
-        v = PyObject_GetAttr(err, &_Py_ID(end_offset));
-        if (!v) {
-            PyErr_Clear();
-            *end_offset = -1;
-        }
-        else if (v == Py_None) {
-            *end_offset = -1;
-            Py_DECREF(v);
-        } else {
-            hold = PyLong_AsSsize_t(v);
-            Py_DECREF(v);
-            if (hold < 0 && PyErr_Occurred())
-                goto finally;
-            *end_offset = hold;
-        }
-    } else {
-        // SyntaxError subclasses
+    v = PyObject_GetAttr(err, &_Py_ID(end_lineno));
+    if (!v) {
+        PyErr_Clear();
         *end_lineno = *lineno;
+    }
+    else if (v == Py_None) {
+        *end_lineno = *lineno;
+        Py_DECREF(v);
+    } else {
+        hold = PyLong_AsSsize_t(v);
+        Py_DECREF(v);
+        if (hold < 0 && PyErr_Occurred())
+            goto finally;
+        *end_lineno = hold;
+    }
+
+    v = PyObject_GetAttr(err, &_Py_ID(end_offset));
+    if (!v) {
+        PyErr_Clear();
         *end_offset = -1;
     }
+    else if (v == Py_None) {
+        *end_offset = -1;
+        Py_DECREF(v);
+    } else {
+        hold = PyLong_AsSsize_t(v);
+        Py_DECREF(v);
+        if (hold < 0 && PyErr_Occurred())
+            goto finally;
+        *end_offset = hold;
+    }
 
     v = PyObject_GetAttr(err, &_Py_ID(text));
     if (!v)

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to