Skip to content

Commit d087aaf

Browse files
pythongh-107801: Improve io.*.seek docs and docstrings
- Name positional parameters consistently: seek(offset, whence, /) - Add param docstrings to _io._IOBase.seek, so the various implementations interit them. - Use io.SEEK_*, not os.SEEK_* - Override docstrings for subclasses where 'offset' must be zero for SEEK_CUR and SEEK_END.
1 parent 2e1f688 commit d087aaf

File tree

15 files changed

+160
-135
lines changed

15 files changed

+160
-135
lines changed

Doc/library/io.rst

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,19 @@ High-level Module Interface
168168
:func:`os.stat`) if possible.
169169

170170

171+
.. data:: SEEK_SET
172+
SEEK_CUR
173+
SEEK_END
174+
175+
Parameters to the :meth:`~IOBase.seek` method,
176+
used to specify the relative position
177+
Their values are 0, 1, and 2, respectively.
178+
179+
.. seealso::
180+
181+
:data:`os.SEEK_SET`, :data:`os.SEEK_CUR`, and :data:`os.SEEK_END`.
182+
183+
171184
.. function:: open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
172185

173186
This is an alias for the builtin :func:`open` function.
@@ -405,18 +418,17 @@ I/O Base Classes
405418

406419
.. method:: seek(offset, whence=SEEK_SET, /)
407420

408-
Change the stream position to the given byte *offset*. *offset* is
409-
interpreted relative to the position indicated by *whence*. The default
410-
value for *whence* is :data:`SEEK_SET`. Values for *whence* are:
421+
Change the stream position to the given byte *offset*,
422+
and return the new absolute position as an integer.
423+
*offset* is interpreted relative to the position indicated by *whence*.
424+
Valid values for *whence* are:
411425

412-
* :data:`SEEK_SET` or ``0`` -- start of the stream (the default);
426+
* :data:`SEEK_SET` -- seek from the start of the stream (the default);
413427
*offset* should be zero or positive
414-
* :data:`SEEK_CUR` or ``1`` -- current stream position; *offset* may
415-
be negative
416-
* :data:`SEEK_END` or ``2`` -- end of the stream; *offset* is usually
417-
negative
418-
419-
Return the new absolute position.
428+
* :data:`SEEK_CUR` -- seek from current stream position;
429+
*offset* may be negative
430+
* :data:`SEEK_END` -- seek from the end of the stream;
431+
*offset* is usually negative
420432

421433
.. versionadded:: 3.1
422434
The ``SEEK_*`` constants.
@@ -911,21 +923,22 @@ Text I/O
911923

912924
.. method:: seek(offset, whence=SEEK_SET, /)
913925

914-
Change the stream position to the given *offset*. Behaviour depends on
915-
the *whence* parameter. The default value for *whence* is
916-
:data:`SEEK_SET`.
917-
918-
* :data:`SEEK_SET` or ``0``: seek from the start of the stream
919-
(the default); *offset* must either be a number returned by
920-
:meth:`TextIOBase.tell`, or zero. Any other *offset* value
921-
produces undefined behaviour.
922-
* :data:`SEEK_CUR` or ``1``: "seek" to the current position;
923-
*offset* must be zero, which is a no-operation (all other values
924-
are unsupported).
925-
* :data:`SEEK_END` or ``2``: seek to the end of the stream;
926-
*offset* must be zero (all other values are unsupported).
927-
928-
Return the new absolute position as an opaque number.
926+
Change the stream position to the given byte *offset*,
927+
and return the new absolute position as an opaque number.
928+
*offset* is interpreted relative to the position indicated by *whence*.
929+
Valid values for *whence* are:
930+
931+
* :data:`SEEK_SET` -- seek from the start of the stream (the default);
932+
*offset* must either be a number returned by
933+
:meth:`TextIOBase.tell`, or zero.
934+
Any other *offset* value produces undefined behaviour.
935+
* :data:`SEEK_CUR` -- seek from current stream position;
936+
*offset* must be zero, which is a no-operation.
937+
All other values are unsupported.
938+
* :data:`SEEK_END` -- seek from the end of the stream;
939+
*offset* must be zero.
940+
All other values are unsupported.
941+
929942

930943
.. versionadded:: 3.1
931944
The ``SEEK_*`` constants.

Doc/tutorial/inputoutput.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,12 @@ an opaque number when in text mode.
433433

