summaryrefslogtreecommitdiffstats
path: root/Modules/arraymodule.c
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2004-08-29 07:50:43 (GMT)
committerRaymond Hettinger <python@rcn.com>2004-08-29 07:50:43 (GMT)
commit6ab78cd0c055462af4f5a6c59b9f310a83734c45 (patch)
treee997501de3a39f1e79a77ecf559000dee5ebf5f7 /Modules/arraymodule.c
parentdf7a208ff70a66f4384252d12768dbd9e7c60204 (diff)
downloadcpython-6ab78cd0c055462af4f5a6c59b9f310a83734c45.zip
cpython-6ab78cd0c055462af4f5a6c59b9f310a83734c45.tar.gz
cpython-6ab78cd0c055462af4f5a6c59b9f310a83734c45.tar.bz2
SF feature request #992967: array.array objects should support sequences.
Made the constructor accept general iterables.
Diffstat (limited to 'Modules/arraymodule.c')
-rw-r--r--Modules/arraymodule.c26
1 files changed, 20 insertions, 6 deletions
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index ab904bd..7ed3b73 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -1772,7 +1772,7 @@ static PyObject *
array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
char c;
- PyObject *initial = NULL;
+ PyObject *initial = NULL, *it = NULL;
struct arraydescr *descr;
if (kwds != NULL) {
@@ -1793,9 +1793,15 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (!(initial == NULL || PyList_Check(initial)
|| PyString_Check(initial) || PyTuple_Check(initial)
|| (c == 'u' && PyUnicode_Check(initial)))) {
- PyErr_SetString(PyExc_TypeError,
- "array initializer must be list or string");
- return NULL;
+ it = PyObject_GetIter(initial);
+ if (it == NULL)
+ return NULL;
+ /* We set initial to NULL so that the subsequent code
+ will create an empty array of the appropriate type
+ and afterwards we can use array_iter_extend to populate
+ the array.
+ */
+ initial = NULL;
}
for (descr = descriptors; descr->typecode != '\0'; descr++) {
if (descr->typecode == c) {
@@ -1859,6 +1865,14 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}
#endif
}
+ if (it != NULL) {
+ if (array_iter_extend((arrayobject *)a, it) == -1) {
+ Py_DECREF(it);
+ Py_DECREF(a);
+ return NULL;
+ }
+ Py_DECREF(it);
+ }
return a;
}
}
@@ -1899,8 +1913,8 @@ PyDoc_STRVAR(arraytype_doc,
"array(typecode [, initializer]) -> array\n\
\n\
Return a new array whose items are restricted by typecode, and\n\
-initialized from the optional initializer value, which must be a list\n\
-or a string.\n\
+initialized from the optional initializer value, which must be a list,\n\
+string. or iterable over elements of the appropriate type.\n\
\n\
Arrays represent basic values and behave very much like lists, except\n\
the type of objects stored in them is constrained.\n\