[Python-Dev] Re: Deprecate the buffer object?

Paul Moore pf_moore at yahoo.co.uk
Tue Oct 28 18:05:30 EST 2003


Neil Schemenauer <nas-python at python.ca> writes:

> Looks like I was a little quick sending out that message.  I found
> more recent postings from Tim and Guido:
>
>   http://mail.python.org/pipermail/python-dev/2002-July/026408.html
>   http://mail.python.org/pipermail/python-dev/2002-July/026413.html
>
> Slippery little beast, that buffer object. :-)  I'm going to go
> ahead and add deprecation warnings.

I used it once in combination with ctypes as buffer(a-ctypes-object)
to get at the raw memory whicy ctypes objects expose via the buffer
API. But it was pretty obscure, and I would happily have used an
external module. Like this:

>>> import ctypes
>>> n = ctypes.c_int(12)
>>> buffer(n)
<read-only buffer for 0x008F6530, ptr 0x0093FB88, size 4 at 0x00977980>
>>> str(buffer(n))
'\x0c\x00\x00\x00'

Basically, the only serious use case is getting the bytes out of
objects which support the buffer API but which *don't* offer a "get
the bytes out" interface.

I've just realised that I could, however, also do this via the array
module:

>>> from array import array
>>> a = array('c')
>>> a.fromstring(n) # Hey - fromstring means "from buffer API"!
>>> a.tostring()
'\x0c\x00\x00\x00'

There's an extra copy in there. Disaster :-)

Nope, I don't think there's a good use case after all...

Paul
-- 
This signature intentionally left blank




More information about the Python-Dev mailing list