434434
To change the file object's position, use ``f.seek(offset, whence)``. The position is computed
435435
from adding *offset* to a reference point; the reference point is selected by
436-
the *whence* argument. A *whence* value of 0 measures from the beginning
437-
of the file, 1 uses the current file position, and 2 uses the end of the file as
438-
the reference point. *whence* can be omitted and defaults to 0, using the
439-
beginning of the file as the reference point. ::
436+
the *whence* argument.
437+
A *whence* value of :data:`io.SEEK_SET` measures from the beginning of the file,
438+
:data:`io.SEEK_CUR` uses the current file position,
439+
and :data:`io.SEEK_END` uses the end of the file as the reference point.
440+
*whence* can be omitted and defaults to :data:`!io.SEEK_SET`,
441+
using the beginning of the file as the reference point. ::
440442

441443
>>> f = open('workfile', 'rb+')
442444
>>> f.write(b'0123456789abcdef')
@@ -445,14 +447,14 @@ beginning of the file as the reference point. ::
445447
5
446448
>>> f.read(1)
447449
b'5'
448-
>>> f.seek(-3, 2) # Go to the 3rd byte before the end
450+
>>> f.seek(-3, io.SEEK_END) # Go to the 3rd byte before the end
449451
13
450452
>>> f.read(1)
451453
b'd'
452454

453455
In text files (those opened without a ``b`` in the mode string), only seeks
454456
relative to the beginning of the file are allowed (the exception being seeking
455-
to the very file end with ``seek(0, 2)``) and the only valid *offset* values are
457+
to the very file end with ``seek(0, io.SEEK_END)``) and the only valid *offset* values are
456458
those returned from the ``f.tell()``, or zero. Any other *offset* value produces
457459
undefined behaviour.
458460

Doc/whatsnew/3.1.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,8 @@ New, Improved, and Deprecated Modules
367367

368368
(Contributed by Benjamin Peterson and Antoine Pitrou.)
369369

370-
* The :mod:`io` module has three new constants for the :meth:`seek`
371-
method :data:`SEEK_SET`, :data:`SEEK_CUR`, and :data:`SEEK_END`.
370+
* The :mod:`io` module has three new constants for the :meth:`~io.IOBase.seek`
371+
method :data:`~io.SEEK_SET`, :data:`~io.SEEK_CUR`, and :data:`~io.SEEK_END`.
372372

373373
* The :data:`sys.version_info` tuple is now a named tuple::
374374

Modules/_io/bufferedio.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,14 +1284,14 @@ _io__Buffered_tell_impl(buffered *self)
12841284

12851285
/*[clinic input]
12861286
_io._Buffered.seek
1287-
target as targetobj: object
1288-
whence: int = 0
1287+
offset as targetobj: object
1288+
whence: int(c_default='SEEK_SET') = io.SEEK_SET
12891289
/
12901290
[clinic start generated code]*/
12911291

12921292
static PyObject *
12931293
_io__Buffered_seek_impl(buffered *self, PyObject *targetobj, int whence)
1294-
/*[clinic end generated code: output=7ae0e8dc46efdefb input=a9c4920bfcba6163]*/
1294+
/*[clinic end generated code: output=7ae0e8dc46efdefb input=51b4741b1f355115]*/
12951295
{
12961296
Py_off_t target, n;
12971297
PyObject *res = NULL;

Modules/_io/bytesio.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -636,22 +636,18 @@ bytesio_iternext(bytesio *self)
636636

637637
/*[clinic input]
638638
_io.BytesIO.seek
639-
pos: Py_ssize_t
640-
whence: int = 0
639+
offset as pos: Py_ssize_t
640+
whence: int(c_default='SEEK_SET') = io.SEEK_SET
641641
/
642642
643-
Change stream position.
643+
Change stream position and return the new absolute position.
644644
645-
Seek to byte offset pos relative to position indicated by whence:
646-
0 Start of stream (the default). pos should be >= 0;
647-
1 Current position - pos may be negative;
648-
2 End of stream - pos usually negative.
649-
Returns the new absolute position.
645+
Set the byte offset 'offset', relative to position indicated by 'whence':
650646
[clinic start generated code]*/
651647

652648
static PyObject *
653649
_io_BytesIO_seek_impl(bytesio *self, Py_ssize_t pos, int whence)
654-
/*[clinic end generated code: output=c26204a68e9190e4 input=1e875e6ebc652948]*/
650+
/*[clinic end generated code: output=c26204a68e9190e4 input=2dd44abb89ad75fb]*/
655651
{
656652
CHECK_CLOSED(self);
657653

Modules/_io/clinic/bufferedio.c.h

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/_io/clinic/bytesio.c.h

Lines changed: 5 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/_io/clinic/fileio.c.h

Lines changed: 4 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/_io/clinic/iobase.c.h

Lines changed: 22 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/_io/clinic/stringio.c.h

Lines changed: 11 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)