Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-107801: Improve the accuracy of io.TextIOWrapper.seek docs #107933

Merged
merged 6 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Doc/library/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@

.. note::

The abstract base classes also provide default implementations of some

Check warning on line 258 in Doc/library/io.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:meth reference target not found: IOBase.readinto
methods in order to help implementation of concrete stream classes. For
example, :class:`BufferedIOBase` provides unoptimized implementations of
:meth:`~IOBase.readinto` and :meth:`~IOBase.readline`.
Expand Down Expand Up @@ -320,7 +320,7 @@
implementations represent a file that cannot be read, written or
seeked.

Even though :class:`IOBase` does not declare :meth:`read`

Check warning on line 323 in Doc/library/io.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:meth reference target not found: read

Check warning on line 323 in Doc/library/io.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:meth reference target not found: write
or :meth:`write` because their signatures will vary, implementations and
clients should consider those methods part of the interface. Also,
implementations may raise a :exc:`ValueError` (or :exc:`UnsupportedOperation`)
Expand Down Expand Up @@ -379,7 +379,7 @@

.. method:: readable()

Return ``True`` if the stream can be read from. If ``False``, :meth:`read`

Check warning on line 382 in Doc/library/io.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:meth reference target not found: read
will raise :exc:`OSError`.

.. method:: readline(size=-1, /)
Expand All @@ -405,15 +405,15 @@

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

Change the stream position to the given byte *offset*. *offset* is

Check warning on line 408 in Doc/library/io.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:data reference target not found: SEEK_SET
interpreted relative to the position indicated by *whence*. The default
value for *whence* is :data:`SEEK_SET`. Values for *whence* are:

* :data:`SEEK_SET` or ``0`` -- start of the stream (the default);

Check warning on line 412 in Doc/library/io.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:data reference target not found: SEEK_SET
*offset* should be zero or positive
* :data:`SEEK_CUR` or ``1`` -- current stream position; *offset* may

Check warning on line 414 in Doc/library/io.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:data reference target not found: SEEK_CUR
be negative
* :data:`SEEK_END` or ``2`` -- end of the stream; *offset* is usually

Check warning on line 416 in Doc/library/io.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:data reference target not found: SEEK_END
negative

Return the new absolute position.
Expand All @@ -422,7 +422,7 @@
The ``SEEK_*`` constants.

.. versionadded:: 3.3
Some operating systems could support additional values, like

Check warning on line 425 in Doc/library/io.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:const reference target not found: os.SEEK_HOLE

Check warning on line 425 in Doc/library/io.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:const reference target not found: os.SEEK_DATA
:const:`os.SEEK_HOLE` or :const:`os.SEEK_DATA`. The valid values
for a file could depend on it being open in text or binary mode.

Expand Down Expand Up @@ -1043,6 +1043,20 @@
.. versionchanged:: 3.11
The method supports ``encoding="locale"`` option.

.. method:: seek(cookie, whence, /)

Set the stream position and return the current position.

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

* ``seek(0, SEEK_SET)``: Rewind to the start of the stream.
* ``seek(n, SEEK_SET)``: Restore a previous position;
'n' is a number returned by :meth:`!tell`.
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved
* ``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 undefined behaviour.
AA-Turner marked this conversation as resolved.
Show resolved Hide resolved
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved

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

Expand Down
23 changes: 20 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.

19 changes: 17 additions & 2 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -2428,13 +2428,28 @@ _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 current position.

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

- seek(0, SEEK_SET): Rewind to the start of the stream.
- seek(n, SEEK_SET): Restore a previous position;
'n' is 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 undefined behaviour.
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved
[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=10ce5c609984638a]*/
{
PyObject *posobj;
cookie_type cookie;
Expand Down
Loading