[issue18141] tkinter.Image.__del__ can throw an exception if module globals are destroyed in the wrong order

Terry J. Reedy report at bugs.python.org
Wed May 28 20:44:05 CEST 2014


Terry J. Reedy added the comment:

My worry about the patch is that is is a bandage covering up a problem elsewhere.

1. You indicate that you only have a problem with your custom build, but not on a standard build. This seems odd. Perhaps the problem is with your build.

2. turtledemo.__main__ only calls root.destroy indirectly via
        root.wm_protocol("WM_DELETE_WINDOW", self._destroy)
    def _destroy(self):
        self.root.destroy()
        sys.exit()
Let's assume that .destroy is called when the exception happens (verifiable by adding "print('destroy called')".)

3. The purpose of Tk.destroy is to cleanly deconstruct the gui before python's random cleanup. Should it have cleared PhotoImage?

Image and Variable (and subclasses) are the only classes in tkinter.__init__ with __del__ methods. Both have a tcl.call, so Variable potentially has the same problem as Image. But neither seem to have a problem in routine tkinter use.

4. Why is there a PhotoImage to be deleted? Idle Find in Files does not find 'PhotoImage' in turtledemo/*.py. I believe it is created as follows. turtledemo.DemoWindonw.__init__ contains this odd pair of lines that initializes a turtle.Screen instance with turtle.TurtleScreen.__init__ (TurtleScreen subclasses TurtleScreenBase, not Screen).
        self.screen = _s_ = turtle.Screen()
        turtle.TurtleScreen.__init__(_s_, _s_._canvas)
The latter call creates a collection of default turtles (_shapes) that includes 'blank', a 1x1 (pixel) blank PhotoImage that gets tkinter._default_root as (default) master.

I suspect that adding either of these lines to _destroy would prevent the TclError.
    del self.screen  # before self.root.destroy()
    import tkinter; tkinter._default_root.destroy()  # after self.root

5. While it seems like a buglet for turtle to create a hidden PhotoImage, with an anonymous master, and not clean it up somehow; and while I am puzzled if this never happens with standard builds; it seems plausible that we could patch Variable/Image.__delete__ to be sure they do not leak an exception.

Serhiy, what do you think?

----------
nosy:  -gpolo

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue18141>
_______________________________________


More information about the Python-bugs-list mailing list