Skip to content

Adding read/readline() after write appends to EOF, not to the position #117095

Closed

Description

@dom-0

Bug report

Bug description:

with open("testfile", "r+") as my_file:

    text = my_file.write("========")
    print(my_file.readline())

Instead of writing to the beginning of the file, the program writes to the EOF even if readline() is placed after write.

CPython versions tested on:

3.8, 3.9

Operating systems tested on:

Windows

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Mar 20, 2024
  2. TooPercentMilk commented on Apr 2, 2024

    @TooPercentMilk

    I'm a beginner contributor and I'm looking to work on this issue, what should I do?

  3. TooPercentMilk commented on Apr 23, 2024

    @TooPercentMilk

    I've been working on this issue, and it seems that the bug doesn't occur when tell() and seek() are used between the write and readline calls. I'm struggling to properly use the built in seek and tell functions within the textio.c file however, if someone has experience in this module I would appreciate the help.

  4. TooPercentMilk commented on Apr 25, 2024

    @TooPercentMilk

    For anyone who wants to take over this task, here are some test cases I wrote.
    `

    def compare(file1, file2):
        with open(file1, 'r') as file1:
            content1 = file1.read()
        with open(file2, 'r') as file2:
            content2 = file2.read()
        if content1 == content2:
            return True
        else:
            return False
    
    def restore(modified, original):
        for m, o in zip(modified, original):
            with open(o, 'r') as src_file:
                content = src_file.read()
            with open(m, 'w') as dest_file:
                dest_file.write(content)
            src_file.close()
            dest_file.close()
    
    
    def test1a():
        filename = 'test1.txt'
        with open(filename, "r+") as my_file:
            my_file.write("========")
            r = my_file.readline()
    
            my_file.close()
        if compare('test1.txt', 'test1_corr.txt'):
            print('Test 1a - PASSED')
        else:
            print('Test 1a - FAILED')
    
    
    def test1b():
        filename = 'test1.txt'
        with open(filename, "r+") as my_file:
            my_file.write("========")
            my_file.close()
        if compare('test1.txt', 'test1_corr.txt'):
            print('Test 1b - PASSED')
        else:
            print('Test 1b - FAILED')
    
    def test2a():
        filename = 'test2.txt'
        f = open(filename, 'r+')
        f.write('....')
        f.read()
        f.close()
        if compare('test2.txt', 'test2_corr.txt'):
            print('Test 2a - PASSED')
        else:
            print('Test 2a - FAILED')
    
    def test2b():
        filename = 'test2.txt'
        f = open(filename, 'w+')
        f.write('....')
        f.readline()
        f.close()
        if compare('test2.txt', 'test2_corr.txt'):
            print('Test 2b - PASSED')
        else:
            print('Test 2b - FAILED')
    
    def test2c():
        filename = 'test2.txt'
        f = open(filename, 'r+')
        f.write('....')
        f.readline(3)
        f.close()
        if compare('test2.txt', 'test2_corr.txt'):
            print('Test 2c - PASSED')
        else:
            print('Test 2c - FAILED')
    
    def test3a():
        filename = 'test3.txt'
        f = open(filename, 'r+')
        f.write('^^^^^^^^')
        f.readline()
        f.close()
        if compare('test3.txt', 'test3_corr.txt'):
            print('Test 3a - PASSED')
        else:
            print('Test 3a - FAILED')
    
    def test3b():
        filename = 'test3.txt'
        f = open(filename, 'r+')
        print("Initial:", f.tell())
        f.write('^^^^^^^^')
        # print("After write:", f.tell())
        f.readline(3)
        print("End:", f.tell())
        f.close()
        if compare('test3.txt', 'test3_corr.txt'):
            print('Test 3b - PASSED')
        else:
            print('Test 3b - FAILED')
    
    def test3c():
        filename = 'test3.txt'
        f = open(filename, 'r+')
        f.write('^^^^^^^^')
        f.flush()
        f.readline()
        f.close()
        if compare('test3.txt', 'test3_corr.txt'):
            print('Test 3c - PASSED')
        else:
            print('Test 3c - FAILED')
    

    `
    test1.txt
    test1_corr.txt
    test1_orig.txt
    test2.txt
    test2_corr.txt
    test2_orig.txt
    test3.txt
    test3_corr.txt
    test3_orig.txt

  5. vadmium commented on Jul 26, 2024

    @vadmium
    Member

    Might be related to the existing bug reports about writing after reading: #82891 and #70346

  6. cmaloney commented on Oct 9, 2026

    @cmaloney
    Contributor

    The reproducers here pass on python 3.13, 3.14, and 3.15. They seem to have been fixed by gh-115059 which was backported to 3.11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)topic-IOtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions