mixing for x in file: and file.readline

Oren Tirosh oren-py-l at hishome.net
Sun Sep 14 04:11:01 EDT 2003


On Fri, Sep 12, 2003 at 10:57:47AM -0700, Russell E. Owen wrote:
...
> >It's possible to hide the problem in most cases by making read and 
> >readline use the iteration readahead buffer if it's attached to the file
> >object and stdio if it isn't. I don't think it's a good idea. It will
> >require some hairy code and and seems susceptible to subtle bugs and
> >corner cases.
> 
> I agree that fixing read would probably be too messy to justify.
> 
> But it seems to me that a simple reimplementation of readline() would 
> work fine:
> 
> def readline(self):
>    try:
>       return self.next()
>    except StopIteration
>       return ""
> 
> That's basically the way I ended up working around the problem (but I 
> didn't try to modify any classes). I do see two issues with that fix:
> - existing code (if any) that mixes readlines and read would be harmed
> - it may not be efficient enough (even implemented in C)

It will be very efficient. In fact, it will be faster than the current
readline implementation because it will use the readahead buffer. But
the problem is more than just mixing readline() and read(). Mixing 
readline() and tell() will also be broken. It is valid (and useful) to 
read a file line by line, store a tell() offset and later seek() back to 
the same line. It works even if the file is in text mode doing CRLF->LF 
conversions. 

> >Another alternative it to make read and readline fail noisily after 
> >iteration starts (unless cleared by seek())
> 
> If readlines cannot be fixed, this might be worth doing since I think 
> it's a common thing to want to mix readlines and iteration. If read is 
> the only issue, I suspect adding a warning to the documentation for file 
> method "read" would suffice.

The problem is that it will work on, say, Python 2.3.1 but fail silently
on earlier versions. Why not just use next() instead of readline()? 
Because catching StopIteration takes a little more typing than checking
an empty string? 

> I'm wondering where the problem is discussed in the manual. I'm pretty 
> sure I saw it recently, but when I read about file methods I saw nothing 
> about it.

I believe it's not documented clearly enough. Docpatch time?

    Oren





More information about the Python-list mailing list