Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
| 3 | .. _slice-objects: |
| 4 | |
| 5 | Slice Objects |
| 6 | ------------- |
| 7 | |
| 8 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 9 | .. c:var:: PyTypeObject PySlice_Type |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 10 | |
Georg Brandl | 2aff335 | 2010-10-17 10:59:41 +0000 | [diff] [blame] | 11 | The type object for slice objects. This is the same as :class:`slice` in the |
| 12 | Python layer. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 13 | |
| 14 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 15 | .. c:function:: int PySlice_Check(PyObject *ob) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 16 | |
| 17 | Return true if *ob* is a slice object; *ob* must not be *NULL*. |
| 18 | |
| 19 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 20 | .. c:function:: PyObject* PySlice_New(PyObject *start, PyObject *stop, PyObject *step) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 21 | |
| 22 | Return a new slice object with the given values. The *start*, *stop*, and |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 23 | *step* parameters are used as the values of the slice object attributes of |
| 24 | the same names. Any of the values may be *NULL*, in which case the |
| 25 | ``None`` will be used for the corresponding attribute. Return *NULL* if |
| 26 | the new object could not be allocated. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 27 | |
| 28 | |
Martin v. Löwis | dd59097 | 2010-12-11 18:17:22 +0000 | [diff] [blame] | 29 | .. c:function:: int PySlice_GetIndices(PyObject *slice, Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 30 | |
| 31 | Retrieve the start, stop and step indices from the slice object *slice*, |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 32 | assuming a sequence of length *length*. Treats indices greater than |
| 33 | *length* as errors. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 34 | |
Serhiy Storchaka | 1ecf7d2 | 2016-10-27 21:41:19 +0300 | [diff] [blame] | 35 | Returns ``0`` on success and ``-1`` on error with no exception set (unless one of |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 36 | the indices was not :const:`None` and failed to be converted to an integer, |
Serhiy Storchaka | 1ecf7d2 | 2016-10-27 21:41:19 +0300 | [diff] [blame] | 37 | in which case ``-1`` is returned with an exception set). |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 38 | |
Georg Brandl | e6bcc91 | 2008-05-12 18:05:20 +0000 | [diff] [blame] | 39 | You probably do not want to use this function. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 40 | |
Martin v. Löwis | 6d9839d | 2010-12-11 19:22:04 +0000 | [diff] [blame] | 41 | .. versionchanged:: 3.2 |
Georg Brandl | 18d378d | 2010-12-11 22:19:34 +0000 | [diff] [blame] | 42 | The parameter type for the *slice* parameter was ``PySliceObject*`` |
| 43 | before. |
Martin v. Löwis | 6d9839d | 2010-12-11 19:22:04 +0000 | [diff] [blame] | 44 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 45 | |
Martin v. Löwis | dd59097 | 2010-12-11 18:17:22 +0000 | [diff] [blame] | 46 | .. c:function:: int PySlice_GetIndicesEx(PyObject *slice, Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 47 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 48 | Usable replacement for :c:func:`PySlice_GetIndices`. Retrieve the start, |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 49 | stop, and step indices from the slice object *slice* assuming a sequence of |
| 50 | length *length*, and store the length of the slice in *slicelength*. Out |
| 51 | of bounds indices are clipped in a manner consistent with the handling of |
| 52 | normal slices. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 53 | |
Serhiy Storchaka | 1ecf7d2 | 2016-10-27 21:41:19 +0300 | [diff] [blame] | 54 | Returns ``0`` on success and ``-1`` on error with exception set. |
Jeroen Ruigrok van der Werven | bd87552 | 2009-04-26 21:06:15 +0000 | [diff] [blame] | 55 | |
Serhiy Storchaka | 4d3f084 | 2017-10-08 12:53:34 +0300 | [diff] [blame] | 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 | |
Martin v. Löwis | 6d9839d | 2010-12-11 19:22:04 +0000 | [diff] [blame] | 72 | .. versionchanged:: 3.2 |
Georg Brandl | 18d378d | 2010-12-11 22:19:34 +0000 | [diff] [blame] | 73 | The parameter type for the *slice* parameter was ``PySliceObject*`` |
| 74 | before. |
Serhiy Storchaka | 6e08baf | 2017-01-25 13:27:44 +0200 | [diff] [blame] | 75 | |
| 76 | .. versionchanged:: 3.6.1 |
| 77 | If ``Py_LIMITED_API`` is not set or set to the value between ``0x03050400`` |
| 78 | and ``0x03060000`` (not including) or ``0x03060100`` or higher |
| 79 | :c:func:`!PySlice_GetIndicesEx` is implemented as a macro using |
Serhiy Storchaka | 4d3f084 | 2017-10-08 12:53:34 +0300 | [diff] [blame] | 80 | :c:func:`!PySlice_Unpack` and :c:func:`!PySlice_AdjustIndices`. |
Serhiy Storchaka | 6e08baf | 2017-01-25 13:27:44 +0200 | [diff] [blame] | 81 | Arguments *start*, *stop* and *step* are evaluated more than once. |
| 82 | |
| 83 | .. deprecated:: 3.6.1 |
| 84 | If ``Py_LIMITED_API`` is set to the value less than ``0x03050400`` or |
| 85 | between ``0x03060000`` and ``0x03060100`` (not including) |
| 86 | :c:func:`!PySlice_GetIndicesEx` is a deprecated function. |
| 87 | |
| 88 | |
| 89 | .. c:function:: int PySlice_Unpack(PyObject *slice, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step) |
| 90 | |
| 91 | Extract the start, stop and step data members from a slice object as |
| 92 | C integers. Silently reduce values larger than ``PY_SSIZE_T_MAX`` to |
| 93 | ``PY_SSIZE_T_MAX``, silently boost the start and stop values less than |
Xiang Zhang | 2ddf5a1 | 2017-05-10 18:19:41 +0800 | [diff] [blame] | 94 | ``PY_SSIZE_T_MIN`` to ``PY_SSIZE_T_MIN``, and silently boost the step |
Serhiy Storchaka | 6e08baf | 2017-01-25 13:27:44 +0200 | [diff] [blame] | 95 | values less than ``-PY_SSIZE_T_MAX`` to ``-PY_SSIZE_T_MAX``. |
| 96 | |
| 97 | Return ``-1`` on error, ``0`` on success. |
| 98 | |
| 99 | .. versionadded:: 3.6.1 |
| 100 | |
| 101 | |
| 102 | .. c:function:: Py_ssize_t PySlice_AdjustIndices(Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step) |
| 103 | |
| 104 | Adjust start/end slice indices assuming a sequence of the specified length. |
| 105 | Out of bounds indices are clipped in a manner consistent with the handling |
| 106 | of normal slices. |
| 107 | |
| 108 | Return the length of the slice. Always successful. Doesn't call Python |
| 109 | code. |
| 110 | |
| 111 | .. versionadded:: 3.6.1 |
Michael Seifert | 0dc5c31 | 2017-04-14 21:18:35 +0200 | [diff] [blame] | 112 | |
| 113 | |
| 114 | Ellipsis Object |
| 115 | --------------- |
| 116 | |
| 117 | |
| 118 | .. c:var:: PyObject *Py_Ellipsis |
| 119 | |
| 120 | The Python ``Ellipsis`` object. This object has no methods. It needs to be |
| 121 | treated just like any other object with respect to reference counts. Like |
| 122 | :c:data:`Py_None` it is a singleton object. |