diff Objects/stringobject.c @ 35331:ef11cbc08dab legacy-trunk

Change the %s format specifier for str objects so that it returns a unicode instance if the argument is not an instance of basestring and calling __str__ on the argument returns a unicode instance.
author Neil Schemenauer <nascheme@enme.ucalgary.ca>
date Fri, 12 Aug 2005 17:34:58 +0000
parents 27a718221e72
children f63e6960c5cf
line wrap: on
line diff
--- a/Objects/stringobject.c	Fri Aug 12 14:02:32 2005 +0000
+++ b/Objects/stringobject.c	Fri Aug 12 17:34:58 2005 +0000
@@ -3853,7 +3853,6 @@
 	return 1;
 }
 
-
 /* fmt%(v1,v2,...) is roughly equivalent to sprintf(fmt, v1, v2, ...)
 
    FORMATBUFLEN is the length of the buffer in which the floats, ints, &
@@ -4079,7 +4078,9 @@
 				break;
 			case 's':
 #ifdef Py_USING_UNICODE
-				if (PyUnicode_Check(v)) {
+				temp = _PyObject_Str(v);
+				if (temp != NULL && PyUnicode_Check(temp)) {
+					Py_DECREF(temp);
 					fmt = fmt_start;
 					argidx = argidx_start;
 					goto unicode;
@@ -4087,16 +4088,11 @@
 #endif
 				/* Fall through */
 			case 'r':
-				if (c == 's')
-					temp = PyObject_Str(v);
-				else
+				if (c == 'r')
 					temp = PyObject_Repr(v);
 				if (temp == NULL)
 					goto error;
 				if (!PyString_Check(temp)) {
-					/* XXX Note: this should never happen,
-					   since PyObject_Repr() and
-					   PyObject_Str() assure this */
 					PyErr_SetString(PyExc_TypeError,
 					  "%s argument has non-string str()");
 					Py_DECREF(temp);