Skip to content

bpo-27867: Add a porting guide for PySlice_GetIndicesEx(). #1973

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

Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 17 additions & 1 deletion Doc/c-api/slice.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ Slice Objects

Returns ``0`` on success and ``-1`` on error with exception set.

.. note::
This function is considered not safe for resizable sequences.
Its invocation should be replaced by a combination of
:c:func:`PySlice_Unpack` and :c:func:`PySlice_AdjustIndices` where ::

if (PySlice_GetIndicesEx(slice, length, &start, &stop, &step, &slicelength) < 0) {
// return error
}

is replaced by ::

if (PySlice_Unpack(slice, &start, &stop, &step) < 0) {
// return error
}
slicelength = PySlice_AdjustIndices(length, &start, &stop, step);

.. versionchanged:: 3.2
The parameter type for the *slice* parameter was ``PySliceObject*``
before.
Expand All @@ -61,7 +77,7 @@ Slice Objects
If ``Py_LIMITED_API`` is not set or set to the value between ``0x03050400``
and ``0x03060000`` (not including) or ``0x03060100`` or higher
:c:func:`!PySlice_GetIndicesEx` is implemented as a macro using
:c:func:`PySlice_Unpack` and :c:func:`PySlice_AdjustIndices`.
:c:func:`!PySlice_Unpack` and :c:func:`!PySlice_AdjustIndices`.
Arguments *start*, *stop* and *step* are evaluated more than once.

.. deprecated:: 3.6.1
Expand Down
13 changes: 13 additions & 0 deletions Doc/whatsnew/3.7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,19 @@ Changes in the Python API
in the output. (Contributed by Utkarsh Upadhyay in :issue:`30302`.)


Changes in the C API
--------------------

* The function :c:func:`PySlice_GetIndicesEx` is considered not safe for
resizable sequences. If the slice indices are not instances of :class:`int`,
but objects that implement the :meth:`!__index__` method, the sequence can be
resized after passing its length to :c:func:`!PySlice_GetIndicesEx`. This
can lead to returning indices out of the length of the sequence. For
avoiding possible problems use new functions :c:func:`PySlice_Unpack` and
:c:func:`PySlice_AdjustIndices`.
(Contributed by Serhiy Storchaka in :issue:`27867`.)


CPython bytecode changes
------------------------

Expand Down