diff options
author | Guido van Rossum <guido@python.org> | 2002-10-09 19:07:53 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2002-10-09 19:07:53 (GMT) |
commit | b00c07f038b3fdc567eaf7ae3d6f5006dd66a5dd (patch) | |
tree | c96e7f2b62b7384009c14a4613ce0022f24e3324 /Objects/stringobject.c | |
parent | 06e2a5e05275f814ec179d0c1dc2980cd237a521 (diff) | |
download | cpython-b00c07f038b3fdc567eaf7ae3d6f5006dd66a5dd.zip cpython-b00c07f038b3fdc567eaf7ae3d6f5006dd66a5dd.tar.gz cpython-b00c07f038b3fdc567eaf7ae3d6f5006dd66a5dd.tar.bz2 |
The string formatting code has a test to switch to Unicode when %s
sees a Unicode argument. Unfortunately this test was also executed
for %r, because %s and %r share almost all of their code. This meant
that, if u is a unicode object while repr(u) is an 8-bit string
containing ASCII characters, '%r' % u is a *unicode* string containing
only ASCII characters!
Fixed by executing the test only for %s.
Also fixed an error message -- %s argument has non-string str()
doesn't make sense for %r, so the error message now differentiates
between %s and %r.
Diffstat (limited to 'Objects/stringobject.c')
-rw-r--r-- | Objects/stringobject.c | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 932ef51..52f96ff 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -3858,7 +3858,6 @@ PyString_Format(PyObject *format, PyObject *args) len = 1; break; case 's': - case 'r': #ifdef Py_USING_UNICODE if (PyUnicode_Check(v)) { fmt = fmt_start; @@ -3866,6 +3865,8 @@ PyString_Format(PyObject *format, PyObject *args) goto unicode; } #endif + /* Fall through */ + case 'r': if (c == 's') temp = PyObject_Str(v); else @@ -3874,7 +3875,9 @@ PyString_Format(PyObject *format, PyObject *args) goto error; if (!PyString_Check(temp)) { PyErr_SetString(PyExc_TypeError, - "%s argument has non-string str()"); + c == 's' ? + "%s argument has non-string str()" : + "%r argument has non-string repr()"); Py_DECREF(temp); goto error; } |