diff -r 9f9ae5f7c4ae Lib/test/test_array.py --- a/Lib/test/test_array.py Tue Dec 17 21:27:56 2013 -0500 +++ b/Lib/test/test_array.py Wed Dec 18 17:46:29 2013 +0800 @@ -11,6 +11,12 @@ from cPickle import loads, dumps, HIGHEST_PROTOCOL import sys +try: + unicode +except NameError: + unicode = None + + class ArraySubclass(array.array): pass @@ -27,8 +33,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(unicode, "Test needs unicode support") + def test_unicode_constructor(self): + self.assertRaises(TypeError, array.array, u'xx') + self.assertRaises(TypeError, array.array, u'') + self.assertRaises(ValueError, array.array, unichr(0x80)) + self.assertRaises(ValueError, array.array, u'x') + tests.append(BadConstructorTest) class BaseTest(unittest.TestCase): @@ -1040,6 +1055,55 @@ minitemsize = 4 tests.append(UnsignedLongTest) + +@unittest.skipUnless(unicode, "Test needs unicode support") +class UnicodeByteTest(ByteTest): + typecode = u'b' +tests.append(UnicodeByteTest) + + +@unittest.skipUnless(unicode, "Test needs unicode support") +class UnicodeUnsignedByteTest(UnsignedByteTest): + typecode = u'B' +tests.append(UnicodeUnsignedByteTest) + + +@unittest.skipUnless(unicode, "Test needs unicode support") +class UnicodeShortTest(ShortTest): + typecode = u'h' +tests.append(UnicodeShortTest) + + +@unittest.skipUnless(unicode, "Test needs unicode support") +class UnicodeUnsignedShortTest(UnsignedShortTest): + typecode = u'H' +tests.append(UnicodeUnsignedShortTest) + + +@unittest.skipUnless(unicode, "Test needs unicode support") +class UnicodeIntTest(IntTest): + typecode = u'i' +tests.append(UnicodeIntTest) + + +@unittest.skipUnless(unicode, "Test needs unicode support") +class UnicodeUnsignedIntTest(UnsignedIntTest): + typecode = u'I' +tests.append(UnicodeUnsignedIntTest) + + +@unittest.skipUnless(unicode, "Test needs unicode support") +class UnicodeLongTest(LongTest): + typecode = u'l' +tests.append(UnicodeLongTest) + + +@unittest.skipUnless(unicode, "Test needs unicode support") +class UnicodeUnsignedLongTest(UnsignedLongTest): + typecode = u'L' +tests.append(UnicodeUnsignedLongTest) + + class FPTest(NumberTest): example = [-42.0, 0, 42, 1e5, -1e10] smallerexample = [-42.0, 0, 42, 1e5, -2e10] diff -r 9f9ae5f7c4ae Modules/arraymodule.c --- a/Modules/arraymodule.c Tue Dec 17 21:27:56 2013 -0500 +++ b/Modules/arraymodule.c Wed Dec 18 17:46:29 2013 +0800 @@ -1929,15 +1929,35 @@ 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); + } + + 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)))) {