Skip to content

StringIO and seek() #57131

Closed
Closed

Description

@terryjreedy
BPO 12922
Nosy @terryjreedy, @pitrou, @PierreQuentel, @vadmium

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 2012-01-21.19:30:32.285>
created_at = <Date 2011-09-06.20:48:49.879>
labels = ['type-bug', 'library', 'expert-IO', 'docs']
title = 'StringIO and seek()'
updated_at = <Date 2015-09-20.06:17:30.011>
user = 'https://github.com/terryjreedy'

bugs.python.org fields:

activity = <Date 2015-09-20.06:17:30.011>
actor = 'martin.panter'
assignee = 'docs@python'
closed = True
closed_date = <Date 2012-01-21.19:30:32.285>
closer = 'pitrou'
components = ['Documentation', 'Library (Lib)', 'IO']
creation = <Date 2011-09-06.20:48:49.879>
creator = 'terry.reedy'
dependencies = []
files = []
hgrepos = []
issue_num = 12922
keywords = []
message_count = 4.0
messages = ['143649', '150078', '151741', '251150']
nosy_count = 6.0
nosy_names = ['terry.reedy', 'pitrou', 'docs@python', 'quentel', 'python-dev', 'martin.panter']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue12922'
versions = ['Python 2.7', 'Python 3.2', 'Python 3.3']

Activity

  1. terryjreedy commented on Sep 6, 2011

    @terryjreedy
    MemberAuthor

    First, there is a minor documentation issue.
    15.2.3.1. I/O Base Classes
    class io.IOBase
    seek(offset, whence=SEEK_SET)
    Change the stream position to the given byte offset

    Since StringIO seeks by code units that should perhaps say 'byte or code unit offset' or a separate note should be added to the doc entry for StringIO.

    >>> txt = StringIO('ab\U00010030')
    >>> txt.seek(3)
    3
    >>> txt.write('x')
    1
    >>> txt.getvalue()
    'ab\ud800x'

    The behavior problem is that seeking for StringIO does not work relative to the current position or end.

    IOError: Can't do nonzero cur-relative seeks
    # Note: this message is wrong for end-relative seeks.

    I presume this is inherited from an undocumented restriction on seeking with text streams, because chars *might* be variably sized. However, I do not think it should be. StringIO does not inherit the same reason for the restriction (certainly not on wide builds, and on narrow builds, seeking from the beginning is just as problematical). For StringIO, there is no option of 'opening in binary (byte) mode instead' as there is for disk files. Since a StringIO object is a wrapped array of fixed-size units, seeking from any position is as trivial as it is from the beginning. And again, the current docs imply that it should work.

    Note that seeking from the beginning is not limited to the existing content. Instead, skipped areas are filled with nulls.

    from io import StringIO
    txt = StringIO('0123456789')
    txt.seek(15,0) # no problem with absolute seek
    txt.write('xxx')
    s  = txt.getvalue()
    print(ord(s[12]))
    # 0

    So that is not a reason to limit seeking from other positions either.

  2. added
    type-bugAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directory
    on Sep 6, 2011
  3. added
    type-featureA feature request or enhancement
    and removed
    type-bugAn unexpected behavior, bug, or error
    on Sep 7, 2011
  4. pitrou commented on Dec 22, 2011

    @pitrou
    Member

    I would rather document it in TextIOBase:
    http://docs.python.org/dev/library/io.html#io.TextIOBase

    With text I/O streams, tell() returns an arbitrary "position cookie", meaning you can't meaningfully do arithmetic on it: this is why cur-relative seeking and end-relative seeking isn't supported.

    Of course, on StringIO the "arbitrary position cookie" is a perfectly well-defined character offset, so we *could* specifically enhance StringIO.tell. Whether it's a good idea to do it (while arbitrary text files would still have the limitation) is left to debate.

  5. added
    type-bugAn unexpected behavior, bug, or error
    and removed
    type-featureA feature request or enhancement
    on Dec 22, 2011
  6. python-dev commented on Jan 21, 2012

    python-devmannequin
    Mannequin

    New changeset 03e61104f7a2 by Antoine Pitrou in branch '3.2':
    Issue bpo-12922: fix the TextIOBase documentation to include a description of seek() and tell() methods.
    http://hg.python.org/cpython/rev/03e61104f7a2

    New changeset f7e5abfb31ea by Antoine Pitrou in branch 'default':
    Issue bpo-12922: fix the TextIOBase documentation to include a description of seek() and tell() methods.
    http://hg.python.org/cpython/rev/f7e5abfb31ea

    New changeset fcf4d547bed8 by Antoine Pitrou in branch '2.7':
    Issue bpo-12922: fix the TextIOBase documentation to include a description of seek() and tell() methods.
    http://hg.python.org/cpython/rev/fcf4d547bed8

  7. vadmium commented on Sep 20, 2015

    @vadmium
    Member

    Opened bpo-25190 about the enhancing StringIO side of this.

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

    docsDocumentation in the Doc dirstdlibStandard Library Python modules in the Lib/ directorytopic-IOtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions