diff Objects/stringobject.c @ 25682:ce49f12a1769 legacy-trunk

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.
author Guido van Rossum <guido@python.org>
date Wed, 09 Oct 2002 19:07:53 +0000
parents cb9c1f94220b
children 38843d35b646
line wrap: on
line diff
--- a/Objects/stringobject.c	Wed Oct 09 18:17:06 2002 +0000
+++ b/Objects/stringobject.c	Wed Oct 09 19:07:53 2002 +0000
@@ -3858,7 +3858,6 @@
 				len = 1;
 				break;
 			case 's':
-  			case 'r':
 #ifdef Py_USING_UNICODE
 				if (PyUnicode_Check(v)) {
 					fmt = fmt_start;
@@ -3866,6 +3865,8 @@
 					goto unicode;
 				}
 #endif
+				/* Fall through */
+  			case 'r':
 				if (c == 's')
 					temp = PyObject_Str(v);
 				else
@@ -3874,7 +3875,9 @@
 					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;
 				}