diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-05-16 14:11:41 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-05-16 14:11:41 (GMT) |
commit | b70091a8d5014b1c5d8738c17ae801d79dd5392d (patch) | |
tree | ca8314f9372ccaff5c4f9453900b0bff0e45221a /Modules | |
parent | f40fcb33d2e905b128daa24f7ce6f25fb5cd5d9e (diff) | |
download | cpython-b70091a8d5014b1c5d8738c17ae801d79dd5392d.zip cpython-b70091a8d5014b1c5d8738c17ae801d79dd5392d.tar.gz cpython-b70091a8d5014b1c5d8738c17ae801d79dd5392d.tar.bz2 |
Issue #20014: array.array() now accepts unicode typecodes. Based on patch by
Vajrasky Kok.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/arraymodule.c | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 5a92862..c37644d 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -1928,16 +1928,28 @@ static PyBufferProcs array_as_buffer = { 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)))) { |