Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Lib/test/test_tkinter/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,23 @@ def test_embedded_null(self):
widget.selection_range(0, 'end')
self.assertEqual(widget.selection_get(), '\u20ac\0abc\x00def')

def test_settrace_gc(self):
# Regression test for https://github.com/python/cpython/issues/138791.
root = tkinter.Tk()
trace = lambda *_, **__: None
trace.evil = type(root.tk)
root.tk.settrace(trace)
root.destroy()

def test_createtimerhandler_gc(self):
# Regression test for https://github.com/python/cpython/issues/138791.
root = tkinter.Tk()
func = lambda *_, **__: None
func.evil = type(root.tk.createtimerhandler(0, print))
# Large timeout (in ms) so that the object is destroyed before.
root.tk.createtimerhandler(1234567, func)
root.destroy()


class WmTest(AbstractTkTest, unittest.TestCase):

Expand Down
12 changes: 11 additions & 1 deletion Modules/_tkinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -2764,7 +2764,14 @@ static int
Tktt_Clear(PyObject *op)
{
TkttObject *self = TkttObject_CAST(op);
Py_CLEAR(self->func);
if (self->token != NULL) {
Tcl_DeleteTimerHandler(self->token);
self->token = NULL;
}
if (self->func != NULL) {
Py_CLEAR(self->func);
Py_DECREF(op); /* See Tktt_New() */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I'm probably missing something -- why do we have that extra reference in the first place?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To prevent the result of createtimerhandler() from being collected before the timer fires.

}
return 0;
}

Expand All @@ -2783,6 +2790,9 @@ Tktt_Traverse(PyObject *op, visitproc visit, void *arg)
{
TkttObject *self = TkttObject_CAST(op);
Py_VISIT(Py_TYPE(op));
if (self->token != NULL) {
Py_VISIT(op); /* See Tktt_New() */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be missing something, but won't this just lead to infinite recursion?

}
Py_VISIT(self->func);
return 0;
}
Expand Down
Loading