Skip to content

Commit 7f87ebb

Browse files
pythongh-107801: Improve the accuracy of io.TextIOWrapper.seek docs (python#107933)
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
1 parent 58f9c63 commit 7f87ebb

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

Doc/library/io.rst

+16
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,22 @@ Text I/O
10441044
.. versionchanged:: 3.11
10451045
The method supports ``encoding="locale"`` option.
10461046

1047+
.. method:: seek(cookie, whence=os.SEEK_SET, /)
1048+
1049+
Set the stream position.
1050+
Return the new stream position as an :class:`int`.
1051+
1052+
Four operations are supported,
1053+
given by the following argument combinations:
1054+
1055+
* ``seek(0, SEEK_SET)``: Rewind to the start of the stream.
1056+
* ``seek(cookie, SEEK_SET)``: Restore a previous position;
1057+
*cookie* **must be** a number returned by :meth:`!tell`.
1058+
* ``seek(0, SEEK_END)``: Fast-forward to the end of the stream.
1059+
* ``seek(0, SEEK_CUR)``: Leave the current stream position unchanged.
1060+
1061+
Any other argument combinations are invalid,
1062+
and may raise exceptions.
10471063

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

Modules/_io/clinic/textio.c.h

+21-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/_io/textio.c

+18-2
Original file line numberDiff line numberDiff line change
@@ -2428,13 +2428,29 @@ _textiowrapper_encoder_setstate(textio *self, cookie_type *cookie)
24282428
/*[clinic input]
24292429
_io.TextIOWrapper.seek
24302430
cookie as cookieObj: object
2431-
whence: int = 0
2431+
Zero or an opaque number returned by tell().
2432+
whence: int(c_default='0') = os.SEEK_SET
2433+
The relative position to seek from.
24322434
/
2435+
2436+
Set the stream position, and return the new stream position.
2437+
2438+
Four operations are supported, given by the following argument
2439+
combinations:
2440+
2441+
- seek(0, SEEK_SET): Rewind to the start of the stream.
2442+
- seek(cookie, SEEK_SET): Restore a previous position;
2443+
'cookie' must be a number returned by tell().
2444+
- seek(0, SEEK_END): Fast-forward to the end of the stream.
2445+
- seek(0, SEEK_CUR): Leave the current stream position unchanged.
2446+
2447+
Any other argument combinations are invalid,
2448+
and may raise exceptions.
24332449
[clinic start generated code]*/
24342450

24352451
static PyObject *
24362452
_io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
2437-
/*[clinic end generated code: output=0a15679764e2d04d input=0458abeb3d7842be]*/
2453+
/*[clinic end generated code: output=0a15679764e2d04d input=0f68adcb02cf2823]*/
24382454
{
24392455
PyObject *posobj;
24402456
cookie_type cookie;

0 commit comments

Comments
 (0)