This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Support (SEEK_END/SEEK_CUR) relative seeking in StringIO
Type: enhancement Stage: resolved
Components: IO, Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Define StringIO seek offset as code point offset
View: 25190
Assigned To: Nosy List: random832, sanketplus, serhiy.storchaka
Priority: normal Keywords:

Created on 2020-01-17 04:23 by random832, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg360158 - (view) Author: (random832) Date: 2020-01-17 04:23
Currently this fails with a (misleading in the case of SEEK_END) "Can't do nonzero cur-relative seeks" error, but there's no conceptual reason it shouldn't be possible.

The offset should simply be taken as a character offset, without any pretense that the "file" represents bytes in some Unicode encoding. This is already done for SEEK_START and tell, and has not caused any problems.
msg360256 - (view) Author: sanket (sanketplus) Date: 2020-01-19 12:27
can you provide more details? Python version and code/steps to reproduce. That'd be more helpful as it seems the methods are already implemented in latest version https://docs.python.org/3/library/io.html#io.TextIOBase.seek
msg360284 - (view) Author: (random832) Date: 2020-01-20 02:56
That documentation isn't specific to StringIO, and in any case, the limitation in question isn't documented. The actual implementation is at https://github.com/python/cpython/blob/HEAD/Modules/_io/stringio.c#L484

But if examples would help, they're simple to come up with:

>>> f = io.StringIO('t\xe9st')
>>> f.seek(-1, io.SEEK_END)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: Can't do nonzero cur-relative seeks
>>> f.seek(2, io.SEEK_CUR)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: Can't do nonzero cur-relative seeks
# demonstration that SEEK_SET works treating all characters as one unit
>>> f.seek(2, io.SEEK_SET)
2
>>> f.read()
'st'

As far as I know this is the case in all currently maintained versions of Python 3, since the C-based unicode StringIO implementation was added in 2008.
msg360297 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-01-20 08:22
This is a duplicate of issue25190.
History
Date User Action Args
2022-04-11 14:59:25adminsetgithub: 83546
2020-01-20 08:22:33serhiy.storchakasetstatus: open -> closed

resolution: duplicate

type: behavior -> enhancement
stage: resolved
versions: + Python 3.9
nosy: + serhiy.storchaka
messages: + msg360297
components: + Library (Lib)
superseder: Define StringIO seek offset as code point offset
2020-01-20 02:56:55random832setmessages: + msg360284
2020-01-19 12:27:50sanketplussetnosy: + sanketplus
messages: + msg360256
2020-01-17 04:23:15random832create