[Python-bugs-list] [ python-Bugs-444842 ] array's buffer_info() -> length in bytes

noreply@sourceforge.net noreply@sourceforge.net
Fri, 27 Jul 2001 09:07:42 -0700


Bugs item #444842, was opened at 2001-07-26 08:51
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=444842&group_id=5470

Category: Documentation
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Armin Rigo (arigo)
Assigned to: Fred L. Drake, Jr. (fdrake)
Summary: array's buffer_info() -> length in bytes

Initial Comment:
In arraymodule.c, the array's method buffer_info() is
documented to return (address, length), where 'length'
is measured in bytes. Unfortunately, it currently
returns the number of items instead. It should be fixed.

Changing the return value could potentially break code
relying on the actual (undocumented) behavior. On the
other hand, merely changing the documentation to
'officialize' the bug would be a departure from
buffer_info()'s original goal to provide direct raw
memory access.

I think the bug should be fixed, with a note about the
different behavior of previous versions in the docs --
for maximum portability, users can always compute the
length in bytes separately as "len(a)*a.itemsize".
Current users of buffer_info() may already have noticed
the bug and used the above formula to get the length,
so their code won't break.
Fix: in arraymodule.c: array_buffer_info:

PyTuple_SET_ITEM(retval, 1,
PyInt_FromLong((long)(self->ob_size *
self->ob_descr->itemsize)));
The multiplication should not overflow (care was taken
about it at resize time).

Armin

----------------------------------------------------------------------

>Comment By: Guido van Rossum (gvanrossum)
Date: 2001-07-27 09:07

Message:
Logged In: YES 
user_id=6380

Hm, perhaps buffer_info() should be deprecated?


----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2001-07-27 09:03

Message:
Logged In: YES 
user_id=6380

Boy what a mess.

There are two possibilities:

(a) essentially nobody uses this

(b) occasionally someone uses it

In case (a), fixing the code won't make a difference, so why
bother.

In case (b), fixing the code will break somebody's code.

So I see no choice but to fix the documentation.

Since the item size is readily available through the
a.itemsize attribute,
the actual buffer size is readily calculated as
len(a)*a.itemsize.

Reassigned to Fred for doc updates.  I'll fix the docstring.

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2001-07-26 15:56

Message:
Logged In: YES 
user_id=31435

Assigned to Guido cuz it's a mess.  buffer_info() has 
worked this way since it was first checked in (about 4 
years ago).  Armin is right that it doesn't match the 
manual or the docstring, and the docs make more sense than 
what it actually does.  Dare we "break" this?  Does 
buffer_info() even serve a purpose now given that arrays 
also support the buffer interface?

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=444842&group_id=5470