Commit b70091a8 authored by Serhiy Storchaka's avatar Serhiy Storchaka

Issue #20014: array.array() now accepts unicode typecodes. Based on patch by

Vajrasky Kok.
parent f40fcb33
...@@ -26,7 +26,17 @@ class BadConstructorTest(unittest.TestCase): ...@@ -26,7 +26,17 @@ class BadConstructorTest(unittest.TestCase):
self.assertRaises(TypeError, array.array) self.assertRaises(TypeError, array.array)
self.assertRaises(TypeError, array.array, spam=42) self.assertRaises(TypeError, array.array, spam=42)
self.assertRaises(TypeError, array.array, 'xx') 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, '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) tests.append(BadConstructorTest)
...@@ -1039,6 +1049,17 @@ class UnsignedLongTest(UnsignedNumberTest): ...@@ -1039,6 +1049,17 @@ class UnsignedLongTest(UnsignedNumberTest):
minitemsize = 4 minitemsize = 4
tests.append(UnsignedLongTest) tests.append(UnsignedLongTest)
@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): class FPTest(NumberTest):
example = [-42.0, 0, 42, 1e5, -1e10] example = [-42.0, 0, 42, 1e5, -1e10]
smallerexample = [-42.0, 0, 42, 1e5, -2e10] smallerexample = [-42.0, 0, 42, 1e5, -2e10]
......
...@@ -13,6 +13,9 @@ Core and Builtins ...@@ -13,6 +13,9 @@ Core and Builtins
Library 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. - Issue #23637: Showing a warning no longer fails with UnicodeErrror.
Formatting unicode warning in the file with the path containing non-ascii Formatting unicode warning in the file with the path containing non-ascii
characters no longer fails with UnicodeErrror. characters no longer fails with UnicodeErrror.
......
...@@ -1928,16 +1928,28 @@ static PyBufferProcs array_as_buffer = { ...@@ -1928,16 +1928,28 @@ static PyBufferProcs array_as_buffer = {
static PyObject * static PyObject *
array_new(PyTypeObject *type, PyObject *args, PyObject *kwds) array_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{ {
char c; int c = -1;
PyObject *initial = NULL, *it = NULL; PyObject *initial = NULL, *it = NULL, *typecode = NULL;
struct arraydescr *descr; struct arraydescr *descr;
if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds)) if (type == &Arraytype && !_PyArg_NoKeywords("array.array()", kwds))
return NULL; return NULL;
if (!PyArg_ParseTuple(args, "c|O:array", &c, &initial)) if (!PyArg_ParseTuple(args, "O|O:array", &typecode, &initial))
return NULL; 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) if (!(initial == NULL || PyList_Check(initial)
|| PyString_Check(initial) || PyTuple_Check(initial) || PyString_Check(initial) || PyTuple_Check(initial)
|| (c == 'u' && PyUnicode_Check(initial)))) { || (c == 'u' && PyUnicode_Check(initial)))) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment