diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2013-12-08 15:44:50 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2013-12-08 15:44:50 (GMT) |
commit | 5493d5ea2afec8a1ff3227e08e908c6e433638cc (patch) | |
tree | 86070bc851a043dd89168ff1b20419ea439bb648 /Modules/_struct.c | |
parent | 337c50b8cb84155918ff0b4b5478c70f7a6f3faf (diff) | |
download | cpython-5493d5ea2afec8a1ff3227e08e908c6e433638cc.zip cpython-5493d5ea2afec8a1ff3227e08e908c6e433638cc.tar.gz cpython-5493d5ea2afec8a1ff3227e08e908c6e433638cc.tar.bz2 |
Issue #19099: The struct module now supports Unicode format strings.
Diffstat (limited to 'Modules/_struct.c')
-rw-r--r-- | Modules/_struct.c | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/Modules/_struct.c b/Modules/_struct.c index 8056956..f035847 100644 --- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -1371,13 +1371,28 @@ s_init(PyObject *self, PyObject *args, PyObject *kwds) assert(PyStruct_Check(self)); - if (!PyArg_ParseTupleAndKeywords(args, kwds, "S:Struct", kwlist, + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:Struct", kwlist, &o_format)) return -1; - Py_INCREF(o_format); - Py_CLEAR(soself->s_format); - soself->s_format = o_format; + if (PyString_Check(o_format)) { + Py_INCREF(o_format); + Py_CLEAR(soself->s_format); + soself->s_format = o_format; + } + else if (PyUnicode_Check(o_format)) { + PyObject *str = PyUnicode_AsEncodedString(o_format, "ascii", NULL); + if (str == NULL) + return -1; + Py_CLEAR(soself->s_format); + soself->s_format = str; + } + else { + PyErr_Format(PyExc_TypeError, + "Struct() argument 1 must be string, not %s", + Py_TYPE(o_format)->tp_name); + return -1; + } ret = prepare_s(soself); return ret; |