Python 2.1 / 2.3: xreadlines not working with codecs.open

Peter Otten __peter__ at web.de
Tue Jun 28 09:41:27 EDT 2005


Eric Brunel wrote:

> I just found a problem in the xreadlines method/module when used with
> codecs.open: the codec specified in the open does not seem to be taken
> into account by xreadlines which also returns byte-strings instead of
> unicode strings.

> So f.xreadlines does not work, but xreadlines.xreadlines(f) does. And this
> happens in Python 2.3, but also in Python 2.1, where the implementation
> for f.xreadlines() calls xreadlines.xreadlines(f) (?!?). Something's
> escaping me here... Reading the source didn't help.

codecs.StreamReaderWriter seems to delegate everything it doesn't implement
itself to the underlying file instance which is ignorant of the encoding.
The culprit:

   def __getattr__(self, name,
                    getattr=getattr):

        """ Inherit all other methods from the underlying stream.
        """
        return getattr(self.stream, name)

> At least, it does provide a workaround...

Note that the xreadlines module hasn't made it into Python 2.4. 

Peter




More information about the Python-list mailing list