Author vstinner
Recipients pitrou, vstinner
Date 2011-05-30.12:38:03
SpamBayes Score 0.0126049
Marked as misclassified No
Message-id <1306759084.05.0.928785750761.issue12215@psf.upfronthosting.co.za>
In-reply-to
Content
The following code fails on an assertion error (Python exception for _pyio, C assertion for io):

------------------
with io.BytesIO(b'abcd') as raw:
    with _pyio.TextIOWrapper(raw, encoding='ascii') as f:
        f.read(1)
        f.write('2')
        f.tell()
------------------

I found this assertion while testing interlaced read-write on TextIOWrapper:
------------------
with io.BytesIO(b'abcd') as raw:
    with _pyio.TextIOWrapper(raw, encoding='ascii') as f:
        f.write("1")
        # read() must call writer.flush()
        assertEqual(f.read(1), 'b')
        # write() must rewind the raw stream
        f.write('2')
        assertEqual(f.read(), 'd')
        f.flush()
        assertEqual(raw.getvalue(), b'1b2d')

with io.BytesIO(b'abc') as raw:
    with _pyio.TextIOWrapper(raw, encoding='ascii') as f:
        self.assertEqual(f.read(1), b'a')
        # write() must undo reader readahead
        f.write(b"2")
        assertEqual(f.read(1), b'c')
        f.flush()
        assertEqual(raw.getvalue(), b'a2c')
------------------
These tests fails on "read, write, read" path: write() breaks TextIOWrapper internal state if a read() occured just before. Note that _pyio.TextIOWrapper.write() contains the following comment...

# XXX What if we were just reading?

See also the issue #12213 for BufferedRandom and BufferedRWPair.
History
Date User Action Args
2011-05-30 12:38:04vstinnersetrecipients: + vstinner, pitrou
2011-05-30 12:38:04vstinnersetmessageid: <1306759084.05.0.928785750761.issue12215@psf.upfronthosting.co.za>
2011-05-30 12:38:03vstinnerlinkissue12215 messages
2011-05-30 12:38:03vstinnercreate