diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index 4b2d0554d44..6f6e96fba07 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -1142,6 +1142,15 @@ def test_raiseMemError(self): self.assertRaises(MemoryError, alloc) self.assertRaises(MemoryError, alloc) + def test_format_subclass(self): + class U(unicode): + def __unicode__(self): + return u'__unicode__ overridden' + u = U(u'xxx') + self.assertEquals("%s" % u, u'__unicode__ overridden') + self.assertEquals("{0}".format(u), u'__unicode__ overridden') + + def test_main(): test_support.run_unittest(__name__) diff --git a/Misc/NEWS b/Misc/NEWS index e4500a89796..d4a11dc7348 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -12,6 +12,8 @@ What's New in Python 2.6.6 alpha 1? Core and Builtins ----------------- +- Issue #1583863: An unicode subclass can now override the __unicode__ method + - Issue #7544: Preallocate thread memory before creating the thread to avoid a fatal error in low memory condition. diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 133cae5d5f6..111f9bb096e 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -8653,7 +8653,7 @@ PyObject *PyUnicode_Format(PyObject *format, case 's': case 'r': - if (PyUnicode_Check(v) && c == 's') { + if (PyUnicode_CheckExact(v) && c == 's') { temp = v; Py_INCREF(temp); }