Skip to content

file.read() followed by file.write() result in incorrect behavior #67750

Closed

Description

@maximesteisel
BPO 23562
Nosy @vstinner, @maximesteisel
Superseder
  • bpo-12215: TextIOWrapper: issues with interlaced read-write
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2015-03-02.15:45:52.019>
    created_at = <Date 2015-03-02.14:37:41.806>
    labels = ['expert-IO']
    title = 'file.read() followed by file.write() result in incorrect behavior'
    updated_at = <Date 2015-03-02.15:45:52.018>
    user = 'https://github.com/maximesteisel'

    bugs.python.org fields:

    activity = <Date 2015-03-02.15:45:52.018>
    actor = 'r.david.murray'
    assignee = 'none'
    closed = True
    closed_date = <Date 2015-03-02.15:45:52.019>
    closer = 'r.david.murray'
    components = ['IO']
    creation = <Date 2015-03-02.14:37:41.806>
    creator = 'Maxime S'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 23562
    keywords = []
    message_count = 2.0
    messages = ['237046', '237047']
    nosy_count = 2.0
    nosy_names = ['vstinner', 'Maxime S']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = None
    status = 'closed'
    superseder = '12215'
    type = None
    url = 'https://bugs.python.org/issue23562'
    versions = ['Python 3.2', 'Python 3.3', 'Python 3.4', 'Python 3.5']

    Activity

    1. maximesteisel commented on Mar 2, 2015

      maximesteiselmannequin
      MannequinAuthor

      Observed Behavior:

      $python3
      Python 3.5.0a1+ (default, Mar  2 2015, 14:30:05) 
      [GCC 4.6.3] on linux
      Type "help", "copyright", "credits" or "license" for more information.
      >>> f = open("test", "w+")
      >>> f.write("Hello World")
      11
      >>> f.seek(0)
      0
      >>> f.read(5)
      'Hello'
      >>> f.write(" people")
      7
      >>> f.seek(0)
      0
      >>> f.read()
      'Hello World people'

      Expected Behavior

      According to POSIX, the last f.read() should return "Hello people", like in Python 2:

      $ python2
      Python 2.7.3 (default, Feb 27 2014, 20:00:17) 
      [GCC 4.6.3] on linux2
      Type "help", "copyright", "credits" or "license" for more information.
      >>> f = open("test", "w+")
      >>> f.write("Hello World")
      >>> f.seek(0)
      >>> f.read(5)
      'Hello'
      >>> f.write(" people")
      >>> f.seek(0)
      >>> f.read()
      'Hello people'

      Workaround:

      f.seek(f.tell()) immediately after f.read(5) restore the correct behavior.

    2. vstinner commented on Mar 2, 2015

      @vstinner
      Member

      This issue is probably a duplicate of the issue bpo-12215 (see also bpo-12513 for the codecs moduile).

    3. transferred this issue fromon Apr 10, 2022
    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

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions