You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
fromioimportStringIOtxt=StringIO('0123456789')
txt.seek(15,0) # no problem with absolute seektxt.write('xxx')
s=txt.getvalue()
print(ord(s[12]))
# 0
So that is not a reason to limit seeking from other positions either.
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.
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
Activity
terryjreedy commentedon Sep 6, 2011
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.
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.
So that is not a reason to limit seeking from other positions either.
pitrou commentedon Dec 22, 2011
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.
python-dev commentedon Jan 21, 2012
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
vadmium commentedon Sep 20, 2015
Opened bpo-25190 about the enhancing StringIO side of this.