[Python-checkins] cpython (2.7): Issue #20014: array.array() now accepts unicode typecodes. Based on patch by

serhiy.storchaka python-checkins at python.org
Sat May 16 16:13:24 CEST 2015


https://hg.python.org/cpython/rev/7d60cb6c33cf
changeset:   96088:7d60cb6c33cf
branch:      2.7
parent:      96085:22e44a7ee89f
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sat May 16 17:11:41 2015 +0300
summary:
  Issue #20014: array.array() now accepts unicode typecodes.  Based on patch by
Vajrasky Kok.

files:
  Lib/test/test_array.py |  21 +++++++++++++++++++++
  Misc/NEWS              |   3 +++
  Modules/arraymodule.c  |  18 +++++++++++++++---
  3 files changed, 39 insertions(+), 3 deletions(-)


diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py
--- a/Lib/test/test_array.py
+++ b/Lib/test/test_array.py
@@ -26,7 +26,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')
+        self.assertRaises(ValueError, array.array, '\x80')
+
+    @test_support.requires_unicode
+    def test_unicode_constructor(self):
+        self.assertRaises(TypeError, array.array, u'xx')
+        self.assertRaises(TypeError, array.array, u'')
+        self.assertRaises(ValueError, array.array, u'x')
+        self.assertRaises(ValueError, array.array, u'\x80')
 
 tests.append(BadConstructorTest)
 
@@ -1039,6 +1049,17 @@
     minitemsize = 4
 tests.append(UnsignedLongTest)
 
+
+ at test_support.requires_unicode
+class UnicodeTypecodeTest(unittest.TestCase):
+    def test_unicode_typecode(self):
+        for typecode in typecodes:
+            a = array.array(unicode(typecode))
+            self.assertEqual(a.typecode, typecode)
+            self.assertIs(type(a.typecode), str)
+tests.append(UnicodeTypecodeTest)
+
+
 class FPTest(NumberTest):
     example = [-42.0, 0, 42, 1e5, -1e10]
     smallerexample = [-42.0, 0, 42, 1e5, -2e10]
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,6 +13,9 @@
 Library
 -------
 
+- Issue #20014: array.array() now accepts unicode typecodes.  Based on patch by
+  Vajrasky Kok.
+
 - Issue #23637: Showing a warning no longer fails with UnicodeErrror.
   Formatting unicode warning in the file with the path containing non-ascii
   characters no longer fails with UnicodeErrror.
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -1928,16 +1928,28 @@
 static PyObject *
 array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
-    char c;
-    PyObject *initial = NULL, *it = NULL;
+    int c = -1;
+    PyObject *initial = NULL, *it = NULL, *typecode = NULL;
     struct arraydescr *descr;
 
     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) && PyString_GET_SIZE(typecode) == 1)
+        c = (unsigned char)*PyString_AS_STRING(typecode);
+    else if (PyUnicode_Check(typecode) && PyUnicode_GET_SIZE(typecode) == 1)
+        c = *PyUnicode_AS_UNICODE(typecode);
+    else {
+        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;
+    }
+
     if (!(initial == NULL || PyList_Check(initial)
           || PyString_Check(initial) || PyTuple_Check(initial)
           || (c == 'u' && PyUnicode_Check(initial)))) {

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list