]> granicus.if.org Git - python/commitdiff
Creating an array with a bytes object as initializer
authorGuido van Rossum <guido@python.org>
Tue, 3 Jul 2007 16:22:09 +0000 (16:22 +0000)
committerGuido van Rossum <guido@python.org>
Tue, 3 Jul 2007 16:22:09 +0000 (16:22 +0000)
should treat the bytes as it treats a string.
Not doing this broke re.compile() of big charsets.

Lib/sre_compile.py
Lib/test/test_array.py
Modules/arraymodule.c

index 6b280520b3887a6f2d28680751cf9e2c747024d4..5b660844b36a9f0dc4c10ce7ff13ce3bd3a7e8fc 100644 (file)
@@ -353,6 +353,7 @@ def _optimize_unicode(charset, fixup):
     # Convert byte array to word array
     mapping = array.array(code, mapping)
     assert mapping.itemsize == _sre.CODESIZE
+    assert len(mapping) * mapping.itemsize == 256
     header = header + mapping.tolist()
     data[0:0] = header
     return [(BIGCHARSET, data)]
index 9b11edfc7ecca639e0bcae5986b9a328035abc1d..cf5c2e8c09d2b45fb65e1ee246b1c175698e784d 100755 (executable)
@@ -700,6 +700,10 @@ class BaseTest(unittest.TestCase):
         # SF bug #1486663 -- this used to erroneously raise a TypeError
         ArraySubclassWithKwargs('b', newarg=1)
 
+    def test_create_from_bytes(self):
+        a = array.array('H', b"1234")
+        self.assertEqual(len(a) * a.itemsize, 4)
+
 
 class StringTest(BaseTest):
 
index 253907757af259be78054faf88b833d2b9a9dcf6..11819e271f82137708952546c8387656eb67020f 100644 (file)
@@ -1789,6 +1789,7 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
                return NULL;
 
        if (!(initial == NULL || PyList_Check(initial)
+             || PyBytes_Check(initial)
              || PyString_Check(initial) || PyTuple_Check(initial)
              || (c == 'u' && PyUnicode_Check(initial)))) {
                it = PyObject_GetIter(initial);
@@ -1832,7 +1833,8 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
                                        }
                                        Py_DECREF(v);
                                }
-                       } else if (initial != NULL && PyString_Check(initial)) {
+                       } else if (initial != NULL &&
+                                  (PyString_Check(initial) || PyBytes_Check(initial))) {
                                PyObject *t_initial, *v;
                                t_initial = PyTuple_Pack(1, initial);
                                if (t_initial == NULL) {