summaryrefslogtreecommitdiff
path: root/Modules/arraymodule.c
diff options
context:
space:
mode:
authorAlexandre Vassalotti <alexandre@peadrop.com>2013-11-29 20:47:15 -0800
committerAlexandre Vassalotti <alexandre@peadrop.com>2013-11-29 20:47:15 -0800
commitb8eab76e7b6dde24a8a4878c49403bc8f570d36f (patch)
tree5a9bb5ca54e489b8d00689312113241832e734c0 /Modules/arraymodule.c
parent0cae4beb88ee7c27a4ae7384dfb554d3a53e95d4 (diff)
downloadcpython-b8eab76e7b6dde24a8a4878c49403bc8f570d36f.tar.gz
Issue #3693: Fix array obscure error message when given a str.
Diffstat (limited to 'Modules/arraymodule.c')
-rw-r--r--Modules/arraymodule.c45
1 files changed, 27 insertions, 18 deletions
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index 3466064a3d..3b013acf64 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -2518,6 +2518,20 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if (!PyArg_ParseTuple(args, "C|O:array", &c, &initial))
return NULL;
+ if (initial && c != 'u') {
+ if (PyUnicode_Check(initial)) {
+ PyErr_Format(PyExc_TypeError, "cannot use a str to initialize "
+ "an array with typecode '%c'", c);
+ return NULL;
+ }
+ else if (array_Check(initial) &&
+ ((arrayobject*)initial)->ob_descr->typecode == 'u') {
+ PyErr_Format(PyExc_TypeError, "cannot use a unicode array to "
+ "initialize an array with typecode '%c'", c);
+ return NULL;
+ }
+ }
+
if (!(initial == NULL || PyList_Check(initial)
|| PyByteArray_Check(initial)
|| PyBytes_Check(initial)
@@ -2644,9 +2658,19 @@ PyDoc_STRVAR(module_doc,
"This module defines an object type which can efficiently represent\n\
an array of basic values: characters, integers, floating point\n\
numbers. Arrays are sequence types and behave very much like lists,\n\
-except that the type of objects stored in them is constrained. The\n\
-type is specified at object creation time by using a type code, which\n\
-is a single character. The following type codes are defined:\n\
+except that the type of objects stored in them is constrained.\n");
+
+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\
+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. The type is specified\n\
+at object creation time by using a type code, which is a single character.\n\
+The following type codes are defined:\n\
\n\
Type code C Type Minimum size in bytes \n\
'b' signed integer 1 \n\
@@ -2670,21 +2694,6 @@ NOTE: The 'q' and 'Q' type codes are only available if the platform \n\
C compiler used to build Python supports 'long long', or, on Windows, \n\
'__int64'.\n\
\n\
-The constructor is:\n\
-\n\
-array(typecode [, initializer]) -- create a new array\n\
-");
-
-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\
-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\
-\n\
Methods:\n\
\n\
append() -- append a new item to the end of the array\n\