diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2010-02-23 23:16:07 +0000 |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2010-02-23 23:16:07 +0000 |
commit | f20f9c299ee093fd67c5b3caf0b5013af71c3136 (patch) | |
tree | fbd924eb5d0c4f24b121074fce82ca6a5dc3a658 /Objects/unicodeobject.c | |
parent | ea370a9edd7212c81921decc7e33589ad9f69389 (diff) | |
download | cpython-git-f20f9c299ee093fd67c5b3caf0b5013af71c3136.tar.gz |
Issue #7649: Fix u'%c' % char for character in range 0x80..0xFF
=> raise an UnicodeDecodeError. Patch written by Ezio Melotti.
Diffstat (limited to 'Objects/unicodeobject.c')
-rw-r--r-- | Objects/unicodeobject.c | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 130ca48463..d80ff714ae 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -8170,6 +8170,7 @@ formatchar(Py_UNICODE *buf, size_t buflen, PyObject *v) { + PyObject *s; /* presume that the buffer is at least 2 characters long */ if (PyUnicode_Check(v)) { if (PyUnicode_GET_SIZE(v) != 1) @@ -8180,7 +8181,14 @@ formatchar(Py_UNICODE *buf, else if (PyString_Check(v)) { if (PyString_GET_SIZE(v) != 1) goto onError; - buf[0] = (Py_UNICODE)PyString_AS_STRING(v)[0]; + /* #7649: if the char is a non-ascii (i.e. in range(0x80,0x100)) byte + string, "u'%c' % char" should fail with a UnicodeDecodeError */ + s = PyUnicode_FromStringAndSize(PyString_AS_STRING(v), 1); + /* if the char is not decodable return -1 */ + if (s == NULL) + return -1; + buf[0] = PyUnicode_AS_UNICODE(s)[0]; + Py_DECREF(s); } else { |