summaryrefslogtreecommitdiffstats
path: root/Modules/arraymodule.c
diff options
context:
space:
mode:
authorAlexander Belopolsky <alexander.belopolsky@gmail.com>2011-01-11 21:44:00 (GMT)
committerAlexander Belopolsky <alexander.belopolsky@gmail.com>2011-01-11 21:44:00 (GMT)
commitef4a03fffec57617e4c3396046960f49c463f5df (patch)
tree8e989318d3d91cd44cbeb7b2f9d0ebba921706e9 /Modules/arraymodule.c
parentcf8a382c942ab84f93a95d85758ca2e438346399 (diff)
downloadcpython-ef4a03fffec57617e4c3396046960f49c463f5df.zip
cpython-ef4a03fffec57617e4c3396046960f49c463f5df.tar.gz
cpython-ef4a03fffec57617e4c3396046960f49c463f5df.tar.bz2
Issue #5109: array.array constructor will now use fast code when
initial data is provided in an array object with correct type.
Diffstat (limited to 'Modules/arraymodule.c')
-rw-r--r--Modules/arraymodule.c20
1 files changed, 15 insertions, 5 deletions
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index 77a162c..fe6106c 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -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);