classification
Title: codec.StreamReaderWriter: issues with interlaced read-write
Type: behavior Stage:
Components: Library (Lib), Unicode Versions: Python 3.5, Python 3.4, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: BreamoreBoy, doerwalter, haypo, lemburg
Priority: normal Keywords:

Created on 2011-07-07 13:12 by haypo, last changed 2014-07-03 22:00 by BreamoreBoy.

Messages (2)
msg139975 - (view) Author: STINNER Victor (haypo) * (Python committer) Date: 2011-07-07 13:12
The following test fails with an AssertionError('a' != 'b') on the first read.

import codecs

FILENAME = 'test'

with open(FILENAME, 'wb') as f:
    f.write('abcd'.encode('utf-8'))
with codecs.open(FILENAME, 'r+', encoding='utf-8') as f:
    f.write("1")

    pos = f.writer.tell()
    assert pos == 1, "writer pos: %s != 1" % pos
    pos = f.reader.tell()
    assert pos == 1, "reader pos: %s != 1" % pos
    pos = f.stream.tell()
    assert pos == 1, "stream pos: %s != 1" % pos

    # read() must call writer.flush()
    char = f.read(1)
    assert char == 'b', "%r != 'b'" % (char,)
    # write() must rewind the raw stream
    f.write('3')
    tail = f.read()
    assert tail == 'd', "%r != 'd'" % (tail,)
    f.flush()
    with open(FILENAME, 'rb') as f:
        assert f.read() == b'1b3d'

I suppose that readline(), readlines() and __next__() have also issues.

See also issue #12215: similar issue with io.TextIOWrapper.
msg222225 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-07-03 22:00
Codecs gurus FYI.
History
Date User Action Args
2014-07-03 22:00:45BreamoreBoysetversions: + Python 3.4, Python 3.5, - Python 3.2, Python 3.3
nosy: + BreamoreBoy, lemburg, doerwalter

messages: + msg222225

type: behavior
2011-07-07 13:12:46haypocreate