diff options
author | Guido van Rossum <guido@python.org> | 2007-07-03 16:22:09 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2007-07-03 16:22:09 (GMT) |
commit | 6b826abc707eb101f8426382487de88cd4e7b046 (patch) | |
tree | 4f56a78f9a4108866b265a7acbd89ba5e3aa6994 | |
parent | 166746c142a88d4f27541f6102154cc592f208ee (diff) | |
download | cpython-6b826abc707eb101f8426382487de88cd4e7b046.zip cpython-6b826abc707eb101f8426382487de88cd4e7b046.tar.gz cpython-6b826abc707eb101f8426382487de88cd4e7b046.tar.bz2 |
Creating an array with a bytes object as initializer
should treat the bytes as it treats a string.
Not doing this broke re.compile() of big charsets.
-rw-r--r-- | Lib/sre_compile.py | 1 | ||||
-rwxr-xr-x | Lib/test/test_array.py | 4 | ||||
-rw-r--r-- | Modules/arraymodule.c | 4 |
3 files changed, 8 insertions, 1 deletions
diff --git a/Lib/sre_compile.py b/Lib/sre_compile.py index 6b28052..5b66084 100644 --- a/Lib/sre_compile.py +++ b/Lib/sre_compile.py @@ -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)] diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py index 9b11edf..cf5c2e8 100755 --- a/Lib/test/test_array.py +++ b/Lib/test/test_array.py @@ -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): diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 2539077..11819e2 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -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) { |