FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.15] gh-80937: Fix memory leak in tkinter createcommand (GH-152294) by miss-islington · Pull Request #152327 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
12 changes: 12 additions & 0 deletions Lib/test/test_tkinter/test_misc.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import collections.abc
import functools
import unittest
import weakref
import tkinter
from tkinter import TclError
import enum
Expand Down Expand Up @@ -348,6 +349,17 @@ def callback():
self.root.deletecommand(name)
self.assertRaises(TclError, self.root.tk.call, name)

def test_createcommand_no_leak(self):
# gh-80937: dropping the interpreter must release a command's callback,
# even without an explicit deletecommand().
interp = tkinter.Tcl()
callback = lambda: ''
ref = weakref.ref(callback)
interp.tk.createcommand('cb', callback)
del callback, interp
support.gc_collect()
self.assertIsNone(ref())

def test_option(self):
self.addCleanup(self.root.option_clear)
self.root.option_add('*Button.background', 'red')
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix a memory leak in :mod:`tkinter` when a Tcl command created with
``createcommand`` was not explicitly removed before the interpreter was
deleted. The command no longer keeps the interpreter alive through a
reference cycle.
7 changes: 5 additions & 2 deletions Modules/_tkinter.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2464,7 +2464,7 @@ PythonCmdDelete(ClientData clientData)
PythonCmd_ClientData *data = (PythonCmd_ClientData *)clientData;

ENTER_PYTHON
Py_XDECREF(data->self);
/* data->self is borrowed. */
Py_XDECREF(data->func);
PyMem_Free(data);
LEAVE_PYTHON
Expand Down Expand Up @@ -2533,7 +2533,9 @@ _tkinter_tkapp_createcommand_impl(TkappObject *self, const char *name,
data = PyMem_NEW(PythonCmd_ClientData, 1);
if (!data)
return PyErr_NoMemory();
Py_INCREF(self);
/* Borrow the interpreter: a strong reference would form an uncollectable
cycle (interp -> command -> data->self -> interp) and leak the command
(gh-80937). The command cannot outlive the interpreter. */
data->self = self;
data->func = Py_NewRef(func);
if (self->threaded && self->thread_id != Tcl_GetCurrentThread()) {
Expand Down Expand Up @@ -2566,6 +2568,7 @@ _tkinter_tkapp_createcommand_impl(TkappObject *self, const char *name,
}
if (err) {
PyErr_SetString(Tkinter_TclError, "can't create Tcl command");
Py_DECREF(data->func);
PyMem_Free(data);
return NULL;
}
Expand Down
Loading

Back | FazBrowse Home | New Git URL