[Python-3000] Are bytes object really immutable?

Travis Oliphant oliphant.travis at ieee.org
Thu Apr 3 00:11:34 CEST 2008


Amaury Forgeot d'Arc wrote:
> Stop me if I'm wrong, but I thought that bytes objects are immutable
> (they are based on the PyStringType, after all)
>

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

I'm not sure about this one...

> 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)

This one is O.K. because 'w' is requesting read-write access.  An error 
will occur if the object does not allow it.

> 
> 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);
>     }
> 

You are right that the 0 here should be a 1 for the immutable bytes object.


Good job.

-Travis O.



More information about the Python-3000 mailing list