[Python-checkins] cpython: Fix array.array('u') constructor

victor.stinner python-checkins at python.org
Fri Sep 30 01:53:50 CEST 2011


http://hg.python.org/cpython/rev/8b4828bcd67a
changeset:   72543:8b4828bcd67a
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Fri Sep 30 01:54:04 2011 +0200
summary:
  Fix array.array('u') constructor

files:
  Modules/arraymodule.c |  18 ++++++++++++------
  1 files changed, 12 insertions(+), 6 deletions(-)


diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -2529,19 +2529,25 @@
                 Py_DECREF(v);
             }
             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) {
                     arrayobject *self = (arrayobject *)a;
-                    char *item = self->ob_item;
-                    item = (char *)PyMem_Realloc(item, n);
+                    Py_UCS4 *item = (Py_UCS4 *)self->ob_item;
+                    item = (char *)PyMem_Realloc(item, n * sizeof(Py_UCS4));
                     if (item == NULL) {
                         PyErr_NoMemory();
                         Py_DECREF(a);
                         return NULL;
                     }
-                    self->ob_item = item;
-                    Py_SIZE(self) = n / sizeof(Py_UCS4);
-                    memcpy(item, PyUnicode_AS_DATA(initial), n);
+                    self->ob_item = (char*)item;
+                    Py_SIZE(self) = n;
+                    if (!PyUnicode_AsUCS4(initial, item, n, 0))
+                        return NULL;
                     self->allocated = Py_SIZE(self);
                 }
             }

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list