summaryrefslogtreecommitdiffstats
path: root/Objects/sliceobject.c
diff options
context:
space:
mode:
Diffstat (limited to 'Objects/sliceobject.c')
-rw-r--r--Objects/sliceobject.c404
1 files changed, 78 insertions, 326 deletions
diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c
index e884a58..dc18211 100644
--- a/Objects/sliceobject.c
+++ b/Objects/sliceobject.c
@@ -14,49 +14,24 @@ this type and there is exactly one in existence.
*/
#include "Python.h"
-#include "pycore_object.h"
-#include "pycore_pymem.h"
-#include "pycore_pystate.h"
#include "structmember.h"
static PyObject *
-ellipsis_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
-{
- if (PyTuple_GET_SIZE(args) || (kwargs && PyDict_GET_SIZE(kwargs))) {
- PyErr_SetString(PyExc_TypeError, "EllipsisType takes no arguments");
- return NULL;
- }
- Py_INCREF(Py_Ellipsis);
- return Py_Ellipsis;
-}
-
-static PyObject *
ellipsis_repr(PyObject *op)
{
- return PyUnicode_FromString("Ellipsis");
-}
-
-static PyObject *
-ellipsis_reduce(PyObject *op, PyObject *Py_UNUSED(ignored))
-{
- return PyUnicode_FromString("Ellipsis");
+ return PyString_FromString("Ellipsis");
}
-static PyMethodDef ellipsis_methods[] = {
- {"__reduce__", ellipsis_reduce, METH_NOARGS, NULL},
- {NULL, NULL}
-};
-
PyTypeObject PyEllipsis_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"ellipsis", /* tp_name */
0, /* tp_basicsize */
0, /* tp_itemsize */
0, /*never called*/ /* tp_dealloc */
- 0, /* tp_vectorcall_offset */
+ 0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
- 0, /* tp_as_async */
+ 0, /* tp_compare */
ellipsis_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
@@ -68,24 +43,6 @@ PyTypeObject PyEllipsis_Type = {
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
- 0, /* tp_doc */
- 0, /* tp_traverse */
- 0, /* tp_clear */
- 0, /* tp_richcompare */
- 0, /* tp_weaklistoffset */
- 0, /* tp_iter */
- 0, /* tp_iternext */
- ellipsis_methods, /* tp_methods */
- 0, /* tp_members */
- 0, /* tp_getset */
- 0, /* tp_base */
- 0, /* tp_dict */
- 0, /* tp_descr_get */
- 0, /* tp_descr_set */
- 0, /* tp_dictoffset */
- 0, /* tp_init */
- 0, /* tp_alloc */
- ellipsis_new, /* tp_new */
};
PyObject _Py_EllipsisObject = {
@@ -94,39 +51,19 @@ PyObject _Py_EllipsisObject = {
};
-/* Slice object implementation */
-
-/* Using a cache is very effective since typically only a single slice is
- * created and then deleted again
- */
-static PySliceObject *slice_cache = NULL;
-
-void _PySlice_Fini(void)
-{
- PySliceObject *obj = slice_cache;
- if (obj != NULL) {
- slice_cache = NULL;
- PyObject_GC_Del(obj);
- }
-}
+/* Slice object implementation
-/* start, stop, and step are python objects with None indicating no
+ start, stop, and step are python objects with None indicating no
index is present.
*/
PyObject *
PySlice_New(PyObject *start, PyObject *stop, PyObject *step)
{
- PySliceObject *obj;
- if (slice_cache != NULL) {
- obj = slice_cache;
- slice_cache = NULL;
- _Py_NewReference((PyObject *)obj);
- } else {
- obj = PyObject_GC_New(PySliceObject, &PySlice_Type);
- if (obj == NULL)
- return NULL;
- }
+ PySliceObject *obj = PyObject_GC_New(PySliceObject, &PySlice_Type);
+
+ if (obj == NULL)
+ return NULL;
if (step == NULL) step = Py_None;
Py_INCREF(step);
@@ -147,10 +84,10 @@ PyObject *
_PySlice_FromIndices(Py_ssize_t istart, Py_ssize_t istop)
{
PyObject *start, *end, *slice;
- start = PyLong_FromSsize_t(istart);
+ start = PyInt_FromSsize_t(istart);
if (!start)
return NULL;
- end = PyLong_FromSsize_t(istop);
+ end = PyInt_FromSsize_t(istop);
if (!end) {
Py_DECREF(start);
return NULL;
@@ -163,29 +100,28 @@ _PySlice_FromIndices(Py_ssize_t istart, Py_ssize_t istop)
}
int
-PySlice_GetIndices(PyObject *_r, Py_ssize_t length,
+PySlice_GetIndices(PySliceObject *r, Py_ssize_t length,
Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
{
- PySliceObject *r = (PySliceObject*)_r;
/* XXX support long ints */
if (r->step == Py_None) {
*step = 1;
} else {
- if (!PyLong_Check(r->step)) return -1;
- *step = PyLong_AsSsize_t(r->step);
+ if (!_PyAnyInt_Check(r->step)) return -1;
+ *step = PyInt_AsSsize_t(r->step);
}
if (r->start == Py_None) {
*start = *step < 0 ? length-1 : 0;
} else {
- if (!PyLong_Check(r->start)) return -1;
- *start = PyLong_AsSsize_t(r->start);
+ if (!_PyAnyInt_Check(r->start)) return -1;
+ *start = PyInt_AsSsize_t(r->start);
if (*start < 0) *start += length;
}
if (r->stop == Py_None) {
*stop = *step < 0 ? -1 : length;
} else {
- if (!PyLong_Check(r->stop)) return -1;
- *stop = PyLong_AsSsize_t(r->stop);
+ if (!_PyAnyInt_Check(r->stop)) return -1;
+ *stop = PyInt_AsSsize_t(r->stop);
if (*stop < 0) *stop += length;
}
if (*stop > length) return -1;
@@ -195,13 +131,13 @@ PySlice_GetIndices(PyObject *_r, Py_ssize_t length,
}
int
-PySlice_Unpack(PyObject *_r,
- Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
+_PySlice_Unpack(PyObject *_r,
+ Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
{
- PySliceObject *r = (PySliceObject*)_r;
+ PySliceObject *r = (PySliceObject *)_r;
/* this is harder to get right than you might think */
- Py_BUILD_ASSERT(PY_SSIZE_T_MIN + 1 <= -PY_SSIZE_T_MAX);
+ assert(PY_SSIZE_T_MIN + 1 <= -PY_SSIZE_T_MAX);
if (r->step == Py_None) {
*step = 1;
@@ -240,8 +176,8 @@ PySlice_Unpack(PyObject *_r,
}
Py_ssize_t
-PySlice_AdjustIndices(Py_ssize_t length,
- Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step)
+_PySlice_AdjustIndices(Py_ssize_t length,
+ Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t step)
{
/* this is harder to get right than you might think */
@@ -284,13 +220,13 @@ PySlice_AdjustIndices(Py_ssize_t length,
#undef PySlice_GetIndicesEx
int
-PySlice_GetIndicesEx(PyObject *_r, Py_ssize_t length,
+PySlice_GetIndicesEx(PySliceObject *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((PyObject *)r, start, stop, step) < 0)
return -1;
- *slicelength = PySlice_AdjustIndices(length, start, stop, *step);
+ *slicelength = _PySlice_AdjustIndices(length, start, stop, *step);
return 0;
}
@@ -301,7 +237,7 @@ slice_new(PyTypeObject *type, PyObject *args, PyObject *kw)
start = stop = step = NULL;
- if (!_PyArg_NoKeywords("slice", kw))
+ if (!_PyArg_NoKeywords("slice()", kw))
return NULL;
if (!PyArg_UnpackTuple(args, "slice", 1, 3, &start, &stop, &step))
@@ -329,16 +265,24 @@ slice_dealloc(PySliceObject *r)
Py_DECREF(r->step);
Py_DECREF(r->start);
Py_DECREF(r->stop);
- if (slice_cache == NULL)
- slice_cache = r;
- else
- PyObject_GC_Del(r);
+ PyObject_GC_Del(r);
}
static PyObject *
slice_repr(PySliceObject *r)
{
- return PyUnicode_FromFormat("slice(%R, %R, %R)", r->start, r->stop, r->step);
+ PyObject *s, *comma;
+
+ s = PyString_FromString("slice(");
+ comma = PyString_FromString(", ");
+ PyString_ConcatAndDel(&s, PyObject_Repr(r->start));
+ PyString_Concat(&s, comma);
+ PyString_ConcatAndDel(&s, PyObject_Repr(r->stop));
+ PyString_Concat(&s, comma);
+ PyString_ConcatAndDel(&s, PyObject_Repr(r->step));
+ PyString_ConcatAndDel(&s, PyString_FromString(")"));
+ Py_DECREF(comma);
+ return s;
}
static PyMemberDef slice_members[] = {
@@ -348,195 +292,23 @@ static PyMemberDef slice_members[] = {
{0}
};
-/* Helper function to convert a slice argument to a PyLong, and raise TypeError
- with a suitable message on failure. */
-
-static PyObject*
-evaluate_slice_index(PyObject *v)
-{
- if (PyIndex_Check(v)) {
- return PyNumber_Index(v);
- }
- else {
- PyErr_SetString(PyExc_TypeError,
- "slice indices must be integers or "
- "None or have an __index__ method");
- return NULL;
- }
-}
-
-/* Compute slice indices given a slice and length. Return -1 on failure. Used
- by slice.indices and rangeobject slicing. Assumes that `len` is a
- nonnegative instance of PyLong. */
-
-int
-_PySlice_GetLongIndices(PySliceObject *self, PyObject *length,
- PyObject **start_ptr, PyObject **stop_ptr,
- PyObject **step_ptr)
-{
- PyObject *start=NULL, *stop=NULL, *step=NULL;
- PyObject *upper=NULL, *lower=NULL;
- int step_is_negative, cmp_result;
-
- /* Convert step to an integer; raise for zero step. */
- if (self->step == Py_None) {
- step = _PyLong_One;
- Py_INCREF(step);
- step_is_negative = 0;
- }
- else {
- int step_sign;
- step = evaluate_slice_index(self->step);
- if (step == NULL)
- goto error;
- step_sign = _PyLong_Sign(step);
- if (step_sign == 0) {
- PyErr_SetString(PyExc_ValueError,
- "slice step cannot be zero");
- goto error;
- }
- step_is_negative = step_sign < 0;
- }
-
- /* Find lower and upper bounds for start and stop. */
- if (step_is_negative) {
- lower = PyLong_FromLong(-1L);
- if (lower == NULL)
- goto error;
-
- upper = PyNumber_Add(length, lower);
- if (upper == NULL)
- goto error;
- }
- else {
- lower = _PyLong_Zero;
- Py_INCREF(lower);
- upper = length;
- Py_INCREF(upper);
- }
-
- /* Compute start. */
- if (self->start == Py_None) {
- start = step_is_negative ? upper : lower;
- Py_INCREF(start);
- }
- else {
- start = evaluate_slice_index(self->start);
- if (start == NULL)
- goto error;
-
- if (_PyLong_Sign(start) < 0) {
- /* start += length */
- PyObject *tmp = PyNumber_Add(start, length);
- Py_DECREF(start);
- start = tmp;
- if (start == NULL)
- goto error;
-
- cmp_result = PyObject_RichCompareBool(start, lower, Py_LT);
- if (cmp_result < 0)
- goto error;
- if (cmp_result) {
- Py_INCREF(lower);
- Py_DECREF(start);
- start = lower;
- }
- }
- else {
- cmp_result = PyObject_RichCompareBool(start, upper, Py_GT);
- if (cmp_result < 0)
- goto error;
- if (cmp_result) {
- Py_INCREF(upper);
- Py_DECREF(start);
- start = upper;
- }
- }
- }
-
- /* Compute stop. */
- if (self->stop == Py_None) {
- stop = step_is_negative ? lower : upper;
- Py_INCREF(stop);
- }
- else {
- stop = evaluate_slice_index(self->stop);
- if (stop == NULL)
- goto error;
-
- if (_PyLong_Sign(stop) < 0) {
- /* stop += length */
- PyObject *tmp = PyNumber_Add(stop, length);
- Py_DECREF(stop);
- stop = tmp;
- if (stop == NULL)
- goto error;
-
- cmp_result = PyObject_RichCompareBool(stop, lower, Py_LT);
- if (cmp_result < 0)
- goto error;
- if (cmp_result) {
- Py_INCREF(lower);
- Py_DECREF(stop);
- stop = lower;
- }
- }
- else {
- cmp_result = PyObject_RichCompareBool(stop, upper, Py_GT);
- if (cmp_result < 0)
- goto error;
- if (cmp_result) {
- Py_INCREF(upper);
- Py_DECREF(stop);
- stop = upper;
- }
- }
- }
-
- *start_ptr = start;
- *stop_ptr = stop;
- *step_ptr = step;
- Py_DECREF(upper);
- Py_DECREF(lower);
- return 0;
-
- error:
- *start_ptr = *stop_ptr = *step_ptr = NULL;
- Py_XDECREF(start);
- Py_XDECREF(stop);
- Py_XDECREF(step);
- Py_XDECREF(upper);
- Py_XDECREF(lower);
- return -1;
-}
-
-/* Implementation of slice.indices. */
-
static PyObject*
slice_indices(PySliceObject* self, PyObject* len)
{
- PyObject *start, *stop, *step;
- PyObject *length;
- int error;
+ Py_ssize_t ilen, start, stop, step;
- /* Convert length to an integer if necessary; raise for negative length. */
- length = PyNumber_Index(len);
- if (length == NULL)
- return NULL;
+ ilen = PyNumber_AsSsize_t(len, PyExc_OverflowError);
- if (_PyLong_Sign(length) < 0) {
- PyErr_SetString(PyExc_ValueError,
- "length should not be negative");
- Py_DECREF(length);
+ if (ilen == -1 && PyErr_Occurred()) {
return NULL;
}
- error = _PySlice_GetLongIndices(self, length, &start, &stop, &step);
- Py_DECREF(length);
- if (error == -1)
+ if (_PySlice_Unpack((PyObject *)self, &start, &stop, &step) < 0) {
return NULL;
- else
- return Py_BuildValue("(NNN)", start, stop, step);
+ }
+ _PySlice_AdjustIndices(ilen, &start, &stop, step);
+
+ return Py_BuildValue("(nnn)", start, stop, step);
}
PyDoc_STRVAR(slice_indices_doc,
@@ -548,7 +320,7 @@ S. Out of bounds indices are clipped in a manner consistent with the\n\
handling of normal slices.");
static PyObject *
-slice_reduce(PySliceObject* self, PyObject *Py_UNUSED(ignored))
+slice_reduce(PySliceObject* self)
{
return Py_BuildValue("O(OOO)", Py_TYPE(self), self->start, self->stop, self->step);
}
@@ -563,52 +335,32 @@ static PyMethodDef slice_methods[] = {
{NULL, NULL}
};
-static PyObject *
-slice_richcompare(PyObject *v, PyObject *w, int op)
+static int
+slice_compare(PySliceObject *v, PySliceObject *w)
{
- if (!PySlice_Check(v) || !PySlice_Check(w))
- Py_RETURN_NOTIMPLEMENTED;
-
- if (v == w) {
- PyObject *res;
- /* XXX Do we really need this shortcut?
- There's a unit test for it, but is that fair? */
- switch (op) {
- case Py_EQ:
- case Py_LE:
- case Py_GE:
- res = Py_True;
- break;
- default:
- res = Py_False;
- break;
- }
- Py_INCREF(res);
- return res;
- }
-
-
- PyObject *t1 = PyTuple_Pack(3,
- ((PySliceObject *)v)->start,
- ((PySliceObject *)v)->stop,
- ((PySliceObject *)v)->step);
- if (t1 == NULL) {
- return NULL;
- }
-
- PyObject *t2 = PyTuple_Pack(3,
- ((PySliceObject *)w)->start,
- ((PySliceObject *)w)->stop,
- ((PySliceObject *)w)->step);
- if (t2 == NULL) {
- Py_DECREF(t1);
- return NULL;
- }
+ int result = 0;
+
+ if (v == w)
+ return 0;
+
+ if (PyObject_Cmp(v->start, w->start, &result) < 0)
+ return -2;
+ if (result != 0)
+ return result;
+ if (PyObject_Cmp(v->stop, w->stop, &result) < 0)
+ return -2;
+ if (result != 0)
+ return result;
+ if (PyObject_Cmp(v->step, w->step, &result) < 0)
+ return -2;
+ return result;
+}
- PyObject *res = PyObject_RichCompare(t1, t2, op);
- Py_DECREF(t1);
- Py_DECREF(t2);
- return res;
+static long
+slice_hash(PySliceObject *v)
+{
+ PyErr_SetString(PyExc_TypeError, "unhashable type");
+ return -1L;
}
static int
@@ -626,15 +378,15 @@ PyTypeObject PySlice_Type = {
sizeof(PySliceObject), /* Basic object size */
0, /* Item size for varobject */
(destructor)slice_dealloc, /* tp_dealloc */
- 0, /* tp_vectorcall_offset */
+ 0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
- 0, /* tp_as_async */
+ (cmpfunc)slice_compare, /* tp_compare */
(reprfunc)slice_repr, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
- PyObject_HashNotImplemented, /* tp_hash */
+ (hashfunc)slice_hash, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
PyObject_GenericGetAttr, /* tp_getattro */
@@ -644,7 +396,7 @@ PyTypeObject PySlice_Type = {
slice_doc, /* tp_doc */
(traverseproc)slice_traverse, /* tp_traverse */
0, /* tp_clear */
- slice_richcompare, /* tp_richcompare */
+ 0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */