Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7c40489

Browse files
committed
Issue #12062: Fix a flushing bug when doing a certain type of I/O sequence
on a file opened in read+write mode (namely: reading, seeking a bit forward, writing, then seeking before the previous write but still within buffered data, and writing again).
1 parent 0a42982 commit 7c40489

3 files changed

Lines changed: 32 additions & 1 deletion

File tree

Lib/test/test_io.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,32 @@ def test_write_after_readahead(self):
14611461
self.assertEqual(s,
14621462
b"A" + b"B" * overwrite_size + b"A" * (9 - overwrite_size))
14631463

1464+
def test_write_rewind_write(self):
1465+
# Various combinations of reading / writing / seeking backwards / writing again
1466+
def mutate(bufio, pos1, pos2):
1467+
assert pos2 >= pos1
1468+
# Fill the buffer
1469+
bufio.seek(pos1)
1470+
bufio.read(pos2 - pos1)
1471+
bufio.write(b'\x02')
1472+
# This writes earlier than the previous write, but still inside
1473+
# the buffer.
1474+
bufio.seek(pos1)
1475+
bufio.write(b'\x01')
1476+
1477+
b = b"\x80\x81\x82\x83\x84"
1478+
for i in range(0, len(b)):
1479+
for j in range(i, len(b)):
1480+
raw = self.BytesIO(b)
1481+
bufio = self.tp(raw, 100)
1482+
mutate(bufio, i, j)
1483+
bufio.flush()
1484+
expected = bytearray(b)
1485+
expected[j] = 2
1486+
expected[i] = 1
1487+
self.assertEqual(raw.getvalue(), expected,
1488+
"failed result for i=%d, j=%d" % (i, j))
1489+
14641490
def test_truncate_after_read_or_write(self):
14651491
raw = self.BytesIO(b"A" * 10)
14661492
bufio = self.tp(raw, 100)

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ Core and Builtins
6969
Library
7070
-------
7171

72+
- Issue #12062: Fix a flushing bug when doing a certain type of I/O sequence
73+
on a file opened in read+write mode (namely: reading, seeking a bit forward,
74+
writing, then seeking before the previous write but still within buffered
75+
data, and writing again).
76+
7277
- Issue #1028: Tk returns invalid Unicode null in %A: UnicodeDecodeError.
7378
With Tk < 8.5 _tkinter.c:PythonCmd() raised UnicodeDecodeError, caused
7479
IDLE to exit. Converted to valid Unicode null in PythonCmd().

Modules/_io/bufferedio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1749,7 +1749,7 @@ bufferedwriter_write(buffered *self, PyObject *args)
17491749
avail = Py_SAFE_DOWNCAST(self->buffer_size - self->pos, Py_off_t, Py_ssize_t);
17501750
if (buf.len <= avail) {
17511751
memcpy(self->buffer + self->pos, buf.buf, buf.len);
1752-
if (!VALID_WRITE_BUFFER(self)) {
1752+
if (!VALID_WRITE_BUFFER(self) || self->write_pos > self->pos) {
17531753
self->write_pos = self->pos;
17541754
}
17551755
ADJUST_POSITION(self, self->pos + buf.len);

0 commit comments

Comments
 (0)