Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed reference leak in the initalization of :mod:`tkinter`.
12 changes: 8 additions & 4 deletions Modules/_tkinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -3419,8 +3419,9 @@ PyInit__tkinter(void)
#endif

Tkinter_TclError = PyErr_NewException("_tkinter.TclError", NULL, NULL);
if (PyModule_AddObjectRef(m, "TclError", Tkinter_TclError)) {
if (PyModule_AddObject(m, "TclError", Tkinter_TclError)) {
Copy link
Member

Choose a reason for hiding this comment

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

Tkinter_TclError should be a strong reference to _tkinter.TclError. Even if you remove "TclError" from the the module dict, the use of Tkinter_TclError should not crash.

BTW, do not use PyModule_AddObject(), it is broken beyond repair. Use PyModule_AddObjectRef() or PyModule_Add(), what is more convenient.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, where should the strong reference to Tkinter_TclError be decref'd? Should we add a module clear function?

Py_DECREF(m);
Py_DECREF(Tkinter_TclError);
return NULL;
}

Expand Down Expand Up @@ -3470,20 +3471,23 @@ PyInit__tkinter(void)
}

Tkapp_Type = PyType_FromSpec(&Tkapp_Type_spec);
if (PyModule_AddObjectRef(m, "TkappType", Tkapp_Type)) {
if (PyModule_AddObject(m, "TkappType", Tkapp_Type)) {
Py_DECREF(m);
Py_DECREF(Tkapp_Type);
return NULL;
}

Tktt_Type = PyType_FromSpec(&Tktt_Type_spec);
if (PyModule_AddObjectRef(m, "TkttType", Tktt_Type)) {
if (PyModule_AddObject(m, "TkttType", Tktt_Type)) {
Py_DECREF(m);
Py_DECREF(Tktt_Type);
return NULL;
}

PyTclObject_Type = PyType_FromSpec(&PyTclObject_Type_spec);
if (PyModule_AddObjectRef(m, "Tcl_Obj", PyTclObject_Type)) {
if (PyModule_AddObject(m, "Tcl_Obj", PyTclObject_Type)) {
Py_DECREF(m);
Py_DECREF(PyTclObject_Type);
return NULL;
}

Expand Down