https://github.com/python/cpython/commit/c09ccd9c429bdfac85ee427d9d0def32af47492d
commit: c09ccd9c429bdfac85ee427d9d0def32af47492d
branch: 3.13
author: AN Long <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2026-03-30T21:36:52+05:30
summary:

[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 0de4e08a5990e4692feb1b1ea01c303e468a2894)

Co-authored-by: Brij Kapadia <[email protected]>

* Minimize the changes

* Minimize the changes

* Minimize the changes

---------

Co-authored-by: Brij Kapadia <[email protected]>

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-03-21-11-55-16.gh-issue-146250.ahl3O2.rst
M Lib/test/test_exceptions.py
M Objects/exceptions.c

diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index 939783956d62d6..04c7e973f5ae70 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -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):
diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2026-03-21-11-55-16.gh-issue-146250.ahl3O2.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-21-11-55-16.gh-issue-146250.ahl3O2.rst
new file mode 100644
index 00000000000000..fff07b31ec21c4
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-21-11-55-16.gh-issue-146250.ahl3O2.rst
@@ -0,0 +1 @@
+Fixed a memory leak in :exc:`SyntaxError` when re-initializing it.
diff --git a/Objects/exceptions.c b/Objects/exceptions.c
index db2774d3d6a3e8..6c1807715c0f14 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -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) {

_______________________________________________
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