Description
Bug report
Bug description:
As reported by the StackOverflow question:
https://stackoverflow.com/questions/76142400/python-file-write-stuck-in-append-mode-when-read-has-byte-count-as-parameter
In the code below, when f.read
is not given an argument, the pending content in the write buffer gets flushed so the output is ***s is a line
as expected.
with open("new.txt", 'w+') as f:
f.write("this is a line")
with open("new.txt", 'r+') as f:
f.write("***")
f.read() # << note here
f.seek(0)
print(f.read()) # outputs: ***s is a line
But when f.read
is given a size argument, it is apparent that the underlying write buffer is not flushed immediately, causing the pending content to be written at the end of the file, rendering an output of this is a line***
instead:
with open("new.txt", 'w+') as f:
f.write("this is a line")
with open("new.txt", 'r+') as f:
f.write("***")
f.read(1) # << note here
f.seek(0)
print(f.read()) # outputs: this is a line***
This issue occurs only with the C implementation of io.TextIOWrapper
since the code above would work as expected if it is prepended with:
from _pyio import open
CPython versions tested on:
3.8, 3.11
Operating systems tested on:
Linux, Windows
Linked PRs
- gh-115059: Flush the underlying write buffer in io.BufferedRandom.read1() #115163
- [3.11] gh-115059: Flush the underlying write buffer in io.BufferedRandom.read1() (GH-115163) #115206
- [3.12] gh-115059: Flush the underlying write buffer in io.BufferedRandom.read1() (GH-115163) #115205
- gh-115059: Remove debugging code in test_io #115240
- [3.12] gh-115059: Remove debugging code in test_io (GH-115240) #115244
- [3.11] gh-115059: Remove debugging code in test_io (GH-115240) #115245