Coverage Report

Created: 2026-12-01 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Objects/sliceobject.c
Line
Count
Source
1
/*
2
Written by Jim Hugunin and Chris Chase.
3
4
This includes both the singular ellipsis object and slice objects.
5
6
Guido, feel free to do whatever you want in the way of copyrights
7
for this file.
8
*/
9
10
/*
11
Py_Ellipsis encodes the '...' rubber index token. It is similar to
12
the Py_NoneStruct in that there is no way to create other objects of
13
this type and there is exactly one in existence.
14
*/
15
16
#include "Python.h"
17
#include "pycore_abstract.h"      // _PyIndex_Check()
18
#include "pycore_freelist.h"      // _Py_FREELIST_FREE(), _Py_FREELIST_POP()
19
#include "pycore_long.h"          // _PyLong_GetZero()
20
#include "pycore_modsupport.h"    // _PyArg_NoKeywords()
21
#include "pycore_object.h"        // _PyObject_GC_TRACK()
22
#include "pycore_sliceobject.h"   // _PyBuildSlice_ConsumeRefs
23
24
#include "clinic/sliceobject.c.h"
25
26
/*[clinic input]
27
class slice "PySliceObject *" "&PySlice_Type"
28
[clinic start generated code]*/
29
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=857b514967456ef4]*/
30
31
32
54.3M
#define _PySlice_CAST(op) _Py_CAST(PySliceObject*, (op))
33
34
35
static PyObject *
36
ellipsis_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
37
0
{
38
0
    if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
39
0
        PyErr_SetString(PyExc_TypeError, "EllipsisType takes no arguments");
40
0
        return NULL;
41
0
    }
42
0
    return Py_Ellipsis;
