Okay. Perhaps I am missing something but would fixing it be as simple as adding another field to the tp_as_buffer struct?
/* references returned by the buffer functins are valid while * the object remains alive */ #define PyBuffer_FLAG_SAFE 1
Then in stringobject.c (and elsewhere as appropriate):
static PyBufferProcs buffer_as_buffer = { (getreadbufferproc)buffer_getreadbuf, (getwritebufferproc)buffer_getwritebuf, (getsegcountproc)buffer_getsegcount, (getcharbufferproc)buffer_getcharbuf, PyBuffer_FLAG_SAFE, };
Then change bufferobject so that it can only be created from objects that set PyBuffer_FLAG_SAFE.
I don't know if this is enough, but if it is, I'd recommend adding the flag bitto tp_flags rather than extending the buffer structure (since you'd need to allocate an extra bit for tp_flags anyway to indicate the longer buffer struct). --Guido van Rossum (home page: http://www.python.org/~guido/)