mirror of
https://github.com/python/cpython.git
synced 2026-04-14 07:41:00 +00:00
[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:
parent
0dcb625be4
commit
c09ccd9c42
3 changed files with 37 additions and 11 deletions
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
Fixed a memory leak in :exc:`SyntaxError` when re-initializing it.
|
||||
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue