]> granicus.if.org Git - python/commitdiff
Merged revisions 79278,79280 via svnmerge from
authorVictor Stinner <victor.stinner@haypocalc.com>
Mon, 22 Mar 2010 12:50:40 +0000 (12:50 +0000)
committerVictor Stinner <victor.stinner@haypocalc.com>
Mon, 22 Mar 2010 12:50:40 +0000 (12:50 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r79278 | victor.stinner | 2010-03-22 13:24:37 +0100 (lun., 22 mars 2010) | 2 lines

  Issue #1583863: An unicode subclass can now override the __str__ method
........
  r79280 | victor.stinner | 2010-03-22 13:36:28 +0100 (lun., 22 mars 2010) | 5 lines

  Fix the NEWS about my last commit: an unicode subclass can now override the
  __unicode__ method (and not the __str__ method).

  Simplify also the testcase.
........

Lib/test/test_unicode.py
Misc/NEWS
Objects/unicodeobject.c

index 1da2d1491cdc000b66f7785ff462854deedc1e04..4ac07123b86d6e7db25d9ebd854ba37baeebbd68 100644 (file)
@@ -1221,6 +1221,14 @@ class UnicodeTest(string_tests.CommonTest,
         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__)
index cfe45fb666190f77c5a2e79d152a8ca188e3238d..8a62cbf4a29a2ecfc14b03c38a7ec48cf9fd8256 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 3.2 Alpha 1?
 Core and Builtins
 -----------------
 
+- Issue #1583863: An str subclass can now override the __str__ method
+
 - Issue #8014: Setting a T_UINT or T_PYSSIZET attribute of an object with
   PyMemberDefs could produce an internal error;  raise TypeError instead.
 
index 3ee90b58ddb79b17ad06544a73995b589bd12839..28b8c66295aae53f3a8346aba7b0c5bab898aeb5 100644 (file)
@@ -9124,7 +9124,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);
                 }