Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

io.TextIOWrapper.read does not flush the underlying write buffer when given a size argument #115059

Closed
blhsing opened this issue Feb 6, 2024 · 2 comments
Assignees
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes topic-IO type-bug An unexpected behavior, bug, or error

Comments

@blhsing
Copy link
Contributor

blhsing commented Feb 6, 2024

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

@blhsing blhsing added the type-bug An unexpected behavior, bug, or error label Feb 6, 2024
@serhiy-storchaka serhiy-storchaka added topic-IO 3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes labels Feb 6, 2024
@serhiy-storchaka serhiy-storchaka self-assigned this Feb 7, 2024
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Feb 8, 2024
@serhiy-storchaka
Copy link
Member

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.

miss-islington pushed a commit to miss-islington/cpython that referenced this issue Feb 9, 2024
…om.read1() (pythonGH-115163)

(cherry picked from commit 846fd72)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Feb 9, 2024
…om.read1() (pythonGH-115163)

(cherry picked from commit 846fd72)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
@serhiy-storchaka
Copy link
Member

Thank you for your report @blhsing. The issue is now fixed.

serhiy-storchaka added a commit that referenced this issue Feb 9, 2024
…dom.read1() (GH-115163) (GH-115205)

(cherry picked from commit 846fd72)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
serhiy-storchaka added a commit that referenced this issue Feb 9, 2024
…dom.read1() (GH-115163) (GH-115206)

(cherry picked from commit 846fd72)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Feb 10, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Feb 10, 2024
(cherry picked from commit 597fad0)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Feb 10, 2024
(cherry picked from commit 597fad0)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
serhiy-storchaka added a commit that referenced this issue Feb 10, 2024
…5245)

(cherry picked from commit 597fad0)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
serhiy-storchaka added a commit that referenced this issue Feb 10, 2024
…5244)

(cherry picked from commit 597fad0)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
fsc-eriker pushed a commit to fsc-eriker/cpython that referenced this issue Feb 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes topic-IO type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants