[Python-Dev] Re: adding a bytes sequence type to Python

Dima Dorfman dima at trit.org
Sun Aug 29 02:42:22 CEST 2004


Guido van Rossum <gvanrossum at gmail.com> wrote:
> Even more mysterious
> is that the array implementation appears to support the buffer API and
> yet it can't be used as an argument to write(). What's going on?

It supports the "read" buffer API but not the "character" buffer API,
so the file has to be opened in binary mode for it to work:

  >>> a = array.array('c', 'fish')
  >>> open('/dev/null', 'w').write(a)
  Traceback (most recent call last):
    File "<stdin>", line 1, in ?
  TypeError: argument 1 must be string or read-only character buffer, not array.array
  >>> open('/dev/null', 'wb').write(a)
  >>>

That restriction(?) comes from this in file_write:

  PyArg_ParseTuple(args, f->f_binary ? "s#" : "t#", ...

where s# requires a read buffer and t# requires a character buffer.

array.array is the only type in the core that's a read buffer but not
a character buffer, and I can't find any semantic differences between
read and character buffers. If someone can explain the differences or
confirm that there aren't any, I'll make this work. The easiest thing
to do would be to make array support the character buffer API (but
maybe only for [cbBu] types?).

Dima.


More information about the Python-Dev mailing list