[Python-checkins] python/dist/src/Modules arraymodule.c,2.96,2.97

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Sun Aug 29 09:50:45 CEST 2004


Update of /cvsroot/python/python/dist/src/Modules
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv772/Modules

Modified Files:
	arraymodule.c 
Log Message:
SF feature request #992967:  array.array objects should support sequences.

Made the constructor accept general iterables.



Index: arraymodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/arraymodule.c,v
retrieving revision 2.96
retrieving revision 2.97
diff -u -d -r2.96 -r2.97
--- arraymodule.c	31 May 2004 00:35:52 -0000	2.96
+++ arraymodule.c	29 Aug 2004 07:50:42 -0000	2.97
@@ -1772,7 +1772,7 @@
 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 @@
 	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 @@
 				}
 #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 @@
 "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\



More information about the Python-checkins mailing list