]> granicus.if.org Git - python/commitdiff
Merged revisions 79281 via svnmerge from
authorVictor Stinner <victor.stinner@haypocalc.com>
Mon, 22 Mar 2010 12:53:14 +0000 (12:53 +0000)
committerVictor Stinner <victor.stinner@haypocalc.com>
Mon, 22 Mar 2010 12:53:14 +0000 (12:53 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

................
  r79281 | victor.stinner | 2010-03-22 13:50:40 +0100 (lun., 22 mars 2010) | 16 lines

  Merged revisions 79278,79280 via svnmerge from
  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 7f87b438ea537653cd36c9463fcd922d7cce1255..6e4f0484a4db1247c2a7ad0d2aab87ca34f09763 100644 (file)
@@ -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__)
index b8959df4e9a7368af6f4c5f27ae306058258406f..3a9a64186a42b3afe3899308f5d221bf33f58a6c 100644 (file)
--- 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
 -------
 
index 5fd23729b6a9b4b1aa5574a1fd853eecc0393d64..2ccec90c089d4c060451a95819cd2ac78b566d23 100644 (file)
@@ -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);
                 }