Skip to content

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

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 3 commits into from
Dec 11, 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
16 changes: 16 additions & 0 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2195,6 +2195,22 @@ def test_range_of_offsets(self):
self.assertIn(expected, err.getvalue())
the_exception = exc

def test_subclass(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also add this test to main if is not there already?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a PR: #127830

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 = (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Respect *end_offset* in :exc:`SyntaxError` subclasses.
62 changes: 28 additions & 34 deletions Python/pythonrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading