diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index 7f87b438ea5..6e4f0484a4d 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -1223,6 +1223,14 @@ class UnicodeTest( self.assertRaises(MemoryError, alloc) self.assertRaises(MemoryError, alloc) + def test_format_subclass(self): + class S(str): + def __str__(self): + return '__str__ overridden' + s = S('xxx') + self.assertEquals("%s" % s, '__str__ overridden') + self.assertEquals("{}".format(s), '__str__ overridden') + def test_main(): support.run_unittest(__name__) diff --git a/Misc/NEWS b/Misc/NEWS index b8959df4e9a..3a9a64186a4 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -9,6 +9,11 @@ What's New in Python 3.1.3? *Release date: 20XX-XX-XX* +Core and Builtins +----------------- + +- Issue #1583863: An str subclass can now override the __str__ method + Library ------- diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 5fd23729b6a..2ccec90c089 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -9308,7 +9308,7 @@ PyObject *PyUnicode_Format(PyObject *format, case 's': case 'r': case 'a': - if (PyUnicode_Check(v) && c == 's') { + if (PyUnicode_CheckExact(v) && c == 's') { temp = v; Py_INCREF(temp); }