diff options
author | Marc-André Lemburg <mal@egenix.com> | 2004-07-23 16:13:25 (GMT) |
---|---|---|
committer | Marc-André Lemburg <mal@egenix.com> | 2004-07-23 16:13:25 (GMT) |
commit | d25c650461b6010ad66768e4495e780683745ced (patch) | |
tree | f6e85aa066a781717bfc58294888f27596b57341 /Objects | |
parent | fe0808382b4bae113ad254ed1d984e94294beea0 (diff) | |
download | cpython-d25c650461b6010ad66768e4495e780683745ced.zip cpython-d25c650461b6010ad66768e4495e780683745ced.tar.gz cpython-d25c650461b6010ad66768e4495e780683745ced.tar.bz2 |
Let u'%s' % obj try obj.__unicode__() first and fallback to obj.__str__().
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/unicodeobject.c | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 6c73df4..c155072 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -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, @@ -6906,6 +6901,13 @@ PyObject *PyUnicode_Format(PyObject *format, 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); if (prec >= 0 && len > prec) |