[Python-Dev] Silencing IO errors on del/dealloc?

Antoine Pitrou solipsis at pitrou.net
Sun Feb 22 20:02:01 CET 2009


Hello,

The Python version of IO lib has taken the stance that all errors happening in
the destructor of an IO object are silenced. Here is the relevant code in IOBase:

    def __del__(self) -> None:
        """Destructor.  Calls close()."""
        # The try/except block is in case this is called at program
        # exit time, when it's possible that globals have already been
        # deleted, and then the close() call might fail.  Since
        # there's nothing we can do about such failures and they annoy
        # the end users, we suppress the traceback.
        try:
            self.close()
        except:
            pass

However, this seems very unreliable and dangerous to me. Users will not be
warned if their IO objects are not closed properly (which might be due to
programming errors), and data possibly gets corrupted.

I would like to change this behaviour so that the "try/except" enclosure above
is removed, letting __del__ at least print those (unraisable) errors on the
console. Of course I also advocate doing so in the C version of the IO lib.

Regards

Antoine.




More information about the Python-Dev mailing list