py3k buffered IO - flush() required between read/write?

Terry Reedy tjreedy at udel.edu
Wed May 11 14:24:57 EDT 2011


On 5/11/2011 12:27 PM, Genstein wrote:

> In py3k is it necessary to flush() a file between read/write calls in order
> to see consistent results?
>
> I ask because I have a case under Python 3.2 (r32:88445) where it does
> appear to be, on both Gentoo Linux and Windows Vista.
>
> I've naturally read http://docs.python.org/py3k/library/io.html and
> http://docs.python.org/py3k/tutorial/inputoutput.html#reading-and-writing-files
> but could find no reference to such a requirement.
>
> PEP 3116 suggested this might not be required in py3k and the
> implementation notes in bufferedio.c state "BufferedReader,
> BufferedWriter and BufferedRandom...share a single buffer...this enables
> interleaved reads and writes without flushing." Which seemed conclusive
> but I'm seeing otherwise.
>
> I have a test case, which is sadly rather long:
> http://pastebin.com/xqrzKr5D It's lengthy because it's autogenerated
> from some rather more complex code I'm working on, in order to reproduce
> the issue in isolation.

I notice that you have required seek calls when switching between 
writing and reading. If you want others to look at this more, you should 
1) produce a minimal* example that demonstrates the questionable 
behavior, and 2) show the comparative outputs that raise your question. 
The code is way too long to cut and paste into an editor and see what is 
does on my windows machine.

*minimal = local minimum rather than global minimum. That means that 
removal or condensation of a line or lines removes the problem. In this 
case, remove extra seeks, unless doing so removes behavior discrepancy. 
Condense 1 byte writes to multibyte writes, unless ... . Are repeated 
interleavings required or is write, seek, read, seek, write enough?

-- 
Terry Jan Reedy




More information about the Python-list mailing list