]> granicus.if.org Git - python/commitdiff
Merged revisions 79278,79280 via svnmerge from
authorVictor Stinner <victor.stinner@haypocalc.com>
Mon, 22 Mar 2010 12:56:39 +0000 (12:56 +0000)
committerVictor Stinner <victor.stinner@haypocalc.com>
Mon, 22 Mar 2010 12:56:39 +0000 (12:56 +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 4b2d0554d44ecaa6fd847ee32415179d5b6cee7d..6f6e96fba07265b92923e85621a29c06980710ba 100644 (file)
@@ -1142,6 +1142,15 @@ class UnicodeTest(
         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__)
 
index e4500a8979616327309bb27c1f8e1da3815807c8..d4a11dc7348ac968e81fe2770aab9e4803450a86 100644 (file)
--- 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.
 
index 133cae5d5f6dfbc47dfb4334f479dfac92a56444..111f9bb096ec92f57f0e3157467004e82ee25ee6 100644 (file)
@@ -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);
                 }