[3.13] gh-146250: Fix memory leak in re-initialization of SyntaxError (GH-146251) (#146519)

* [3.13] gh-146250: Fix memory leak in re-initialization of `SyntaxError` (GH-146251)
(cherry picked from commit 0de4e08a59)

Co-authored-by: Brij Kapadia <97006829+bkap123@users.noreply.github.com>

* Minimize the changes

* Minimize the changes

* Minimize the changes

---------

Co-authored-by: Brij Kapadia <97006829+bkap123@users.noreply.github.com>
This commit is contained in:
AN Long 2026-03-31 00:06:52 +08:00 committed by GitHub
parent 0dcb625be4
commit c09ccd9c42
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 37 additions and 11 deletions

View file

@ -2436,6 +2436,30 @@ def test_incorrect_constructor(self):
args = ("bad.py", 1, 2, "abcdefg", 1)
self.assertRaises(TypeError, SyntaxError, "bad bad", args)
def test_syntax_error_memory_leak(self):
# gh-146250: memory leak with re-initialization of SyntaxError
e = SyntaxError("msg", ("file.py", 1, 2, "txt", 2, 3))
e.__init__("new_msg", ("new_file.py", 2, 3, "new_txt", 3, 4))
self.assertEqual(e.msg, "new_msg")
self.assertEqual(e.args, ("new_msg", ("new_file.py", 2, 3, "new_txt", 3, 4)))
self.assertEqual(e.filename, "new_file.py")
self.assertEqual(e.lineno, 2)
self.assertEqual(e.offset, 3)
self.assertEqual(e.text, "new_txt")
self.assertEqual(e.end_lineno, 3)
self.assertEqual(e.end_offset, 4)
e = SyntaxError("msg", ("file.py", 1, 2, "txt", 2, 3))
e.__init__("new_msg", ("new_file.py", 2, 3, "new_txt"))
self.assertEqual(e.msg, "new_msg")
self.assertEqual(e.args, ("new_msg", ("new_file.py", 2, 3, "new_txt")))
self.assertEqual(e.filename, "new_file.py")
self.assertEqual(e.lineno, 2)
self.assertEqual(e.offset, 3)
self.assertEqual(e.text, "new_txt")
self.assertIsNone(e.end_lineno)
self.assertIsNone(e.end_offset)
class TestInvalidExceptionMatcher(unittest.TestCase):
def test_except_star_invalid_exception_type(self):

View file

@ -0,0 +1 @@
Fixed a memory leak in :exc:`SyntaxError` when re-initializing it.

View file

@ -2466,22 +2466,23 @@ SyntaxError_init(PySyntaxErrorObject *self, PyObject *args, PyObject *kwds)
return -1;
}
self->end_lineno = NULL;
self->end_offset = NULL;
PyObject *filename, *lineno, *offset, *text;
PyObject *end_lineno = NULL;
PyObject *end_offset = NULL;
if (!PyArg_ParseTuple(info, "OOOO|OO",
&self->filename, &self->lineno,
&self->offset, &self->text,
&self->end_lineno, &self->end_offset)) {
&filename, &lineno,
&offset, &text,
&end_lineno, &end_offset)) {
Py_DECREF(info);
return -1;
}
Py_INCREF(self->filename);
Py_INCREF(self->lineno);
Py_INCREF(self->offset);
Py_INCREF(self->text);
Py_XINCREF(self->end_lineno);
Py_XINCREF(self->end_offset);
Py_XSETREF(self->filename, Py_NewRef(filename));
Py_XSETREF(self->lineno, Py_NewRef(lineno));
Py_XSETREF(self->offset, Py_NewRef(offset));
Py_XSETREF(self->text, Py_NewRef(text));
Py_XSETREF(self->end_lineno, Py_XNewRef(end_lineno));
Py_XSETREF(self->end_offset, Py_XNewRef(end_offset));
Py_DECREF(info);
if (self->end_lineno != NULL && self->end_offset == NULL) {