[Python-3000] Are bytes object really immutable?

Guido van Rossum guido at python.org
Thu Apr 3 00:10:45 CEST 2008


On Wed, Apr 2, 2008 at 2:47 PM, Amaury Forgeot d'Arc <amauryfa at gmail.com> wrote:
> Stop me if I'm wrong, but I thought that bytes objects are immutable
>  (they are based on the PyStringType, after all)

Right. In 3.0a1 they were mutable, that's probably where these
examples come from.

>  But I was surprised by this code in test_socket.py::
>
>         buf = b" "*1024
>         nbytes = self.cli_conn.recv_into(buf)

That shouldn't work.

>  And this in getargs.c::
>
>         case 'w': { /* memory buffer, read-write access */
>                 ...
>                     ((temp = (*pb->bf_getbuffer)(arg, &view,
>                                                  PyBUF_SIMPLE)) != 0) ||
>
>  (I'd expect PyBUF_READONLY)
>
>  And this in stringobject.c::
>
>     static int
>     string_buffer_getbuffer(PyStringObject *self, Py_buffer *view, int flags)
>     {
>         return PyBuffer_FillInfo(view, (void *)self->ob_sval, Py_SIZE(self),
>                                  0, flags);
>     }
>
>  (The zero is the "readonly" parameter)
>
>  Is all of this wrong?

If it ever writes into bytes/PyString objects, yes, it is wrong!

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


More information about the Python-3000 mailing list