Skip to content

Commit 01b3e71

Browse files
author
guido.van.rossum
committed
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. git-svn-id: http://svn.python.org/projects/python/branches/py3k-struni@56161 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent c1772af commit 01b3e71

3 files changed

Lines changed: 8 additions & 1 deletion

File tree

Lib/sre_compile.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ def _optimize_unicode(charset, fixup):
353353
# Convert byte array to word array
354354
mapping = array.array(code, mapping)
355355
assert mapping.itemsize == _sre.CODESIZE
356+
assert len(mapping) * mapping.itemsize == 256
356357
header = header + mapping.tolist()
357358
data[0:0] = header
358359
return [(BIGCHARSET, data)]

Lib/test/test_array.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,10 @@ def test_subclass_with_kwargs(self):
700700
# SF bug #1486663 -- this used to erroneously raise a TypeError
701701
ArraySubclassWithKwargs('b', newarg=1)
702702

703+
def test_create_from_bytes(self):
704+
a = array.array('H', b"1234")
705+
self.assertEqual(len(a) * a.itemsize, 4)
706+
703707

704708
class StringTest(BaseTest):
705709

Modules/arraymodule.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1789,6 +1789,7 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
17891789
return NULL;
17901790

17911791
if (!(initial == NULL || PyList_Check(initial)
1792+
|| PyBytes_Check(initial)
17921793
|| PyString_Check(initial) || PyTuple_Check(initial)
17931794
|| (c == 'u' && PyUnicode_Check(initial)))) {
17941795
it = PyObject_GetIter(initial);
@@ -1832,7 +1833,8 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
18321833
}
18331834
Py_DECREF(v);
18341835
}
1835-
} else if (initial != NULL && PyString_Check(initial)) {
1836+
} else if (initial != NULL &&
1837+
(PyString_Check(initial) || PyBytes_Check(initial))) {
18361838
PyObject *t_initial, *v;
18371839
t_initial = PyTuple_Pack(1, initial);
18381840
if (t_initial == NULL) {

0 commit comments

Comments
 (0)