]> granicus.if.org Git - python/commitdiff
#12266: Fix str.capitalize() to correctly uppercase/lowercase titlecased and cased...
authorEzio Melotti <ezio.melotti@gmail.com>
Mon, 15 Aug 2011 06:22:24 +0000 (09:22 +0300)
committerEzio Melotti <ezio.melotti@gmail.com>
Mon, 15 Aug 2011 06:22:24 +0000 (09:22 +0300)
Lib/test/string_tests.py
Misc/NEWS
Objects/unicodeobject.c

index 5931f3d88ba26d6f6559880743a36f3b8371375f..9cf334573d68ef54d5ea39ecb5a67b8876a3af94 100644 (file)
@@ -96,6 +96,23 @@ class CommonTest(unittest.TestCase):
         self.checkequal('Aaaa', 'aaaa', 'capitalize')
         self.checkequal('Aaaa', 'AaAa', 'capitalize')
 
+        # check that titlecased chars are lowered correctly
+        # \u1ffc is the titlecased char
+        self.checkequal(u'\u1ffc\u1ff3\u1ff3\u1ff3',
+                        u'\u1ff3\u1ff3\u1ffc\u1ffc', 'capitalize')
+        # check with cased non-letter chars
+        self.checkequal(u'\u24c5\u24e8\u24e3\u24d7\u24de\u24dd',
+                        u'\u24c5\u24ce\u24c9\u24bd\u24c4\u24c3', 'capitalize')
+        self.checkequal(u'\u24c5\u24e8\u24e3\u24d7\u24de\u24dd',
+                        u'\u24df\u24e8\u24e3\u24d7\u24de\u24dd', 'capitalize')
+        self.checkequal(u'\u2160\u2171\u2172',
+                        u'\u2160\u2161\u2162', 'capitalize')
+        self.checkequal(u'\u2160\u2171\u2172',
+                        u'\u2170\u2171\u2172', 'capitalize')
+        # check with Ll chars with no upper - nothing changes here
+        self.checkequal(u'\u019b\u1d00\u1d86\u0221\u1fb7',
+                        u'\u019b\u1d00\u1d86\u0221\u1fb7', 'capitalize')
+
         self.checkraises(TypeError, 'hello', 'capitalize', 42)
 
     def test_count(self):
index 585a56c75d6dfad7bd9d3929840855b02cd3a6f6..497e3d4ab01293af0d17c9d162218ddfad6df277 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,9 @@ What's New in Python 2.7.3?
 Core and Builtins
 -----------------
 
+- Issue #12266: Fix str.capitalize() to correctly uppercase/lowercase
+  titlecased and cased non-letter characters.
+
 - Issues #12610 and #12609: Verify that user generated AST has correct string
   and identifier types before compiling.
 
index af259a01ba34c35cf64bab06d33bd448841978e6..4d6864d7fdb0e6968d54ae613908ed42c83814c3 100644 (file)
@@ -5485,13 +5485,13 @@ int fixcapitalize(PyUnicodeObject *self)
 
     if (len == 0)
         return 0;
-    if (Py_UNICODE_ISLOWER(*s)) {
+    if (!Py_UNICODE_ISUPPER(*s)) {
         *s = Py_UNICODE_TOUPPER(*s);
         status = 1;
     }
     s++;
     while (--len > 0) {
-        if (Py_UNICODE_ISUPPER(*s)) {
+        if (!Py_UNICODE_ISLOWER(*s)) {
             *s = Py_UNICODE_TOLOWER(*s);
             status = 1;
         }