https://github.com/python/cpython/commit/0de4e08a5990e4692feb1b1ea01c303e468a2894
commit: 0de4e08a5990e4692feb1b1ea01c303e468a2894
branch: main
author: Brij Kapadia <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2026-03-27T19:29:21+05:30
summary:
gh-146250: Fix memory leak in re-initialization of `SyntaxError` (#146251)
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 7354f8281d9682..3f5fcb29b442de 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -2561,6 +2561,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 32b0fcec6c4542..c4a591e2cf7f8c 100644
--- a/Objects/exceptions.c
+++ b/Objects/exceptions.c
@@ -2802,23 +2802,25 @@ SyntaxError_init(PyObject *op, 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;
+ PyObject *metadata = NULL;
if (!PyArg_ParseTuple(info, "OOOO|OOO",
- &self->filename, &self->lineno,
- &self->offset, &self->text,
- &self->end_lineno, &self->end_offset,
&self->metadata)) {
+ &filename, &lineno,
+ &offset, &text,
+ &end_lineno, &end_offset, &metadata)) {
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_XINCREF(self->metadata);
+ 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_XSETREF(self->metadata, Py_XNewRef(metadata));
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]