]> granicus.if.org Git - python/commitdiff
Issue #5109: array.array constructor will now use fast code when
authorAlexander Belopolsky <alexander.belopolsky@gmail.com>
Tue, 11 Jan 2011 21:44:00 +0000 (21:44 +0000)
committerAlexander Belopolsky <alexander.belopolsky@gmail.com>
Tue, 11 Jan 2011 21:44:00 +0000 (21:44 +0000)
initial data is provided in an array object with correct type.

Lib/test/test_array.py
Misc/NEWS
Modules/arraymodule.c

index 33fb244534fd0ca1dde600376cf156dc7ab151ee..5190c357ea949c803144df10be91289a254d0bfa 100755 (executable)
@@ -398,6 +398,11 @@ class BaseTest(unittest.TestCase):
         if a.itemsize>1:
             self.assertRaises(ValueError, b.frombytes, b"x")
 
+    def test_fromarray(self):
+        a = array.array(self.typecode, self.example)
+        b = array.array(self.typecode, a)
+        self.assertEqual(a, b)
+
     def test_repr(self):
         a = array.array(self.typecode, 2*self.example)
         self.assertEqual(a, eval(repr(a), {"array": array.array}))
@@ -1113,6 +1118,11 @@ class NumberTest(BaseTest):
 
         self.assertRaises(AttributeError, setattr, a, "color", "blue")
 
+    def test_frombytearray(self):
+        a = array.array('b', range(10))
+        b = array.array(self.typecode, a)
+        self.assertEqual(a, b)
+
 class SignedNumberTest(NumberTest):
     example = [-1, 0, 1, 42, 0x7f]
     smallerexample = [-1, 0, 1, 42, 0x7e]
index b123cb1a6d6284f30e34b92dbd8088bbd78e9b72..262f982853624df0fe3ddfb7da6f0cab5a145295 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -955,6 +955,9 @@ Library
 Extension Modules
 -----------------
 
+- Issue #5109: array.array constructor will now use fast code when
+  initial data is provided in an array object with correct type.
+
 - Issue #6317: Now winsound.PlaySound only accepts unicode.
 
 - Issue #6317: Now winsound.PlaySound can accept non ascii filename.
index 77a162c0e1cdf0f91d7b8e635fc6c8e77e8e5871..fe6106c9a41c76ab68583c2d626c78a38e0c92b5 100644 (file)
@@ -2405,7 +2405,9 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
           || PyByteArray_Check(initial)
           || PyBytes_Check(initial)
           || PyTuple_Check(initial)
-          || ((c=='u') && PyUnicode_Check(initial)))) {
+          || ((c=='u') && PyUnicode_Check(initial))
+          || (array_Check(initial)
+              && c == ((arrayobject*)initial)->ob_descr->typecode))) {
         it = PyObject_GetIter(initial);
         if (it == NULL)
             return NULL;
@@ -2421,17 +2423,20 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
             PyObject *a;
             Py_ssize_t len;
 
-            if (initial == NULL || !(PyList_Check(initial)
-                || PyTuple_Check(initial)))
+            if (initial == NULL)
                 len = 0;
+            else if (PyList_Check(initial))
+                len = PyList_GET_SIZE(initial);
+            else if (PyTuple_Check(initial) || array_Check(initial))
+                len = Py_SIZE(initial);
             else
-                len = PySequence_Size(initial);
+                len = 0;
 
             a = newarrayobject(type, len, descr);
             if (a == NULL)
                 return NULL;
 
-            if (len > 0) {
+            if (len > 0 && !array_Check(initial)) {
                 Py_ssize_t i;
                 for (i = 0; i < len; i++) {
                     PyObject *v =
@@ -2482,6 +2487,11 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
                     self->allocated = Py_SIZE(self);
                 }
             }
+            else if (initial != NULL && array_Check(initial)) {
+                arrayobject *self = (arrayobject *)a;
+                arrayobject *other = (arrayobject *)initial;
+                memcpy(self->ob_item, other->ob_item, len * other->ob_descr->itemsize);
+            }
             if (it != NULL) {
                 if (array_iter_extend((arrayobject *)a, it) == -1) {
                     Py_DECREF(it);