[Python-Dev] Possible py3k io wierdness

Antoine Pitrou solipsis at pitrou.net
Sun Apr 5 01:23:06 CEST 2009


<brian <at> sweetapp.com> writes:
> class _RawIOBase(_FileIO):

FileIO is a subclass of _RawIOBase, not the reverse:

>>> issubclass(_io._RawIOBase, _io.FileIO)
>>> issubclass(_io.FileIO, _io._RawIOBase)

I do understand your surprise, but the Python implementation of IOBase.close()
in _pyio.py does the same thing:

    def close(self) -> None:
        """Flush and close the IO object.

        This method has no effect if the file is already closed.
        if not self.__closed:
            except IOError:
                pass  # If flush() fails, just give up
            self.__closed = True

Note how it calls `self.flush()` and not `IOBase.flush(self)`.
When writing the C version of the I/O stack, we tried to keep the semantics the
same as in the Python version, although there are a couple of subtleties.

Your problem here is that it's IOBase.close() which calls your flush() method,
but FileIO.close() has already done its job before and the internal file
descriptor has been closed (hence `self.closed` is True). In this particular
case, I advocate overriding close() as well and call your flush() method
manually from there.

Thanks for your feedback!



More information about the Python-Dev mailing list