inicio mail me! sindicaci;ón

mmap is almost finished but…

… I’m quite puzzled about a couple of things.

First, I don’t understand when PySequence_GetItem it’s been called. I explain myself:

in mmapmodule.c you can find the following function to implement getitem behavior:

[code lang="c"] 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); } [/code]

What’s not clear from the C api documentation is that PySequence_GetItem is called automatically before passing the numeric index to that function. That means if you pass -1 to the map indexing mechanism in this way:

[code lang="python"] m = mmap.mmap(f.fileno(), size) print m[-1] [/code]

you get it translated to size - 1 automatically.

Update: I dug into CPython more and the truth lies in abstract.c. PyObject_GetItem calls PySequence_GetItem which in turns converts the negative index before calling the actual mmap_item function.

As the title says the mmap porting is almost finished but it misses one thing: buffer-like behavior. If you write something like this with CPython’s mmap

[code lang="python"] m = mmap(fd, size) print f.write(m) [/code]

it works. This triggers getsegcount and getcharbuffer in the mmapmodule.c. This is part of the Buffer Object protocol. How to simulate this in Python? I don’t find anything in the Python documentation.

Update: I know about buffer() existance but as the Python documentation says: Buffer objects are not directly supported by Python syntax, but can be created by calling the builtin function buffer(). They don’t support concatenation or repetition..

Related posts

  • some more mmap
  • mmap methods are completed
  • Where I Am
  • mmap is goin’ on
  • Do the bus a bus
  • Leave a Comment