You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
BufferedRandom: issues with interlaced read-write #56422
changed the title [-]BufferedRandom: write(); read() gives different result using io and _pyio[/-][+]BufferedRandom, BufferedRWPair: issues with interlaced read-write[/+]on May 30, 2011
add interlaced read/write tests for BufferedRandom and BufferedRWPair
_pyio: move "undo readahead" code into BufferedReader.flush()
io: BufferedRandom.flush() doesn't undo readahead if the write buffer is empty, so it's possible to call it in read methods without flusing the readahead buffer when it's not needed
tests BufferedRWPair with read-only + write-only files because I'm not sure if my tests using readable and writeable methods
_pyio: undo the readahead in BufferedReader.flush() is no perfect, because BufferedReader is supposed to be read-only. I choosed that to factorize the code between BufferedRandom and BufferedRWPair
what happens if a write occurs during _pyio.BufferedReader.flush()? "if self._read_buf: <write occurs> with self._read_lock: ...". We may protect the read of self._read_buf with the read lock.
My patch tries to fix interlaced read-write by always calling flush(), but I am not sure that it doesn't change read-only and write-only cases. There are maybe some unnecessary call to flush().
> My patch tries to fix interlaced read-write by always calling flush(),
Why do you need to call flush()? Can't you read from the buffer?
Hum, my patch does not always call flush of the reader and the writer. On read, it flushs the writer. On write, it "flushes" the reader (undo readahead).
It is maybe possible to do better (do something faster), but there should be some tricky cases with seek().
I think your expectations for BufferedRWPair are wrong. You should not use BufferedRWPair with the same underlying stream (that's the whole point of BufferedRWPair).
changed the title [-]BufferedRandom, BufferedRWPair: issues with interlaced read-write[/-][+]BufferedRandom: issues with interlaced read-write[/+]on Aug 20, 2011
You should not use BufferedRWPair with the same underlying stream (that's the whole point of BufferedRWPair).
It might be documented. Something like "Warning: don't use the same
stream as reader and writer, or the BufferedRWPair becomes inconsistent
on interlaced read-write" ?
changed the title [-]BufferedRandom: issues with interlaced read-write[/-][+]BufferedRandom, BufferedRWPair: issues with interlaced read-write[/+]on Aug 20, 2011
changed the title [-]BufferedRandom, BufferedRWPair: issues with interlaced read-write[/-][+]BufferedRandom, BufferedRWPair: issues with interlaced read-write[/+]on Aug 20, 2011
changed the title [-]BufferedRandom, BufferedRWPair: issues with interlaced read-write[/-][+]BufferedRandom: issues with interlaced read-write[/+]on Aug 20, 2011
Activity
vstinner commentedon May 30, 2011
The following code displays "Xbc" using io, and "bc" using _pyio (or an unbuffered file, e.g. io.FileIO):
-------------
I expect .write() to change the file position, and so "bc" must be the correct result, not "Wbc".
_pyio.BufferedRandom overrides its write method, whereas io.BufferedRandom doesn't.
I already noticed the implement difference of BufferedRandom.write(), but I don't remember if I reported it or not!?
vstinner commentedon May 30, 2011
Yes, I already reported the difference:
http://bugs.python.org/issue12062#msg135876
[-]BufferedRandom: write(); read() gives different result using io and _pyio[/-][+]BufferedRandom, BufferedRWPair: issues with interlaced read-write[/+]vstinner commentedon May 30, 2011
io_interlaced_read_write.patch:
TODO:
My patch tries to fix interlaced read-write by always calling flush(), but I am not sure that it doesn't change read-only and write-only cases. There are maybe some unnecessary call to flush().
vstinner commentedon May 30, 2011
See also issue bpo-12215: TextIOWrapper has also issues on interlaced read-write.
pitrou commentedon Jun 5, 2011
Why do you need to call flush()? Can't you read from the buffer?
vstinner commentedon Jun 20, 2011
Hum, my patch does not always call flush of the reader and the writer. On read, it flushs the writer. On write, it "flushes" the reader (undo readahead).
It is maybe possible to do better (do something faster), but there should be some tricky cases with seek().
pitrou commentedon Aug 19, 2011
I think your expectations for BufferedRWPair are wrong. You should not use BufferedRWPair with the same underlying stream (that's the whole point of BufferedRWPair).
pitrou commentedon Aug 19, 2011
Here is a patch.
python-dev commentedon Aug 20, 2011
New changeset 5330af45f777 by Antoine Pitrou in branch '3.2':
Issue bpo-12213: Fix a buffering bug with interleaved reads and writes that
http://hg.python.org/cpython/rev/5330af45f777
New changeset d7f6391954cf by Antoine Pitrou in branch 'default':
Issue bpo-12213: Fix a buffering bug with interleaved reads and writes that
http://hg.python.org/cpython/rev/d7f6391954cf
pitrou commentedon Aug 20, 2011
This should be fixed for BufferedRandom. As I said, I don't think BufferedRWPair is buggy.
[-]BufferedRandom, BufferedRWPair: issues with interlaced read-write[/-][+]BufferedRandom: issues with interlaced read-write[/+]vstinner commentedon Aug 20, 2011
It might be documented. Something like "Warning: don't use the same
stream as reader and writer, or the BufferedRWPair becomes inconsistent
on interlaced read-write" ?
[-]BufferedRandom: issues with interlaced read-write[/-][+]BufferedRandom, BufferedRWPair: issues with interlaced read-write[/+]python-dev commentedon Aug 20, 2011
New changeset cf2010e9f941 by Antoine Pitrou in branch '2.7':
Issue bpo-12213: Fix a buffering bug with interleaved reads and writes that
http://hg.python.org/cpython/rev/cf2010e9f941
[-]BufferedRandom, BufferedRWPair: issues with interlaced read-write[/-][+]BufferedRandom, BufferedRWPair: issues with interlaced read-write[/+][-]BufferedRandom, BufferedRWPair: issues with interlaced read-write[/-][+]BufferedRandom: issues with interlaced read-write[/+]python-dev commentedon Aug 20, 2011
New changeset 524896c305ce by Antoine Pitrou in branch '3.2':
Issue bpo-12213: make it clear that BufferedRWPair shouldn't be called with the
http://hg.python.org/cpython/rev/524896c305ce
New changeset a423bd492d6c by Antoine Pitrou in branch 'default':
Issue bpo-12213: make it clear that BufferedRWPair shouldn't be called with the
http://hg.python.org/cpython/rev/a423bd492d6c
New changeset dd4f29e39756 by Antoine Pitrou in branch '2.7':
Issue bpo-12213: make it clear that BufferedRWPair shouldn't be called with the
http://hg.python.org/cpython/rev/dd4f29e39756
pitrou commentedon Aug 20, 2011
I think it can be closed now.
python-dev commentedon Sep 4, 2011
New changeset 8de8945ebfc3 by Antoine Pitrou in branch '3.2':
Issue bpo-12213: make it clear that BufferedRWPair shouldn't be called with the
http://hg.python.org/cpython/rev/8de8945ebfc3