mmap as a sequence behavior

Lawrence Oluyede rhymes at myself.com
Wed Jun 28 23:50:38 CEST 2006


While wrapping mmap indexing/sequence emulation I noticed something
"strange".

The source code of this oddity is:

static PyObject *
mmap_item(mmap_object *self, Py_ssize_t i)
{
    CHECK_VALID(NULL);
    if (i < 0 || (size_t)i >= self->size) {
        PyErr_SetString(PyExc_IndexError, "mmap index out of range");
        return NULL;
    }
    return PyString_FromStringAndSize(self->data + i, 1);
}

located in mmapmodule.c

What's got my attention was the fact that passing -1 from Python does
not trigger the exception but is changed to the last positive and valid
index. Let's make an example:

import mmap
f = open("foo", "w+")
f.write("foobar")
f.flush()
m = mmap.mmap(f.fileno(), 6)
print m[-1] # == 'f'

I expect this raise IndexError as reading the C source code but it
behaves exactly like sequence types in Python. That's fair to me but I
don't get *where* my index is translated.

If I print the 'i' variable in the C source code before the if statement
I get len(ourmap) - 1 instead of "-1"

What am I missing from the C API?

-- 
Lawrence - http://www.oluyede.org/blog
"Nothing is more dangerous than an idea
if it's the only one you have" - E. A. Chartier



More information about the Python-list mailing list