[Python-Dev] marshal (was:Buffer interface in abstract.c? )

M.-A. Lemburg mal@lemburg.com
Fri, 13 Aug 1999 21:16:44 +0200


Fred L. Drake, Jr. wrote:
> 
> M.-A. Lemburg writes:
>  > Aside: Is the buffer interface reachable in any way from within
>  > Python ? Why isn't the interface exposed via __XXX__ methods
>  > on normal Python instances (could be implemented by returning a
>  > buffer object) ?
> 
>   Would it even make sense?  I though a large part of the intent was
> to for performance, avoiding memory copies.  Perhaps there should be
> an .__as_buffer__() which returned an object that supports the C
> buffer interface.  I'm not sure how useful it would be; perhaps for
> classes that represent image data?  They could return a buffer object
> created from a string/array/NumPy array.

That's what I had in mind.

def __getreadbuffer__(self):
    return buffer(self.data)

def __getcharbuffer__(self):
    return buffer(self.string_data)

def __getwritebuffer__(self):
    return buffer(self.mmaped_file)

Note that buffer() does not copy the data, it only adds a reference
to the object being used.

Hmm, how about adding a writeable binary object to the core ?
This would be useful for the __getwritebbuffer__() API because
currently, I think, only mmap'ed files are useable as write
buffers -- no other in-memory type. Perhaps buffer objects
could be used for this purpose too, e.g. by having them
allocate the needed memory chunk in case you pass None as
object.

-- 
Marc-Andre Lemburg
______________________________________________________________________
Y2000:                                                   140 days left
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/