Skip to content
Open
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
11 changes: 6 additions & 5 deletions Doc/c-api/slice.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ Slice Objects
is replaced by ::

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

.. versionchanged:: 3.2
The parameter type for the *slice* parameter was ``PySliceObject*``
Expand All @@ -83,10 +85,9 @@ Slice Objects
:c:func:`!PySlice_Unpack` and :c:func:`!PySlice_AdjustIndices`.
Arguments *start*, *stop* and *step* are evaluated more than once.

.. deprecated:: 3.6.1
If ``Py_LIMITED_API`` is set to the value less than ``0x03050400`` or
between ``0x03060000`` and ``0x03060100`` (not including)
:c:func:`!PySlice_GetIndicesEx` is a deprecated function.
.. versionchanged:: next
:c:func:`!PySlice_GetIndicesEx` is now implemented as a static inline
function. Arguments are only evaluated once.


.. c:function:: int PySlice_Unpack(PyObject *slice, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
Expand Down
1 change: 0 additions & 1 deletion Doc/data/stable_abi.dat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Doc/deprecations/c-api-pending-removal-in-future.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ although there is currently no date scheduled for their removal.
Use :c:func:`PyModule_GetFilenameObject` instead.
* :c:func:`PyOS_AfterFork`:
Use :c:func:`PyOS_AfterFork_Child` instead.
* :c:func:`PySlice_GetIndicesEx`:
Use :c:func:`PySlice_Unpack` and :c:func:`PySlice_AdjustIndices` instead.
* :c:func:`PyUnicode_READY`:
Unneeded since Python 3.12
* :c:func:`!PyErr_Display`:
Expand Down
5 changes: 5 additions & 0 deletions Doc/whatsnew/3.16.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,11 @@ Deprecated C APIs
and :c:func:`PyAsyncGen_New` are deprecated.
They are scheduled for removal in 3.18.

* Remove :c:func:`PySlice_GetIndicesEx` function, deprecated since Python 3.7.
Only remove the function implementation: :c:func:`PySlice_GetIndicesEx`
remains available as a static inline function, so the API does not change.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the C API is not affected, I'm not sure that it's worth it to mention this change to users. They should not be affected in practice. (The Changelog can also be removed.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add a test.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, if you want to write tests, please go ahead! I was planning to do that after you told me that length should be evaluated later.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #158120.

(Contributed by Victor Stinner in :gh:`158077`.)

.. Add C API deprecations above alphabetically, not here at the end.
.. include:: ../deprecations/c-api-pending-removal-in-3.18.rst
Expand Down
29 changes: 17 additions & 12 deletions Include/sliceobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,28 @@ PyAPI_FUNC(PyObject *) PySlice_New(PyObject* start, PyObject* stop,
PyObject* step);
PyAPI_FUNC(int) PySlice_GetIndices(PyObject *r, Py_ssize_t length,
Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step);
Py_DEPRECATED(3.7)
PyAPI_FUNC(int) PySlice_GetIndicesEx(PyObject *r, Py_ssize_t length,
Py_ssize_t *start, Py_ssize_t *stop,
Py_ssize_t *step,
Py_ssize_t *slicelength);

#if !defined(Py_LIMITED_API) || (Py_LIMITED_API+0 >= 0x03050400 && Py_LIMITED_API+0 < 0x03060000) || Py_LIMITED_API+0 >= 0x03060100

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that Python 3.16 should both with limited C API 3.5.x. Use Python 3.16 to target the recent stable ABI. Or use an old Python version to target an old stable ABI version. I prefer to remove (Py_LIMITED_API+0 >= 0x03050400 && Py_LIMITED_API+0 < 0x03060000).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Python 3.6 is no longer supported upstream, we may even simplify the check as Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 7).

#define PySlice_GetIndicesEx(slice, length, start, stop, step, slicelen) ( \
PySlice_Unpack((slice), (start), (stop), (step)) < 0 ? \
((*(slicelen) = 0), -1) : \
((*(slicelen) = PySlice_AdjustIndices((length), (start), (stop), *(step))), \
0))

#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03060100
PyAPI_FUNC(int) PySlice_Unpack(PyObject *slice,
Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step);
PyAPI_FUNC(Py_ssize_t) PySlice_AdjustIndices(Py_ssize_t length,
Py_ssize_t *start, Py_ssize_t *stop,
Py_ssize_t step);

static inline int
_PySlice_GetIndicesEx(PyObject *slice, Py_ssize_t length,
Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
Py_ssize_t *slicelen)
{
if (PySlice_Unpack(slice, start, stop, step) < 0) {
*slicelen = 0;
return -1;
}

*slicelen = PySlice_AdjustIndices(length, start, stop, *step);
return 0;
}
#define PySlice_GetIndicesEx _PySlice_GetIndicesEx

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The static inline function should have a different name, so sliceobject.c can implement a function under "PySlice_GetIndicesEx" name.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong. The point of the macro is that length is evaluated after calling PySlice_Unpack. This can only be implemented as a macro.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah? It wasn't obvious to me at all when I read the macro. Also, it seems like PySlice_GetIndicesEx() is not tested.

#endif

#ifndef Py_LIMITED_API
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Remove :c:func:`PySlice_GetIndicesEx` function, deprecated since Python 3.7.
Only remove the function implementation: :c:func:`PySlice_GetIndicesEx`
remains available as a static inline function, so the API does not change.
Patch by Victor Stinner.
1 change: 1 addition & 0 deletions Misc/stable_abi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,7 @@
added = '3.2'
[function.PySlice_GetIndicesEx]
added = '3.2'
abi_only = true
[function.PySlice_New]
added = '3.2'
[data.PySlice_Type]
Expand Down
8 changes: 6 additions & 2 deletions Objects/sliceobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,17 @@ PySlice_AdjustIndices(Py_ssize_t length,

#undef PySlice_GetIndicesEx

int
// Function removed from Python 3.16 C API, but kept in the stable ABI
PyAPI_FUNC(int)
PySlice_GetIndicesEx(PyObject *_r, Py_ssize_t length,
Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
Py_ssize_t *slicelength)
{
if (PySlice_Unpack(_r, start, stop, step) < 0)
if (PySlice_Unpack(_r, start, stop, step) < 0) {
*slicelength = 0;
return -1;
}

*slicelength = PySlice_AdjustIndices(length, start, stop, *step);
return 0;
}
Expand Down
Loading