Fix array.array('u') constructor

This commit is contained in:
Victor Stinner 2011-09-30 01:54:04 +02:00
parent eb5657a0c5
commit 1fbcaeff55

View file

@ -2529,19 +2529,25 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Py_DECREF(v); Py_DECREF(v);
} }
else if (initial != NULL && PyUnicode_Check(initial)) { else if (initial != NULL && PyUnicode_Check(initial)) {
Py_ssize_t n = PyUnicode_GET_DATA_SIZE(initial); Py_ssize_t n;
if (PyUnicode_READY(initial)) {
Py_DECREF(a);
return NULL;
}
n = PyUnicode_GET_LENGTH(initial);
if (n > 0) { if (n > 0) {
arrayobject *self = (arrayobject *)a; arrayobject *self = (arrayobject *)a;
char *item = self->ob_item; Py_UCS4 *item = (Py_UCS4 *)self->ob_item;
item = (char *)PyMem_Realloc(item, n); item = (char *)PyMem_Realloc(item, n * sizeof(Py_UCS4));
if (item == NULL) { if (item == NULL) {
PyErr_NoMemory(); PyErr_NoMemory();
Py_DECREF(a); Py_DECREF(a);
return NULL; return NULL;
} }
self->ob_item = item; self->ob_item = (char*)item;
Py_SIZE(self) = n / sizeof(Py_UCS4); Py_SIZE(self) = n;
memcpy(item, PyUnicode_AS_DATA(initial), n); if (!PyUnicode_AsUCS4(initial, item, n, 0))
return NULL;
self->allocated = Py_SIZE(self); self->allocated = Py_SIZE(self);
} }
} }