Skip to content

Commit 2143d7e

Browse files
[3.11] pythongh-107801: Improve the accuracy of io.TextIOWrapper.seek 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
1 parent 4d4871e commit 2143d7e

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
@@ -2415,13 +2415,29 @@ _textiowrapper_encoder_setstate(textio *self, cookie_type *cookie)
24152415
/*[clinic input]
24162416
_io.TextIOWrapper.seek
24172417
cookie as cookieObj: object
2418-
whence: int = 0
2418+
Zero or an opaque number returned by tell().
2419+
whence: int(c_default='0') = os.SEEK_SET
2420+
The relative position to seek from.
24192421
/
2422+
2423+
Set the stream position, and return the new stream position.
2424+
2425+
Four operations are supported, given by the following argument
2426+
combinations:
2427+
2428+
- seek(0, SEEK_SET): Rewind to the start of the stream.
2429+
- seek(cookie, SEEK_SET): Restore a previous position;
2430+
'cookie' must be a number returned by tell().
2431+
- seek(0, SEEK_END): Fast-forward to the end of the stream.
2432+
- seek(0, SEEK_CUR): Leave the current stream position unchanged.
2433+
2434+
Any other argument combinations are invalid,
2435+
and may raise exceptions.
24202436
[clinic start generated code]*/
24212437

24222438
static PyObject *
24232439
_io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
2424-
/*[clinic end generated code: output=0a15679764e2d04d input=0458abeb3d7842be]*/
2440+
/*[clinic end generated code: output=0a15679764e2d04d input=0f68adcb02cf2823]*/
24252441
{
24262442
PyObject *posobj;
24272443
cookie_type cookie;

0 commit comments

Comments
 (0)