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

Guido van Rossum guido at python.org
Sun Feb 22 20:08:15 CET 2009


On Sun, Feb 22, 2009 at 11:02 AM, Antoine Pitrou <solipsis at pitrou.net> wrote:
> 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.

OTOH the is a much larger category of false positives, where a close()
call raise an exception for some spurious reason (see the quoted
comment) but no harm is done; in the past the tracebacks printed for
__del__ by default have caused nothing but confuse users (who think
something is seriously wrong but have no way to debug it).

The svn history of those lines may have more pointers.

> 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.

I'd be very wary of this change. That code has enough comments to make
me believe that I put it in for a very good reason...

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list