43
0
}
44
45
static void
46
ellipsis_dealloc(PyObject *ellipsis)
47
0
{
48
    /* This should never get called, but we also don't want to SEGV if
49
     * we accidentally decref Ellipsis out of existence. Instead,
50
     * since Ellipsis is an immortal object, re-set the reference count.
51
     */
52
0
    _Py_SetImmortal(ellipsis);
53
0
}
54
55
static PyObject *
56
ellipsis_repr(PyObject *op)
57
0
{
58
0
    return PyUnicode_FromString("Ellipsis");
59
0
}
60
61
static PyObject *
62
ellipsis_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
63
0
{
64
0
    return PyUnicode_FromString("Ellipsis");
65
0
}
66
67
static PyMethodDef ellipsis_methods[] = {
68
    {"__reduce__", ellipsis_reduce, METH_NOARGS, NULL},
69
    {NULL, NULL}
70
};
71
72
PyDoc_STRVAR(ellipsis_doc,
73
"ellipsis()\n"
74
"--\n\n"
75
"The type of the Ellipsis singleton.");
76
77
PyTypeObject PyEllipsis_Type = {
78
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
79
    "ellipsis",                         /* tp_name */
80
    0,                                  /* tp_basicsize */
81
    0,                                  /* tp_itemsize */
82
    ellipsis_dealloc,                   /* tp_dealloc */
83
    0,                                  /* tp_vectorcall_offset */
84
    0,                                  /* tp_getattr */
85
    0,                                  /* tp_setattr */
86
    0,                                  /* tp_as_async */
87
    ellipsis_repr,                      /* tp_repr */
88
    0,                                  /* tp_as_number */
89
    0,                                  /* tp_as_sequence */
90
    0,                                  /* tp_as_mapping */
91
    0,                                  /* tp_hash */
92
    0,                                  /* tp_call */
93
    0,                                  /* tp_str */
94
    PyObject_GenericGetAttr,            /* tp_getattro */
95
    0,                                  /* tp_setattro */
96
    0,                                  /* tp_as_buffer */
97
    Py_TPFLAGS_DEFAULT,                 /* tp_flags */
98
    ellipsis_doc,                       /* tp_doc */
99
    0,                                  /* tp_traverse */
100
    0,                                  /* tp_clear */
101
    0,                                  /* tp_richcompare */
102
    0,                                  /* tp_weaklistoffset */
103
    0,                                  /* tp_iter */
104
    0,                                  /* tp_iternext */
105
    ellipsis_methods,                   /* tp_methods */
106
    0,                                  /* tp_members */
107
    0,                                  /* tp_getset */
108
    0,                                  /* tp_base */
109
    0,                                  /* tp_dict */
110
    0,                                  /* tp_descr_get */
111
    0,                                  /* tp_descr_set */
112
    0,                                  /* tp_dictoffset */
113
    0,                                  /* tp_init */
114
    0,                                  /* tp_alloc */
115
    ellipsis_new,                       /* tp_new */
116
};
117
118
PyObject _Py_EllipsisObject = _PyObject_HEAD_INIT(&PyEllipsis_Type);
119
120
121
/* Slice object implementation */
122
123
/* start, stop, and step are python objects with None indicating no
124
   index is present.
125
*/
126
127
PyObject *
128
_PyBuildSlice_ConsumeRefs(PyObject *start, PyObject *stop, PyObject *step)
129
54.2M
{
130
54.2M
    assert(start != NULL && stop != NULL && step != NULL);
131
54.2M
    PySliceObject *obj = _Py_FREELIST_POP(PySliceObject, slices);
132
54.2M
    if (obj == NULL) {
133
3.53k
        obj = PyObject_GC_New(PySliceObject, &PySlice_Type);
134
3.53k
        if (obj == NULL) {
135
0
            goto error;
136
0
        }
137
3.53k
    }
138
139
54.2M
    obj->start = start;
140
54.2M
    obj->stop = stop;
141
54.2M
    obj->step = step;
142
143
54.2M
    _PyObject_GC_TRACK(obj);
144
54.2M
    return (PyObject *)obj;
145
0
error:
146
0
    Py_DECREF(start);
147
0
    Py_DECREF(stop);
148
0
    Py_DECREF(step);
149
0
    return NULL;
150
54.2M
}
151
152
PyObject *
153
PySlice_New(PyObject *start, PyObject *stop, PyObject *step)
154
53.9M
{
155
53.9M
    if (step == NULL) {
156
51.1M
        step = Py_None;
157
51.1M
    }
158
53.9M
    if (start == NULL) {
159
31
        start = Py_None;
160
31
    }
161
53.9M
    if (stop == NULL) {
162
21
        stop = Py_None;
163
21
    }
164
53.9M
    return _PyBuildSlice_ConsumeRefs(Py_NewRef(start),
165
53.9M
                                              Py_NewRef(stop), Py_NewRef(step));
166
53.9M
}
167
168
PyObject *
169
_PySlice_FromIndices(Py_ssize_t istart, Py_ssize_t istop)
170
164k
{
171
164k
    PyObject *start, *end, *slice;
172
164k
    start = PyLong_FromSsize_t(istart);
173
164k
    if (!start)
174
0
        return NULL;
175
164k
    end = PyLong_FromSsize_t(istop);
176
164k
    if (!end) {
177
0
        Py_DECREF(start);
178
0
        return NULL;
179
0
    }
180
181
164k
    slice = _PyBuildSlice_ConsumeRefs(start, end, Py_None);
182
164k
    return slice;
183
164k
}
184
185
int
186
PySlice_GetIndices(PyObject *_r, Py_ssize_t length,
187
                   Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
188
0
{
189
0
    PySliceObject *r = (PySliceObject*)_r;
190
    /* XXX support long ints */
191
0
    if (r->step == Py_None) {
192
0
        *step = 1;
193
0
    } else {
194
0
        if (!PyLong_Check(r->step)) return -1;
195
0
        *step = PyLong_AsSsize_t(r->step);
196
0
    }
197
0
    if (r->start == Py_None) {
198
0
        *start = *step < 0 ? length-1 : 0;
199
0
    } else {
200
0
        if (!PyLong_Check(r->start)) return -1;
201
0
        *start = PyLong_AsSsize_t(r->start);
202
0
        if (*start < 0) *start += length;
203
0
    }
204
0
    if (r->stop == Py_None) {
205
0
        *stop = *step < 0 ? -1 : length;
206
0
    } else {
207
0
        if (!PyLong_Check(r->stop)) return -1;
208
0
        *stop = PyLong_AsSsize_t(r->stop);
209
0
        if (*stop < 0) *stop += length;
210
0
    }
211
0
    if (*stop > length) return -1;
212
0
    if (*start >= length) return -1;
213
0
    if (*step == 0) return -1;
214
0
    return 0;
215
0
}
216
217
int
218
PySlice_Unpack(PyObject *_r,
219
               Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
220
85.0M
{
221
85.0M
    PySliceObject *r = (PySliceObject*)_r;
222
    /* this is harder to get right than you might think */
223
224
85.0M
    static_assert(PY_SSIZE_T_MIN + 1 <= -PY_SSIZE_T_MAX,
225
85.0M
                  "-PY_SSIZE_T_MAX < PY_SSIZE_T_MIN + 1");
226
227
85.0M
    if (r->step == Py_None) {
228
84.9M
        *step = 1;
229
84.9M
    }
230
61.6k
    else {
231
61.6k
        if (!_PyEval_SliceIndex(r->step, step)) return -1;
232
61.6k
        if (*step == 0) {
233
0
            PyErr_SetString(PyExc_ValueError,
234
0
                            "slice step cannot be zero");
235
0
            return -1;
236
0
        }
237
        /* Here *step might be -PY_SSIZE_T_MAX-1; in this case we replace it
238
         * with -PY_SSIZE_T_MAX.  This doesn't affect the semantics, and it
239
         * guards against later undefined behaviour resulting from code that
240
         * does "step = -step" as part of a slice reversal.
241
         */
242
61.6k
        if (*step < -PY_SSIZE_T_MAX)
243
0
            *step = -PY_SSIZE_T_MAX;
244
61.6k
    }
245
246
85.0M
    if (r->start == Py_None) {
247
15.8M
        *start = *step < 0 ? PY_SSIZE_T_MAX : 0;
248
15.8M
    }
249
69.2M
    else {
250
69.2M
        if (!_PyEval_SliceIndex(r->start, start)) return -1;
251
69.2M
    }
252
253
85.0M
    if (r->stop == Py_None) {
254
18.6M
        *stop = *step < 0 ? PY_SSIZE_T_MIN : PY_SSIZE_T_MAX;
255
18.6M
    }
256
66.3M
    else {
257
66.3M
        if (!_PyEval_SliceIndex(r->stop, stop)) return -1;
258
66.3M
    }
259
260
85.0M
    return 0;
261
85.0M
}
262
263
Py_ssize_t
264
PySlice_AdjustIndices(Py_ssize_t length,
265
                      Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step)
266
135M
{
267
    /* this is harder to get right than you might think */
268
269
135M
    assert(step != 0);
270
135M
    assert(step >= -PY_SSIZE_T_MAX);
271
272
135M
    if (*start < 0) {
273
1.53M
        *start += length;
274
1.53M
        if (*start < 0) {
275
0
            *start = (step < 0) ? -1 : 0;
276
0
        }
277
1.53M
    }
278
133M
    else if (*start >= length) {
279
10.8M
        *start = (step < 0) ? length - 1 : length;
280
10.8M
    }
281
282
135M
    if (*stop < 0) {
283
12.1M
        *stop += length;
284
12.1M
        if (*stop < 0) {
285
93.0k
            *stop = (step < 0) ? -1 : 0;
286
93.0k
        }
287
12.1M
    }
288
123M
    else if (*stop >= length) {
289
33.7M
        *stop = (step < 0) ? length - 1 : length;
290
33.7M
    }
291
292
135M
    if (step < 0) {
293
61.5k
        if (*stop < *start) {
294
61.5k
            return (*start - *stop - 1) / (-step) + 1;
295
61.5k
        }
296
61.5k
    }
297
135M
    else {
298
135M
        if (*start < *stop) {
299
116M
            return (*stop - *start - 1) / step + 1;
300
116M
        }
301
135M
    }
302
18.7M
    return 0;
303
135M
}
304
305
#undef PySlice_GetIndicesEx
306
307
int
308
PySlice_GetIndicesEx(PyObject *_r, Py_ssize_t length,
309
                     Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step,
310
                     Py_ssize_t *slicelength)
311
0
{
312
0
    if (PySlice_Unpack(_r, start, stop, step) < 0)
313
0
        return -1;
314
0
    *slicelength = PySlice_AdjustIndices(length, start, stop, *step);
315
0
    return 0;
316
0
}
317
318
/*[clinic input]
319
@classmethod
320
@vectorcall
321
slice.__new__ as slice_new
322
323
    start: object
324
    stop: object(c_default="NULL") = None
325
    step: object(c_default="NULL") = None
326
    /
327
[clinic start generated code]*/
328
329
static PyObject *
330
slice_new_impl(PyTypeObject *type, PyObject *start, PyObject *stop,
331
               PyObject *step)
332
/*[clinic end generated code: output=35a6e86397efb677 input=cf5c572a0d2caa22]*/
333
16
{
334
    /* This swapping of stop and start is to maintain similarity with
335
       range(). */
336
16
    if (stop == NULL) {
337
8
        stop = start;
338
8
        start = NULL;
339
8
    }
340
16
    return PySlice_New(start, stop, step);
341
16
}
342
343
PyDoc_STRVAR(slice_doc,
344
"slice(stop)\n\
345
slice(start, stop[, step])\n\
346
\n\
347
Create a slice object.\n\
348
\n\
349
This is used for extended slicing (e.g. a[0:10:2]).");
350
351
static void
352
slice_dealloc(PyObject *op)
353
54.2M
{
354
54.2M
    PySliceObject *r = _PySlice_CAST(op);
355
54.2M
    PyObject_GC_UnTrack(r);
356
54.2M
    Py_DECREF(r->step);
357
54.2M
    Py_DECREF(r->start);
358
54.2M
    Py_DECREF(r->stop);
359
54.2M
    _Py_FREELIST_FREE(slices, r, PyObject_GC_Del);
360
54.2M
}
361
362
static PyObject *
363
slice_repr(PyObject *op)
364
0
{
365
0
    PySliceObject *r = _PySlice_CAST(op);
366
0
    return PyUnicode_FromFormat("slice(%R, %R, %R)",
367
0
                                r->start, r->stop, r->step);
368
0
}
369
370
static PyMemberDef slice_members[] = {
371
    {"start", _Py_T_OBJECT, offsetof(PySliceObject, start), Py_READONLY},
372
    {"stop", _Py_T_OBJECT, offsetof(PySliceObject, stop), Py_READONLY},
373
    {"step", _Py_T_OBJECT, offsetof(PySliceObject, step), Py_READONLY},
374
    {0}
375
};
376
377
/* Helper function to convert a slice argument to a PyLong, and raise TypeError
378
   with a suitable message on failure. */
379
380
static PyObject*
381
evaluate_slice_index(PyObject *v)
382
2.75M
{
383
2.75M
    if (_PyIndex_Check(v)) {
384
2.75M
        return PyNumber_Index(v);
385
2.75M
    }
386
0
    else {
387
0
        PyErr_SetString(PyExc_TypeError,
388
0
                        "slice indices must be integers or "
389
0
                        "None or have an __index__ method");
390
0
        return NULL;
391
0
    }
392
2.75M
}
393
394
/* Compute slice indices given a slice and length.  Return -1 on failure.  Used
395
   by slice.indices and rangeobject slicing.  Assumes that `len` is a
396
   nonnegative instance of PyLong. */
397
398
int
399
_PySlice_GetLongIndices(PySliceObject *self, PyObject *length,
400
                        PyObject **start_ptr, PyObject **stop_ptr,
401
                        PyObject **step_ptr)
402
2.75M
{
403
2.75M
    PyObject *start=NULL, *stop=NULL, *step=NULL;
404
2.75M
    PyObject *upper=NULL, *lower=NULL;
405
2.75M
    int step_is_negative, cmp_result;
406
407
    /* Convert step to an integer; raise for zero step. */
408
2.75M
    if (self->step == Py_None) {
409
0
        step = _PyLong_GetOne();
410
0
        step_is_negative = 0;
411
0
    }
412
2.75M
    else {
413
2.75M
        step = evaluate_slice_index(self->step);
414
2.75M
        if (step == NULL) {
415
0
            goto error;
416
0
        }
417
2.75M
        assert(PyLong_Check(step));
418
419
2.75M
        int step_sign;
420
2.75M
        (void)PyLong_GetSign(step, &step_sign);
421
2.75M
        if (step_sign == 0) {
422
0
            PyErr_SetString(PyExc_ValueError,
423
0
                            "slice step cannot be zero");
424
0
            goto error;
425
0
        }
426
2.75M
        step_is_negative = step_sign < 0;
427
2.75M
    }
428
429
    /* Find lower and upper bounds for start and stop. */
430
2.75M
    if (step_is_negative) {
431
2.75M
        lower = PyLong_FromLong(-1L);
432
2.75M
        if (lower == NULL)
433
0
            goto error;
434
435
2.75M
        upper = PyNumber_Add(length, lower);
436
2.75M
        if (upper == NULL)
437
0
            goto error;
438
2.75M
    }
439
0
    else {
440
0
        lower = _PyLong_GetZero();
441
0
        upper = Py_NewRef(length);
442
0
    }
443
444
    /* Compute start. */
445
2.75M
    if (self->start == Py_None) {
446
2.75M
        start = Py_NewRef(step_is_negative ? upper : lower);
447
2.75M
    }
448
0
    else {
449
0
        start = evaluate_slice_index(self->start);
450
0
        if (start == NULL)
451
0
            goto error;
452
453
0
        if (_PyLong_IsNegative((PyLongObject *)start)) {
454
            /* start += length */
455
0
            PyObject *tmp = PyNumber_Add(start, length);
456
0
            Py_SETREF(start, tmp);
457
0
            if (start == NULL)
458
0
                goto error;
459
460
0
            cmp_result = PyObject_RichCompareBool(start, lower, Py_LT);
461
0
            if (cmp_result < 0)
462
0
                goto error;
463
0
            if (cmp_result) {
464
0
                Py_SETREF(start, Py_NewRef(lower));
465
0
            }
466
0
        }
467
0
        else {
468
0
            cmp_result = PyObject_RichCompareBool(start, upper, Py_GT);
469
0
            if (cmp_result < 0)
470
0
                goto error;
471
0
            if (cmp_result) {
472
0
                Py_SETREF(start, Py_NewRef(upper));
473
0
            }
474
0
        }
475
0
    }
476
477
    /* Compute stop. */
478
2.75M
    if (self->stop == Py_None) {
479
2.75M
        stop = Py_NewRef(step_is_negative ? lower : upper);
480
2.75M
    }
481
0
    else {
482
0
        stop = evaluate_slice_index(self->stop);
483
0
        if (stop == NULL)
484
0
            goto error;
485
486
0
        if (_PyLong_IsNegative((PyLongObject *)stop)) {
487
            /* stop += length */
488
0
            PyObject *tmp = PyNumber_Add(stop, length);
489
0
            Py_SETREF(stop, tmp);
490
0
            if (stop == NULL)
491
0
                goto error;
492
493
0
            cmp_result = PyObject_RichCompareBool(stop, lower, Py_LT);
494
0
            if (cmp_result < 0)
495
0
                goto error;
496
0
            if (cmp_result) {
497
0
                Py_SETREF(stop, Py_NewRef(lower));
498
0
            }
499
0
        }
500
0
        else {
501
0
            cmp_result = PyObject_RichCompareBool(stop, upper, Py_GT);
502
0
            if (cmp_result < 0)
503
0
                goto error;
504
0
            if (cmp_result) {
505
0
                Py_SETREF(stop, Py_NewRef(upper));
506
0
            }
507
0
        }
508
0
    }
509
510
2.75M
    *start_ptr = start;
511
2.75M
    *stop_ptr = stop;
512
2.75M
    *step_ptr = step;
513
2.75M
    Py_DECREF(upper);
514
2.75M
    Py_DECREF(lower);
515
2.75M
    return 0;
516
517
0
  error:
518
0
    *start_ptr = *stop_ptr = *step_ptr = NULL;
519
0
    Py_XDECREF(start);
520
0
    Py_XDECREF(stop);
521
0
    Py_XDECREF(step);
522
0
    Py_XDECREF(upper);
523
0
    Py_XDECREF(lower);
524
0
    return -1;
525
2.75M
}
526
527
/* Implementation of slice.indices. */
528
529
static PyObject*
530
slice_indices(PyObject *op, PyObject* len)
531
0
{
532
0
    PySliceObject *self = _PySlice_CAST(op);
533
0
    PyObject *start, *stop, *step;
534
0
    PyObject *length;
535
0
    int error;
536
537
    /* Convert length to an integer if necessary; raise for negative length. */
538
0
    length = PyNumber_Index(len);
539
0
    if (length == NULL)
540
0
        return NULL;
541
542
0
    if (_PyLong_IsNegative((PyLongObject *)length)) {
543
0
        PyErr_SetString(PyExc_ValueError,
544
0
                        "length should not be negative");
545
0
        Py_DECREF(length);
546
0
        return NULL;
547
0
    }
548
549
0
    error = _PySlice_GetLongIndices(self, length, &start, &stop, &step);
550
0
    Py_DECREF(length);
551
0
    if (error == -1)
552
0
        return NULL;
553
0
    else
554
0
        return Py_BuildValue("(NNN)", start, stop, step);
555
0
}
556
557
PyDoc_STRVAR(slice_indices_doc,
558
"S.indices(len) -> (start, stop, stride)\n\
559
\n\
560
Assuming a sequence of length len, calculate the start and stop\n\
561
indices, and the stride length of the extended slice described by\n\
562
S. Out of bounds indices are clipped in a manner consistent with the\n\
563
handling of normal slices.");
564
565
static PyObject *
566
slice_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
567
0
{
568
0
    PySliceObject *self = _PySlice_CAST(op);
569
0
    return Py_BuildValue("O(OOO)", Py_TYPE(self), self->start, self->stop, self->step);
570
0
}
571
572
PyDoc_STRVAR(reduce_doc, "Return state information for pickling.");
573
574
static PyMethodDef slice_methods[] = {
575
    {"indices", slice_indices, METH_O, slice_indices_doc},
576
    {"__reduce__", slice_reduce, METH_NOARGS, reduce_doc},
577
    {"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS,
578
     "slices are generic over the types of their start, end, and step values"},
579
    {NULL, NULL}
580
};
581
582
static PyObject *
583
slice_richcompare(PyObject *v, PyObject *w, int op)
584
81
{
585
81
    if (!PySlice_Check(v) || !PySlice_Check(w))
586
0
        Py_RETURN_NOTIMPLEMENTED;
587
588
81
    if (v == w) {
589
0
        PyObject *res;
590
        /* XXX Do we really need this shortcut?
591
           There's a unit test for it, but is that fair? */
592
0
        switch (op) {
593
0
        case Py_EQ:
594
0
        case Py_LE:
595
0
        case Py_GE:
596
0
            res = Py_True;
597
0
            break;
598
0
        default:
599
0
            res = Py_False;
600
0
            break;
601
0
        }
602
0
        return Py_NewRef(res);
603
0
    }
604
605
606
81
    PyObject *t1 = PyTuple_Pack(3,
607
81
                                ((PySliceObject *)v)->start,
608
81
                                ((PySliceObject *)v)->stop,
609
81
                                ((PySliceObject *)v)->step);
610
81
    if (t1 == NULL) {
611
0
        return NULL;
612
0
    }
613
614
81
    PyObject *t2 = PyTuple_Pack(3,
615
81
                                ((PySliceObject *)w)->start,
616
81
                                ((PySliceObject *)w)->stop,
617
81
                                ((PySliceObject *)w)->step);
618
81
    if (t2 == NULL) {
619
0
        Py_DECREF(t1);
620
0
        return NULL;
621
0
    }
622
623
81
    PyObject *res = PyObject_RichCompare(t1, t2, op);
624
81
    Py_DECREF(t1);
625
81
    Py_DECREF(t2);
626
81
    return res;
627
81
}
628
629
static int
630
slice_traverse(PyObject *op, visitproc visit, void *arg)
631
147k
{
632
147k
    PySliceObject *v = _PySlice_CAST(op);
633
147k
    Py_VISIT(v->start);
634
147k
    Py_VISIT(v->stop);
635
147k
    Py_VISIT(v->step);
636
147k
    return 0;
637
147k
}
638
639
/* code based on tuplehash() of Objects/tupleobject.c */
640
#if SIZEOF_PY_UHASH_T > 4
641
846
#define _PyHASH_XXPRIME_1 ((Py_uhash_t)11400714785074694791ULL)
642
846
#define _PyHASH_XXPRIME_2 ((Py_uhash_t)14029467366897019727ULL)
643
282
#define _PyHASH_XXPRIME_5 ((Py_uhash_t)2870177450012600261ULL)
644
846
#define _PyHASH_XXROTATE(x) ((x << 31) | (x >> 33))  /* Rotate left 31 bits */
645
#else
646
#define _PyHASH_XXPRIME_1 ((Py_uhash_t)2654435761UL)
647
#define _PyHASH_XXPRIME_2 ((Py_uhash_t)2246822519UL)
648
#define _PyHASH_XXPRIME_5 ((Py_uhash_t)374761393UL)
649
#define _PyHASH_XXROTATE(x) ((x << 13) | (x >> 19))  /* Rotate left 13 bits */
650
#endif
651
652
static Py_hash_t
653
slice_hash(PyObject *op)
654
282
{
655
282
    PySliceObject *v = _PySlice_CAST(op);
656
282
    Py_uhash_t acc = _PyHASH_XXPRIME_5;
657
846
#define _PyHASH_SLICE_PART(com) { \
658
846
    Py_uhash_t lane = PyObject_Hash(v->com); \
659
846
    if(lane == (Py_uhash_t)-1) { \
660
0
        return -1; \
661
0
    } \
662
846
    acc += lane * _PyHASH_XXPRIME_2; \
663
846
    acc = _PyHASH_XXROTATE(acc); \
664
846
    acc *= _PyHASH_XXPRIME_1; \
665
846
}
666
282
    _PyHASH_SLICE_PART(start);
667
282
    _PyHASH_SLICE_PART(stop);
668
282
    _PyHASH_SLICE_PART(step);
669
282
#undef _PyHASH_SLICE_PART
670
282
    if(acc == (Py_uhash_t)-1) {
671
0
        return 1546275796;
672
0
    }
673
282
    return acc;
674
282
}
675
676
PyTypeObject PySlice_Type = {
677
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
678
    "slice",                    /* Name of this type */
679
    sizeof(PySliceObject),      /* Basic object size */
680
    0,                          /* Item size for varobject */
681
    slice_dealloc,                              /* tp_dealloc */
682
    0,                                          /* tp_vectorcall_offset */
683
    0,                                          /* tp_getattr */
684
    0,                                          /* tp_setattr */
685
    0,                                          /* tp_as_async */
686
    slice_repr,                                 /* tp_repr */
687
    0,                                          /* tp_as_number */
688
    0,                                          /* tp_as_sequence */
689
    0,                                          /* tp_as_mapping */
690
    slice_hash,                                 /* tp_hash */
691
    0,                                          /* tp_call */
692
    0,                                          /* tp_str */
693
    PyObject_GenericGetAttr,                    /* tp_getattro */
694
    0,                                          /* tp_setattro */
695
    0,                                          /* tp_as_buffer */
696
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,    /* tp_flags */
697
    slice_doc,                                  /* tp_doc */
698
    slice_traverse,                             /* tp_traverse */
699
    0,                                          /* tp_clear */
700
    slice_richcompare,                          /* tp_richcompare */
701
    0,                                          /* tp_weaklistoffset */
702
    0,                                          /* tp_iter */
703
    0,                                          /* tp_iternext */
704
    slice_methods,                              /* tp_methods */
705
    slice_members,                              /* tp_members */
706
    0,                                          /* tp_getset */
707
    0,                                          /* tp_base */
708
    0,                                          /* tp_dict */
709
    0,                                          /* tp_descr_get */
710
    0,                                          /* tp_descr_set */
711
    0,                                          /* tp_dictoffset */
712
    0,                                          /* tp_init */
713
    0,                                          /* tp_alloc */
714
    slice_new,                                  /* tp_new */
715
    .tp_vectorcall = slice_vectorcall,
716
};