Skip to content

Commit

Permalink
[3.12] pythongh-107801: Improve the accuracy of io.TextIOWrapper.seek…
Browse files Browse the repository at this point in the history
… docs (python#107933)

(cherry picked from commit 7f87ebb)

Clearly document the supported seek() operations:

- Rewind to the start of the stream
- Restore a previous stream position (given by tell())
- Fast-forward to the end of the stream
  • Loading branch information
erlend-aasland committed Aug 22, 2023
1 parent 47f60c3 commit 2b744f2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
16 changes: 16 additions & 0 deletions Doc/library/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,22 @@ Text I/O
.. versionchanged:: 3.11
The method supports ``encoding="locale"`` option.

.. method:: seek(cookie, whence=os.SEEK_SET, /)

Set the stream position.
Return the new stream position as an :class:`int`.

Four operations are supported,
given by the following argument combinations:

* ``seek(0, SEEK_SET)``: Rewind to the start of the stream.
* ``seek(cookie, SEEK_SET)``: Restore a previous position;
*cookie* **must be** a number returned by :meth:`!tell`.
* ``seek(0, SEEK_END)``: Fast-forward to the end of the stream.
* ``seek(0, SEEK_CUR)``: Leave the current stream position unchanged.

Any other argument combinations are invalid,
and may raise exceptions.

.. class:: StringIO(initial_value='', newline='\n')

Expand Down
24 changes: 21 additions & 3 deletions Modules/_io/clinic/textio.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -2438,13 +2438,29 @@ _textiowrapper_encoder_setstate(textio *self, cookie_type *cookie)
/*[clinic input]
_io.TextIOWrapper.seek
cookie as cookieObj: object
whence: int = 0
Zero or an opaque number returned by tell().
whence: int(c_default='0') = os.SEEK_SET
The relative position to seek from.
/
Set the stream position, and return the new stream position.
Four operations are supported, given by the following argument
combinations:
- seek(0, SEEK_SET): Rewind to the start of the stream.
- seek(cookie, SEEK_SET): Restore a previous position;
'cookie' must be a number returned by tell().
- seek(0, SEEK_END): Fast-forward to the end of the stream.
- seek(0, SEEK_CUR): Leave the current stream position unchanged.
Any other argument combinations are invalid,
and may raise exceptions.
[clinic start generated code]*/

static PyObject *
_io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
/*[clinic end generated code: output=0a15679764e2d04d input=0458abeb3d7842be]*/
/*[clinic end generated code: output=0a15679764e2d04d input=0f68adcb02cf2823]*/
{
PyObject *posobj;
cookie_type cookie;
Expand Down

0 comments on commit 2b744f2

Please sign in to comment.