[issue12062] Buffered I/O inconsistent with unbuffered I/O in certain cases

Genstein report at bugs.python.org
Thu May 12 15:07:28 CEST 2011


New submission from Genstein <pythondev at genstein.net>:

Reporting this as requested by Antoine Pitrou: Under certain circumstances in Python 3.2 (r32:88445) it's possible for buffered I/O to lose data before it is written and/or return the wrong results when reading. I tripped over this issue whilst writing an assembler which first writes out a bunch of binary data and then goes back over it in a somewhat arbitrary order patching addresses.

The following code demonstrates the issue:

[code]
START = 0
MID = 1
LENGTH = 4

def test(buffering):
    f = open("test.bin", "w+b", buffering = buffering)
    for i in range(LENGTH):
        f.write(b'\x00')
    f.seek(MID)
    f.read(1)
    f.write(b'\x00')
    f.seek(MID)
    f.write(b'\x01')
    f.seek(START)
    f.seek(MID)
    print(f.read(1))
    f.close()

print("Buffered result: ")
test(-1)
print("Unbuffered result:")
test(0)
[end code]

Output on both Gentoo and Vista is:
    Buffered result:
    b'\x00'
    Unbuffered result:
    b'\x01'

Expected output is b'\x01' in both cases.

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. 

The lengthy code at this URL also demonstrates the issue: http://pastebin.com/xqrzKr5D The above was produced from this code, which was autogenerated by intercepting my assembler's output.

----------
components: IO
messages: 135830
nosy: genstein
priority: normal
severity: normal
status: open
title: Buffered I/O inconsistent with unbuffered I/O in certain cases
type: behavior
versions: Python 3.2

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue12062>
_______________________________________


More information about the Python-bugs-list mailing list