Skip to content

Commit 94adfe6

Browse files
bpo-44608: Fix memory leak in _tkinter._flatten() (GH-27107)
if it is called with a sequence or set, but not list or tuple. (cherry picked from commit f572cbf) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent fe73509 commit 94adfe6

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

Lib/test/test_tcl.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,14 @@ def get_tk_patchlevel():
4141
class TkinterTest(unittest.TestCase):
4242

4343
def testFlattenLen(self):
44-
# flatten(<object with no length>)
44+
# Object without length.
4545
self.assertRaises(TypeError, _tkinter._flatten, True)
46+
# Object with length, but not sequence.
47+
self.assertRaises(TypeError, _tkinter._flatten, {})
48+
# Sequence or set, but not tuple or list.
49+
# (issue44608: there were leaks in the following cases)
50+
self.assertRaises(TypeError, _tkinter._flatten, 'string')
51+
self.assertRaises(TypeError, _tkinter._flatten, {'set'})
4652

4753

4854
class TclTest(unittest.TestCase):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix memory leak in :func:`_tkinter._flatten` if it is called with a sequence
2+
or set, but not list or tuple.

Modules/_tkinter.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3197,8 +3197,10 @@ _tkinter__flatten(PyObject *module, PyObject *item)
31973197

31983198
context.size = 0;
31993199

3200-
if (!_flatten1(&context, item,0))
3200+
if (!_flatten1(&context, item, 0)) {
3201+
Py_XDECREF(context.tuple);
32013202
return NULL;
3203+
}
32023204

32033205
if (_PyTuple_Resize(&context.tuple, context.size))
32043206
return NULL;

0 commit comments

Comments
 (0)