]> granicus.if.org Git - python/commitdiff
Let u'%s' % obj try obj.__unicode__() first and fallback to obj.__str__().
authorMarc-André Lemburg <mal@egenix.com>
Fri, 23 Jul 2004 16:13:25 +0000 (16:13 +0000)
committerMarc-André Lemburg <mal@egenix.com>
Fri, 23 Jul 2004 16:13:25 +0000 (16:13 +0000)
Lib/test/test_unicode.py
Objects/unicodeobject.c

index e2db2077ccc83ad20ab48660dba1a315aaa2605c..17c82ab338c27876b2b559d8b46dc74312b7d98f 100644 (file)
@@ -438,6 +438,14 @@ class UnicodeTest(
         self.assertEqual(unicode(o), u'unicode(obj) is compatible to str()')
         self.assertEqual(str(o), 'unicode(obj) is compatible to str()')
 
+        # %-formatting and .__unicode__()
+        self.assertEqual(u'%s' %
+                         UnicodeCompat(u"u'%s' % obj uses obj.__unicode__()"),
+                         u"u'%s' % obj uses obj.__unicode__()")
+        self.assertEqual(u'%s' %
+                         UnicodeCompat(u"u'%s' % obj falls back to obj.__str__()"),
+                         u"u'%s' % obj falls back to obj.__str__()")
+
         for obj in (123, 123.45, 123L):
             self.assertEqual(unicode(obj), unicode(str(obj)))
 
index 6c73df48d3622390d2ddcdfef774cf98c4731d2d..c1550720735e4b301f0cb3b79d4155ce776f6761 100644 (file)
@@ -6883,20 +6883,15 @@ PyObject *PyUnicode_Format(PyObject *format,
                else {
                    PyObject *unicode;
                    if (c == 's')
-                       temp = PyObject_Str(v);
+                       temp = PyObject_Unicode(v);
                    else
                        temp = PyObject_Repr(v);
                    if (temp == NULL)
                        goto onError;
-                   if (!PyString_Check(temp)) {
-                       /* XXX Note: this should never happen, since
-                              PyObject_Repr() and PyObject_Str() assure
-                              this */
-                       Py_DECREF(temp);
-                       PyErr_SetString(PyExc_TypeError,
-                                       "%s argument has non-string str()");
-                       goto onError;
-                   }
+                    if (PyUnicode_Check(temp))
+                        /* nothing to do */;
+                    else if (PyString_Check(temp)) {
+                        /* convert to string to Unicode */
                    unicode = PyUnicode_Decode(PyString_AS_STRING(temp),
                                                   PyString_GET_SIZE(temp),
                                               NULL,
@@ -6905,6 +6900,13 @@ PyObject *PyUnicode_Format(PyObject *format,
                    temp = unicode;
                    if (temp == NULL)
                        goto onError;
+               }
+                   else {
+                       Py_DECREF(temp);
+                       PyErr_SetString(PyExc_TypeError,
+                                       "%s argument has non-string str()");
+                       goto onError;
+                   }
                }
                pbuf = PyUnicode_AS_UNICODE(temp);
                len = PyUnicode_GET_SIZE(temp);