diff -r c0dc1400866a Lib/test/test_array.py --- a/Lib/test/test_array.py Wed Dec 18 15:36:34 2013 -0600 +++ b/Lib/test/test_array.py Thu Dec 19 10:30:38 2013 +0800 @@ -27,8 +27,17 @@ self.assertRaises(TypeError, array.array) self.assertRaises(TypeError, array.array, spam=42) self.assertRaises(TypeError, array.array, 'xx') + self.assertRaises(TypeError, array.array, '') + self.assertRaises(TypeError, array.array, 1) self.assertRaises(ValueError, array.array, 'x') + @unittest.skipUnless(test_support.have_unicode, "Needs unicode support") + def test_unicode_constructor(self): + self.assertRaises(TypeError, array.array, unicode('xx')) + self.assertRaises(TypeError, array.array, unicode('')) + self.assertRaises(ValueError, array.array, unichr(0x80)) + self.assertRaises(ValueError, array.array, unicode('x')) + tests.append(BadConstructorTest) class BaseTest(unittest.TestCase): @@ -1040,6 +1049,18 @@ minitemsize = 4 tests.append(UnsignedLongTest) + +@unittest.skipUnless(test_support.have_unicode, "Needs unicode support") +class UnicodeTypecodeTest(unittest.TestCase): + def test_unicode_typecode(self): + for typecode_tuple in zip(unicode(typecodes), typecodes): + unicode_typecode, str_typecode = typecode_tuple + typecode = array.array(unicode_typecode).typecode + self.assertIs(type(typecode), str) + self.assertEqual(typecode, str_typecode) +tests.append(UnicodeTypecodeTest) + + class FPTest(NumberTest): example = [-42.0, 0, 42, 1e5, -1e10] smallerexample = [-42.0, 0, 42, 1e5, -2e10] diff -r c0dc1400866a Modules/arraymodule.c --- a/Modules/arraymodule.c Wed Dec 18 15:36:34 2013 -0600 +++ b/Modules/arraymodule.c Thu Dec 19 10:30:38 2013 +0800 @@ -1929,15 +1929,36 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { char c; - PyObject *initial = NULL, *it = NULL; + PyObject *initial = NULL, *it = NULL, *typecode = NULL; struct arraydescr *descr; + char *c_string = NULL; if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds)) return NULL; - if (!PyArg_ParseTuple(args, "c|O:array", &c, &initial)) + if (!PyArg_ParseTuple(args, "O|O:array", &typecode, &initial)) return NULL; + if (PyString_Check(typecode)) + c_string = PyString_AsString(typecode); + else if (PyUnicode_Check(typecode)) { + PyObject *str = PyUnicode_AsEncodedString(typecode, "ascii", NULL); + if (str == NULL) + return NULL; + c_string = PyString_AsString(str); + Py_DECREF(str); + } + + if (c_string == NULL || (strlen(c_string) != 1)) { + PyErr_Format(PyExc_TypeError, + "array() argument 1 or typecode must be char (string or " + "ascii-unicode with length 1), not %s", + Py_TYPE(typecode)->tp_name); + return NULL; + } + + c = c_string[0]; + if (!(initial == NULL || PyList_Check(initial) || PyString_Check(initial) || PyTuple_Check(initial) || (c == 'u' && PyUnicode_Check(initial)))) {