summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorNeal Norwitz <nnorwitz@gmail.com>2006-03-23 06:03:08 (GMT)
committerNeal Norwitz <nnorwitz@gmail.com>2006-03-23 06:03:08 (GMT)
commitbadc086543e13be82958b8162d3fc5e5b65f283a (patch)
tree7c65ec8f0a8dc2482b55952a8f79b6ef877b71dd /Objects
parent90768424f8c48aa0d50642cb423b356adbf2293d (diff)
downloadcpython-badc086543e13be82958b8162d3fc5e5b65f283a.zip
cpython-badc086543e13be82958b8162d3fc5e5b65f283a.tar.gz
cpython-badc086543e13be82958b8162d3fc5e5b65f283a.tar.bz2
Stop duplicating code and handle slice indices consistently and correctly
wrt to ssize_t.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/abstract.c22
-rw-r--r--Objects/classobject.c27
-rw-r--r--Objects/sliceobject.c19
3 files changed, 24 insertions, 44 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c
index 9d1aaf0..bee71d8 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -1247,24 +1247,6 @@ PySequence_GetItem(PyObject *s, Py_ssize_t i)
return type_error("unindexable object");
}
-static PyObject *
-sliceobj_from_ssizet_ssizet(Py_ssize_t i, Py_ssize_t j)
-{
- PyObject *start, *end, *slice;
- start = PyInt_FromSsize_t(i);
- if (!start)
- return NULL;
- end = PyInt_FromSsize_t(j);
- if (!end) {
- Py_DECREF(start);
- return NULL;
- }
- slice = PySlice_New(start, end, NULL);
- Py_DECREF(start);
- Py_DECREF(end);
- return slice;
-}
-
PyObject *
PySequence_GetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
{
@@ -1289,7 +1271,7 @@ PySequence_GetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2)
return m->sq_slice(s, i1, i2);
} else if ((mp = s->ob_type->tp_as_mapping) && mp->mp_subscript) {
PyObject *res;
- PyObject *slice = sliceobj_from_ssizet_ssizet(i1, i2);
+ PyObject *slice = _PySlice_FromIndices(i1, i2);
if (!slice)
return NULL;
res = mp->mp_subscript(s, slice);
@@ -1381,7 +1363,7 @@ PySequence_SetSlice(PyObject *s, Py_ssize_t i1, Py_ssize_t i2, PyObject *o)
return m->sq_ass_slice(s, i1, i2, o);
} else if ((mp = s->ob_type->tp_as_mapping) && mp->mp_ass_subscript) {
int res;
- PyObject *slice = sliceobj_from_ssizet_ssizet(i1, i2);
+ PyObject *slice = _PySlice_FromIndices(i1, i2);
if (!slice)
return -1;
res = mp->mp_ass_subscript(s, slice, o);
diff --git a/Objects/classobject.c b/Objects/classobject.c
index f3e636a..ea95ec0 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -1128,27 +1128,6 @@ instance_item(PyInstanceObject *inst, Py_ssize_t i)
}
static PyObject *
-sliceobj_from_intint(Py_ssize_t i, Py_ssize_t j)
-{
- PyObject *start, *end, *res;
-
- start = PyInt_FromLong((long)i);
- if (!start)
- return NULL;
-
- end = PyInt_FromLong((long)j);
- if (!end) {
- Py_DECREF(start);
- return NULL;
- }
- res = PySlice_New(start, end, NULL);
- Py_DECREF(start);
- Py_DECREF(end);
- return res;
-}
-
-
-static PyObject *
instance_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j)
{
PyObject *func, *arg, *res;
@@ -1168,7 +1147,7 @@ instance_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j)
func = instance_getattr(inst, getitemstr);
if (func == NULL)
return NULL;
- arg = Py_BuildValue("(N)", sliceobj_from_intint(i, j));
+ arg = Py_BuildValue("(N)", _PySlice_FromIndices(i, j));
} else
arg = Py_BuildValue("(nn)", i, j);
@@ -1239,7 +1218,7 @@ instance_ass_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j, PyObject
return -1;
arg = Py_BuildValue("(N)",
- sliceobj_from_intint(i, j));
+ _PySlice_FromIndices(i, j));
} else
arg = Py_BuildValue("(nn)", i, j);
}
@@ -1260,7 +1239,7 @@ instance_ass_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j, PyObject
return -1;
arg = Py_BuildValue("(NO)",
- sliceobj_from_intint(i, j), value);
+ _PySlice_FromIndices(i, j), value);
} else
arg = Py_BuildValue("(nnO)", i, j, value);
}
diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c
index 3b37dbb..ed00ce4 100644
--- a/Objects/sliceobject.c
+++ b/Objects/sliceobject.c
@@ -79,6 +79,25 @@ PySlice_New(PyObject *start, PyObject *stop, PyObject *step)
return (PyObject *) obj;
}
+PyObject *
+_PySlice_FromIndices(Py_ssize_t istart, Py_ssize_t istop)
+{
+ PyObject *start, *end, *slice;
+ start = PyInt_FromSsize_t(istart);
+ if (!start)
+ return NULL;
+ end = PyInt_FromSsize_t(istop);
+ if (!end) {
+ Py_DECREF(start);
+ return NULL;
+ }
+
+ slice = PySlice_New(start, end, NULL);
+ Py_DECREF(start);
+ Py_DECREF(end);
+ return slice;
+}
+
int
PySlice_GetIndices(PySliceObject *r, Py_ssize_t length,
Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)