[Python-Dev] py3k buffered I/O - flush() required between read/write?

Genstein pythondev at genstein.net
Thu May 12 04:35:16 CEST 2011

Hi all,

Sincere apologies for posting a question without lurking for a while 
first. I'm not sure whether I'm being dumb (which is very plausible) or 
whether this is a potential bug. I asked on comp.lang.python but 
responses were equivocal, so I'm following the README.txt advice and 
asking here. If I'm out of line, do feel free to slap me down viciously, 
remove me from the list, or whatever seems most appropriate.

Under py3k, is it necessary to flush() a file between buffered 
read/write calls in order to see consistent results? I have a case under 
Python 3.2 (r32:88445) where I see different results depending on 
whether buffering is active, on Gentoo Linux and Windows Vista. Perusing 
the docs and PEPs I couldn't seem to find an answer; I did find 
bufferedio.c's comment: "BufferedReader, BufferedWriter and 
BufferedRandom...share a single buffer...this enables interleaved reads 
and writes without flushing" which is suggestive but I may be taking it 
out of context.

The following is the smallest code I can conjure which demonstrates the 
issue I'm seeing:

MID = 1

def test(buffering):
     f = open("test.bin", "w+b", buffering = buffering)
     for i in range(LENGTH):

print("Buffered result: ")
print("Unbuffered result:")
[end code]

Output on both Gentoo and Vista is:
     Buffered result:
     Unbuffered result:

I expected the results to be the same, but they aren't. The issue is 
reproducible with larger files provided that the constants are increased 
~proportionally (START 0, MID 500, LENGTH 1000 for example). Transposing 
the buffered/unbuffered tests and/or using different buffer sizes for 
the buffered test seem have no effect.

Apologies once more if I'm wasting your time.

All the best,


PS. By way of entirely belated introduction, I'm a UK software developer 
with a background mostly in C#, C++ and Lua in both "real software" and 
commercial games. In my spare time I mostly write code (curiously I 
don't know many developers who do; I suspect I just know the wrong 
people.) I perpetrated the Trizbort mapper for interactive fiction which 
doubtless nobody will have heard of, and with good reason. I'm toying 
with Python as a genuinely portable alternative to C# for my own 
projects, and so far loving it.

More information about the Python-Dev mailing list