Skip to content

Commit 601d63f

Browse files
committed
Updated variable memory management and moved test
1 parent f66f5ae commit 601d63f

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

Lib/test/test_exceptions.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,11 +1954,6 @@ def test_exec_set_nomemory_hang(self):
19541954
self.assertGreater(len(output), 0) # At minimum, should not hang
19551955
self.assertIn(b"MemoryError", output)
19561956

1957-
# gh-146250: memory leak with re-initialization of SyntaxError
1958-
def test_syntax_error_memory_leak(self):
1959-
e = SyntaxError("msg", ("file.py", 1, 2, "txt", 2, 3))
1960-
e.__init__("new_msg", ("new_file.py", 2, 3, "new_txt", 3, 4))
1961-
19621957

19631958
class NameErrorTests(unittest.TestCase):
19641959
def test_name_error_has_name(self):
@@ -2566,6 +2561,11 @@ def test_incorrect_constructor(self):
25662561
args = ("bad.py", 1, 2, "abcdefg", 1)
25672562
self.assertRaises(TypeError, SyntaxError, "bad bad", args)
25682563

2564+
def test_syntax_error_memory_leak(self):
2565+
# gh-146250: memory leak with re-initialization of SyntaxError
2566+
e = SyntaxError("msg", ("file.py", 1, 2, "txt", 2, 3))
2567+
e.__init__("new_msg", ("new_file.py", 2, 3, "new_txt", 3, 4))
2568+
25692569

25702570
class TestInvalidExceptionMatcher(unittest.TestCase):
25712571
def test_except_star_invalid_exception_type(self):

Objects/exceptions.c

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2802,29 +2802,25 @@ SyntaxError_init(PyObject *op, PyObject *args, PyObject *kwds)
28022802
return -1;
28032803
}
28042804

2805-
Py_XDECREF(self->filename);
2806-
Py_XDECREF(self->lineno);
2807-
Py_XDECREF(self->offset);
2808-
Py_XDECREF(self->text);
2809-
Py_XSETREF(self->end_lineno, NULL);
2810-
Py_XSETREF(self->end_offset, NULL);
2811-
Py_XDECREF(self->metadata);
2812-
2805+
PyObject *filename, *lineno, *offset, *text;
2806+
PyObject *end_lineno = NULL;
2807+
PyObject *end_offset = NULL;
2808+
PyObject *metadata = NULL;
28132809
if (!PyArg_ParseTuple(info, "OOOO|OOO",
2814-
&self->filename, &self->lineno,
2815-
&self->offset, &self->text,
2816-
&self->end_lineno, &self->end_offset, &self->metadata)) {
2810+
&filename, &lineno,
2811+
&offset, &text,
2812+
&end_lineno, &end_offset, &metadata)) {
28172813
Py_DECREF(info);
28182814
return -1;
28192815
}
28202816

2821-
Py_INCREF(self->filename);
2822-
Py_INCREF(self->lineno);
2823-
Py_INCREF(self->offset);
2824-
Py_INCREF(self->text);
2825-
Py_XINCREF(self->end_lineno);
2826-
Py_XINCREF(self->end_offset);
2827-
Py_XINCREF(self->metadata);
2817+
Py_XSETREF(self->filename, Py_NewRef(filename));
2818+
Py_XSETREF(self->lineno, Py_NewRef(lineno));
2819+
Py_XSETREF(self->offset, Py_NewRef(offset));
2820+
Py_XSETREF(self->text, Py_XNewRef(text));
2821+
Py_XSETREF(self->end_lineno, Py_XNewRef(end_lineno));
2822+
Py_XSETREF(self->end_offset, Py_XNewRef(end_offset));
2823+
Py_XSETREF(self->metadata, Py_XNewRef(metadata));
28282824
Py_DECREF(info);
28292825

28302826
if (self->end_lineno != NULL && self->end_offset == NULL) {

0 commit comments

Comments
 (0)