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
io.TextIOWrapper.read does not flush the underlying write buffer when given a size argument #115059
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:
It is because the C implementation of io.TextIOWrapper.read() uses the read1() method of the underlying binary stream if it is available and the size is specified, and the C implementation of io.BufferedRandom.read1() did not flush the write buffer. #115163 fixes the latter.
Activity
pythongh-115059: Flush the underlying write buffer in io.BufferedRand…
serhiy-storchaka commentedon Feb 8, 2024
It is because the C implementation of
io.TextIOWrapper.read()uses theread1()method of the underlying binary stream if it is available and the size is specified, and the C implementation ofio.BufferedRandom.read1()did not flush the write buffer. #115163 fixes the latter.gh-115059: Flush the underlying write buffer in io.BufferedRandom.rea…
pythongh-115059: Flush the underlying write buffer in io.BufferedRand…
pythongh-115059: Flush the underlying write buffer in io.BufferedRand…
serhiy-storchaka commentedon Feb 9, 2024
Thank you for your report @blhsing. The issue is now fixed.
[3.12] gh-115059: Flush the underlying write buffer in io.BufferedRan…
[3.11] gh-115059: Flush the underlying write buffer in io.BufferedRan…
pythongh-115059: Remove debugging code in test_io
gh-115059: Remove debugging code in test_io (GH-115240)
pythongh-115059: Remove debugging code in test_io (pythonGH-115240)
pythongh-115059: Remove debugging code in test_io (pythonGH-115240)
[3.11] gh-115059: Remove debugging code in test_io (GH-115240) (GH-11…
[3.12] gh-115059: Remove debugging code in test_io (GH-115240) (GH-11…
pythongh-115059: Flush the underlying write buffer in io.BufferedRand…
pythongh-115059: Remove debugging code in test_io (pythonGH-115240)