[Python-3000] Immutable bytes type and bsddb or other IO

Guido van Rossum guido at python.org
Fri Aug 24 07:19:49 CEST 2007


On 8/23/07, "Martin v. Löwis" <martin at v.loewis.de> wrote:
> > It *does* implement that! So there's the solution: these APIs should
> > not insist on bytes but use the buffer API. It's quite a bit of work I
> > suspect (especially since you can't use PyArg_ParseTuple with y# any
> > more) but worth it.
>
> I think there could be another code for PyArg_ParseTuple (or the meaning
> of y# be changed): that code would not only return char* and Py_ssize_t,
> but also a PyObject* and fill a PyBuffer b to be passed to
> PyObject_ReleaseBuffer(o, &b).

That hardly saves any work compared to O though.

> > BTW PyUnicode should *not* support the buffer API.
>
> Why not? It should set readonly to 1, and format to "u" or "w".

Because the read() method of binary files (and similar places, like
socket.send() and in the future probably various database objects)
accept anything that supports the buffer API, but writing a (text)
string to these is almost certainly a bug. Not supporting the buffer
API in PyUnicode is IMO preferable to making explicit exceptions for
PyUnicode in all those places.

I don't think that the savings possible when writing to a text file
using the UTF-16 or -32 encoding (whichever matches Py_UNICODE_SIZE)
in the native byte order are worth leaving that bug unchecked.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list