summaryrefslogtreecommitdiffstats
path: root/Modules/mmapmodule.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2006-03-07 18:50:55 (GMT)
committerGuido van Rossum <guido@python.org>2006-03-07 18:50:55 (GMT)
commit38fff8c4e4276e4e57660a78f305e68bfa87874b (patch)
tree5f79c06159053f9c7113410fc69dccba01e331ab /Modules/mmapmodule.c
parent9d7855076a8e030e30459de685e762f63bdecac6 (diff)
downloadcpython-38fff8c4e4276e4e57660a78f305e68bfa87874b.zip
cpython-38fff8c4e4276e4e57660a78f305e68bfa87874b.tar.gz
cpython-38fff8c4e4276e4e57660a78f305e68bfa87874b.tar.bz2
Checking in the code for PEP 357.
This was mostly written by Travis Oliphant. I've inspected it all; Neal Norwitz and MvL have also looked at it (in an earlier incarnation).
Diffstat (limited to 'Modules/mmapmodule.c')
-rw-r--r--Modules/mmapmodule.c25
1 files changed, 8 insertions, 17 deletions
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
index 7c3c3e4..2e34a9f 100644
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -815,6 +815,8 @@ 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
@@ -822,26 +824,15 @@ static PyTypeObject mmap_object_type = {
static Py_ssize_t
_GetMapSize(PyObject *o)
{
- if (PyInt_Check(o)) {
- long i = PyInt_AsLong(o);
- if (PyErr_Occurred())
+ 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 (i==-1 && PyErr_Occurred())
return -1;
if (i < 0)
goto onnegoverflow;
- return i;
- }
- else if (PyLong_Check(o)) {
- Py_ssize_t i = PyInt_AsSsize_t(o);
- if (PyErr_Occurred()) {
- /* yes negative overflow is mistaken for positive overflow
- but not worth the trouble to check sign of 'i' */
- if (PyErr_ExceptionMatches(PyExc_OverflowError))
- goto onposoverflow;
- else
- return -1;
- }
- if (i < 0)
- goto onnegoverflow;
+ if (i==PY_SSIZE_T_MAX)
+ goto onposoverflow;
return i;
}
else {