[Python-3000] Weird error message from bytes type
Guido van Rossum
guido at python.org
Mon Feb 26 00:56:42 CET 2007
No, I don't want length-1-bytes to get special treatment here. That
would just perpetuate confusion, since b[0] *returns* an int no matter
what you might have set it to.
--Guido
On 2/25/07, Georg Brandl <g.brandl at gmx.net> wrote:
> Thomas Wouters schrieb:
> >
> > This is because a bytes object is not a sequence of bytes objects, like
> > strings. It's a sequence of small integer values, so you need to assign
> > a small integer value to it. You can assign b'a'[0] to it, or assign
> > b'a' to x[:1]. I guess we could specialcase length-1 bytes to make this
> > work 'naturally', but I'm not sure that's the right approach. Guido?
>
> If it is deemed right, see attached patch.
>
> BTW, is it intentional that the setitem/setslice code is duplicated in
> bytesobject.c?
>
> Georg
>
> Index: Objects/bytesobject.c
> ===================================================================
> --- Objects/bytesobject.c (Revision 53912)
> +++ Objects/bytesobject.c (Arbeitskopie)
> @@ -451,7 +451,18 @@
> slicelen = 1;
> }
> else {
> - Py_ssize_t ival = PyNumber_AsSsize_t(values, PyExc_ValueError);
> + Py_ssize_t ival;
> + /* if the value is a length-one bytes object, assign it */
> + if (PyBytes_Check(values)) {
> + if (PyBytes_GET_SIZE(values) != 1) {
> + PyErr_SetString(PyExc_ValueError, "cannot assign bytes "
> + "object of length != 1");
> + return -1;
> + }
> + self->ob_bytes[i] = ((PyBytesObject *)values)->ob_bytes[0];
> + return 0;
> + }
> + ival = PyNumber_AsSsize_t(values, PyExc_ValueError);
> if (ival == -1 && PyErr_Occurred())
> return -1;
> if (ival < 0 || ival >= 256) {
>
> _______________________________________________
> Python-3000 mailing list
> Python-3000 at python.org
> http://mail.python.org/mailman/listinfo/python-3000
> Unsubscribe: http://mail.python.org/mailman/options/python-3000/guido%40python.org
>
>
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-3000
mailing list