8000 bpo-27867: Add a porting guide for PySlice_GetIndicesEx(). (#1973) · python/cpython@4d3f084 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4d3f084

Browse files
bpo-27867: Add a porting guide for PySlice_GetIndicesEx(). (#1973)
1 parent 0ccc0f6 commit 4d3f084

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

Doc/c-api/slice.rst

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,22 @@ Slice Objects
5353
5454
Returns ``0`` on success and ``-1`` on error with exception set.
5555
56+
.. note::
57+
This function is considered not safe for resizable sequences.
58+
Its invocation should be replaced by a combination of
59+
:c:func:`PySlice_Unpack` and :c:func:`PySlice_AdjustIndices` where ::
60+
61+
if (PySlice_GetIndicesEx(slice, length, &start, &stop, &step, &slicelength) < 0) {
62+
// return error
63+
}
64+
65+
is replaced by ::
66+
67+
if (PySlice_Unpack(slice, &start, &stop, &step) < 0) {
68+
// return error
69+
}
70+
slicelength = PySlice_AdjustIndices(length, &start, &stop, step);
71+
5672
.. versionchanged:: 3.2
5773
The parameter type for the *slice* parameter was ``PySliceObject*``
5874
before.
@@ -61,7 +77,7 @@ Slice Objects
6177
If ``Py_LIMITED_API`` is not set or set to the value between ``0x03050400``
6278
and ``0x03060000`` (not including) or ``0x03060100`` or higher
6379
:c:func:`!PySlice_GetIndicesEx` is implemented as a macro using
64-
:c:func:`PySlice_Unpack` and :c:func:`PySlice_AdjustIndices`.
80+
:c:func:`!PySlice_Unpack` and :c:func:`!PySlice_AdjustIndices`.
6581
Arguments *start*, *stop* and *step* are evaluated more than once.
6682
6783
.. deprecated:: 3.6.1

Doc/whatsnew/3.7.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,19 @@ Changes in the Python API
581581
in the output. (Contributed by Utkarsh Upadhyay in :issue:`30302`.)
582582

583583

584+
Changes in the C API
585+
--------------------
586+
587+
* The function :c:func:`PySlice_GetIndicesEx` is considered not safe for
588+
resizable sequences. If the slice indices are not instances of :class:`int`,
589+
but objects that implement the :meth:`!__index__` method, the sequence can be
590+
resized after passing its length to :c:func:`!PySlice_GetIndicesEx`. This
591+
can lead to returning indices out of the length of the sequence. For
592+
avoiding possible problems use new functions :c:func:`PySlice_Unpack` and
593+
:c:func:`PySlice_AdjustIndices`.
594+
(Contributed by Serhiy Storchaka in :issue:`27867`.)
595+
596+
584597
CPython bytecode changes
585598
------------------------
586599

0 commit comments

Comments
 (0)
0