diff options
| author | Neal Norwitz <nnorwitz@gmail.com> | 2006-08-12 17:03:09 +0000 |
|---|---|---|
| committer | Neal Norwitz <nnorwitz@gmail.com> | 2006-08-12 17:03:09 +0000 |
| commit | 8a87f5d37e6aab91ddc4c6491877b6cbd48a12cf (patch) | |
| tree | 330bb4b553958f129b31e31ccea2a2c835b19de0 /Modules | |
| parent | f3e304297e94b9b1956a4ed95debd1b163958d71 (diff) | |
| download | cpython-git-8a87f5d37e6aab91ddc4c6491877b6cbd48a12cf.tar.gz | |
Patch #1538606, Patch to fix __index__() clipping.
I modified this patch some by fixing style, some error checking, and adding
XXX comments. This patch requires review and some changes are to be expected.
I'm checking in now to get the greatest possible review and establish a
baseline for moving forward. I don't want this to hold up release if possible.
Diffstat (limited to 'Modules')
| -rw-r--r-- | Modules/arraymodule.c | 12 | ||||
| -rw-r--r-- | Modules/mmapmodule.c | 30 | ||||
| -rw-r--r-- | Modules/operator.c | 12 |
3 files changed, 14 insertions, 40 deletions
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 14e5e5d2d7..efa7835c37 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -1572,14 +1572,11 @@ array_repr(arrayobject *a) return s; } -#define HASINDEX(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_HAVE_INDEX) - static PyObject* array_subscr(arrayobject* self, PyObject* item) { - PyNumberMethods *nb = item->ob_type->tp_as_number; - if (nb != NULL && HASINDEX(item) && nb->nb_index != NULL) { - Py_ssize_t i = nb->nb_index(item); + if (PyIndex_Check(item)) { + Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); if (i==-1 && PyErr_Occurred()) { return NULL; } @@ -1627,9 +1624,8 @@ array_subscr(arrayobject* self, PyObject* item) static int array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value) { - PyNumberMethods *nb = item->ob_type->tp_as_number; - if (nb != NULL && HASINDEX(item) && nb->nb_index != NULL) { - Py_ssize_t i = nb->nb_index(item); + if (PyIndex_Check(item)) { + Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); if (i==-1 && PyErr_Occurred()) return -1; if (i < 0) diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c index b2dd675181..53df275329 100644 --- a/Modules/mmapmodule.c +++ b/Modules/mmapmodule.c @@ -808,8 +808,6 @@ static PyTypeObject mmap_object_type = { }; -#define HASINDEX(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_HAVE_INDEX) - /* extract the map size from the given PyObject Returns -1 on error, with an appropriate Python exception raised. On @@ -817,31 +815,19 @@ static PyTypeObject mmap_object_type = { static Py_ssize_t _GetMapSize(PyObject *o) { - PyNumberMethods *nb = o->ob_type->tp_as_number; - if (nb != NULL && HASINDEX(o) && nb->nb_index != NULL) { - Py_ssize_t i = nb->nb_index(o); + if (PyIndex_Check(o)) { + Py_ssize_t i = PyNumber_AsSsize_t(o, PyExc_OverflowError); if (i==-1 && PyErr_Occurred()) return -1; - if (i < 0) - goto onnegoverflow; - if (i==PY_SSIZE_T_MAX) - goto onposoverflow; + if (i < 0) { + PyErr_SetString(PyExc_OverflowError, + "memory mapped size must be positive"); + return -1; + } return i; } - else { - PyErr_SetString(PyExc_TypeError, - "map size must be an integral value"); - return -1; - } - - onnegoverflow: - PyErr_SetString(PyExc_OverflowError, - "memory mapped size must be positive"); - return -1; - onposoverflow: - PyErr_SetString(PyExc_OverflowError, - "memory mapped size is too large (limited by C int)"); + PyErr_SetString(PyExc_TypeError, "map size must be an integral value"); return -1; } diff --git a/Modules/operator.c b/Modules/operator.c index 7fc1f8abe8..7479a53553 100644 --- a/Modules/operator.c +++ b/Modules/operator.c @@ -139,15 +139,7 @@ op_ipow(PyObject *s, PyObject *a) static PyObject * op_index(PyObject *s, PyObject *a) { - Py_ssize_t i; - PyObject *a1; - if (!PyArg_UnpackTuple(a,"index", 1, 1, &a1)) - return NULL; - i = PyNumber_Index(a1); - if (i == -1 && PyErr_Occurred()) - return NULL; - else - return PyInt_FromSsize_t(i); + return PyNumber_Index(a); } static PyObject* @@ -249,7 +241,7 @@ spam1o(isMappingType, spam1(is_, "is_(a, b) -- Same as a is b.") spam1(is_not, "is_not(a, b) -- Same as a is not b.") -spam2(index, __index__, "index(a) -- Same as a.__index__()") +spam2o(index, __index__, "index(a) -- Same as a.__index__()") spam2(add,__add__, "add(a, b) -- Same as a + b.") spam2(sub,__sub__, "sub(a, b) -- Same as a - b.") spam2(mul,__mul__, "mul(a, b) -- Same as a * b.") |
