Skip to content

The return of truncate(size=None) (file io) is unexpected #79485

Closed

Description

@liugang93
mannequin
BPO 35304
Nosy @vadmium
Superseder
  • bpo-26158: File truncate() not defaulting to current position as documented
  • 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 2018-11-24.01:11:07.036>
    created_at = <Date 2018-11-23.19:35:08.389>
    labels = ['type-bug', 'expert-IO']
    title = 'The return of truncate(size=None) (file io) is unexpected'
    updated_at = <Date 2018-11-24.01:11:06.953>
    user = 'https://bugs.python.org/liugang93'

    bugs.python.org fields:

    activity = <Date 2018-11-24.01:11:06.953>
    actor = 'martin.panter'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-11-24.01:11:07.036>
    closer = 'martin.panter'
    components = ['IO']
    creation = <Date 2018-11-23.19:35:08.389>
    creator = 'liugang93'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 35304
    keywords = []
    message_count = 3.0
    messages = ['330357', '330358', '330368']
    nosy_count = 2.0
    nosy_names = ['martin.panter', 'liugang93']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '26158'
    type = 'behavior'
    url = 'https://bugs.python.org/issue35304'
    versions = ['Python 3.5']

    Activity

    1. liugang93 commented on Nov 23, 2018

      liugang93mannequin
      MannequinAuthor

      -- run in Windows 10

      1 -
      f = open('test', "w+")
      f.write('xxx\nyyy\nzzz')

      f.seek(0)
      
      f.readline()
      print(f.tell())  # output 5 (xxx\r\n)
      
      x = f.truncate()
      print(x)  # output 13 (xxx\r\nyyy\r\nzzz), why it is 13, not 5?

      2 -
      f = open('test', "w+")
      f.write('xxx\nyyy\nzzz')

      f.seek(0)
      
      f.readline()
      print(f.tell())  # output 5 (xxx\r\n)
      
      f.seek(f.tell())
      
      x = f.truncate()
      print(x)  # output 5
    2. added
      type-bug
      on Nov 23, 2018
    3. liugang93 commented on Nov 23, 2018

      liugang93mannequin
      MannequinAuthor

      # Run in Windows 10

      # Code snippet 1
      f = open('test', "w+")
      f.write('xxx\nyyy\nzzz')
      
      f.seek(0)
      
      f.readline()
      print(f.tell())  # 5
      
      x = f.truncate()
      print(x)  # 13 - why it is 13, not 5?
      
      
      # Code snippet 2
      f = open('test', "w+")
      f.write('xxx\nyyy\nzzz')
      
      f.seek(0)
      
      f.readline()
      print(f.tell())  # 5
      
      f.seek(f.tell())
      
      x = f.truncate()
      print(x)  # 5
    4. vadmium commented on Nov 24, 2018

      @vadmium
      Member

      This is the same as bpo-26158. Truncating text files is not clearly documented for a start, and truncating after reading doesn’t seem to be considered much in the implementations.

      Your question is answered at <https://bugs.python.org/issue26158#msg258619\>. Your code calls the C implementation of “io.TextIOWrapper.truncate”. This implementation does not consider that there are 8 unread bytes in a buffer, so it truncates the file at the end of the buffer (5 read + 8 unread = 13 bytes).

    5. 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

      topic-IOtype-bug

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions