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
[email protected]
http://mail.python.org/mailman/listinfo/python-3000
Unsubscribe: 
http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com

Reply via email to