mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Let u'%s' % obj try obj.__unicode__() first and fallback to obj.__str__().
This commit is contained in:
parent
fe0808382b
commit
d25c650461
2 changed files with 20 additions and 10 deletions
|
@ -438,6 +438,14 @@ class UnicodeTest(
|
||||||
self.assertEqual(unicode(o), u'unicode(obj) is compatible to str()')
|
self.assertEqual(unicode(o), u'unicode(obj) is compatible to str()')
|
||||||
self.assertEqual(str(o), '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):
|
for obj in (123, 123.45, 123L):
|
||||||
self.assertEqual(unicode(obj), unicode(str(obj)))
|
self.assertEqual(unicode(obj), unicode(str(obj)))
|
||||||
|
|
||||||
|
|
|
@ -6883,20 +6883,15 @@ PyObject *PyUnicode_Format(PyObject *format,
|
||||||
else {
|
else {
|
||||||
PyObject *unicode;
|
PyObject *unicode;
|
||||||
if (c == 's')
|
if (c == 's')
|
||||||
temp = PyObject_Str(v);
|
temp = PyObject_Unicode(v);
|
||||||
else
|
else
|
||||||
temp = PyObject_Repr(v);
|
temp = PyObject_Repr(v);
|
||||||
if (temp == NULL)
|
if (temp == NULL)
|
||||||
goto onError;
|
goto onError;
|
||||||
if (!PyString_Check(temp)) {
|
if (PyUnicode_Check(temp))
|
||||||
/* XXX Note: this should never happen, since
|
/* nothing to do */;
|
||||||
PyObject_Repr() and PyObject_Str() assure
|
else if (PyString_Check(temp)) {
|
||||||
this */
|
/* convert to string to Unicode */
|
||||||
Py_DECREF(temp);
|
|
||||||
PyErr_SetString(PyExc_TypeError,
|
|
||||||
"%s argument has non-string str()");
|
|
||||||
goto onError;
|
|
||||||
}
|
|
||||||
unicode = PyUnicode_Decode(PyString_AS_STRING(temp),
|
unicode = PyUnicode_Decode(PyString_AS_STRING(temp),
|
||||||
PyString_GET_SIZE(temp),
|
PyString_GET_SIZE(temp),
|
||||||
NULL,
|
NULL,
|
||||||
|
@ -6905,6 +6900,13 @@ PyObject *PyUnicode_Format(PyObject *format,
|
||||||
temp = unicode;
|
temp = unicode;
|
||||||
if (temp == NULL)
|
if (temp == NULL)
|
||||||
goto onError;
|
goto onError;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Py_DECREF(temp);
|
||||||
|
PyErr_SetString(PyExc_TypeError,
|
||||||
|
"%s argument has non-string str()");
|
||||||
|
goto onError;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
pbuf = PyUnicode_AS_UNICODE(temp);
|
pbuf = PyUnicode_AS_UNICODE(temp);
|
||||||
len = PyUnicode_GET_SIZE(temp);
|
len = PyUnicode_GET_SIZE(temp);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue