[Python-checkins] r54130 - sandbox/trunk/pep3101/test_simpleformat.py sandbox/trunk/pep3101/unicodeformat.c

eric.smith python-checkins at python.org
Mon Mar 5 02:31:19 CET 2007


Author: eric.smith
Date: Mon Mar  5 02:31:11 2007
New Revision: 54130

Modified:
   sandbox/trunk/pep3101/test_simpleformat.py
   sandbox/trunk/pep3101/unicodeformat.c
Log:
Removed dead code.  Fixed unicode 'c' formatter and added test cases for it.

Modified: sandbox/trunk/pep3101/test_simpleformat.py
==============================================================================
--- sandbox/trunk/pep3101/test_simpleformat.py	(original)
+++ sandbox/trunk/pep3101/test_simpleformat.py	Mon Mar  5 02:31:11 2007
@@ -15,14 +15,10 @@
     # overridden to change the class of string being tested
     # and the function being used.
     def formatEquals(self, result, text, *args, **kwargs):
-        text = str(text)
-        result = str(result)
         val = pep3101.format(text, *args, **kwargs)
         self.assertEquals(val, result)
 
     def formatEqualsWithUnicode(self, result, text, *args, **kwargs):
-        text = str(text)
-        result = str(result)
         val = pep3101.format(text, *args, **kwargs)
         self.assertEquals(val, result)
 
@@ -34,8 +30,6 @@
         # test both the upper and lowercase versions.  assume the
         # result and text come in as lowercase
 
-        text = str(text)
-        result = str(result)
         val = pep3101.format(text, *args, **kwargs)
         self.assertEquals(val, result)
 
@@ -120,14 +114,13 @@
         #    "The shiny red {0[-2]}", t)
 
     def test_formatlookup(self):
-        pass
-#        self.formatEquals("32", "{0:{1}}", 32, "0>4d")
-#        self.formatEquals("32", "{0:{1}{2}4{3}}", 32, "*", ">", "d")
+        self.formatEquals("0032", "{0:{1}}", 32, "0>4d")
+        self.formatEquals("**32", "{0:{1}{2}4{3}}", 32, "*", ">", "d")
 
     def test_specifiers(self):
         self.formatEquals("a", "{0:c}", ord("a"))
         self.formatEquals("8_08b", "{0:08b}", 8)
-#        self.formatEquals("8", "{0: >3d}", 8)
+        self.formatEquals("  8", "{0: >3d}", 8)
         self.formatEquals("0.1515_.0%", "{0:.0%}", .1515)
 
     def test_string_specifiers(self):
@@ -218,10 +211,11 @@
         self.formatEqualsWithUnicodeUC("-deadbeef", "{0:x}", -n)
 
     def test_char_specifiers(self):
-        self.formatEquals("A", "{0:c}", "A")
-        self.formatEquals("8", "{0:c}", "8")
-        self.formatEquals(";", "{0:c}", ";")
-        self.formatEquals(";", "{0:c}", long(ord(";")))
+        self.formatEqualsWithUnicode("A", "{0:c}", "A")
+        self.formatEqualsWithUnicode("8", "{0:c}", "8")
+        self.formatEqualsWithUnicode(";", "{0:c}", ";")
+        self.formatEqualsWithUnicode(";", "{0:c}", long(ord(";")))
+        self.formatEquals(u"f", u"{0:c}", u"f")
 
         self.formatRaises(TypeError, "{0:c}", "abcd")
 

Modified: sandbox/trunk/pep3101/unicodeformat.c
==============================================================================
--- sandbox/trunk/pep3101/unicodeformat.c	(original)
+++ sandbox/trunk/pep3101/unicodeformat.c	Mon Mar  5 02:31:11 2007
@@ -886,18 +886,36 @@
 format_char(PyObject *fieldobj, FmtState *fs,
             const InternalFormatSpec *format)
 {
-    CH_TYPE buf;
+    char ch;
+    CH_TYPE uch;
 
     if (PyString_Check(fieldobj)) {
-        if (!PyArg_Parse(fieldobj, "c;%c requires int or char", &buf))
+        if (!PyArg_Parse(fieldobj, "c;\"c\" format requires int or char", &ch))
             return 0;
+        /* convert to unicode, if needed */
+        uch = (CH_TYPE)ch;
+#if C_UNICODE
+    } else if (PyUnicode_Check(fieldobj)) {
+        CH_TYPE *ubuf;
+        int len;
+        if (!PyArg_Parse(fieldobj, "u#;\"c\" format requires int or char", &ubuf, &len))
+            return 0;
+
+        if (len != 1) {
+            PyErr_Format(PyExc_TypeError, "\"c\" format requires int or char");
+            return 0;
+        }
+        uch = ubuf[0];
+#endif
     }
     else {
-        if (!PyArg_Parse(fieldobj, "b;%c requires int or char", &buf))
+        if (!PyArg_Parse(fieldobj, "b;\"c\" format requires int or char", &ch))
             return -1;
+        /* convert to unicode, if needed */
+        uch = (CH_TYPE)ch;
     }
 
-    return output_data(fs, &buf, 1);
+    return output_data(fs, &uch, 1);
 }
 
 
@@ -1353,46 +1371,6 @@
 
     /* do the formatting, writing into the output string */
     return formatter(fieldobj, fs, &format);
-
-#if 0
-
-    /* Handle the sign logic */
-    prefix = '\0';
-    suffix = '\0';
-    if (sign == '-') {
-        if (format.sign == '(') {
-            prefix = '(';
-            suffix = ')';
-        } else {
-            prefix = '-';
-        }
-    } else if (sign == '+') {
-        if (format.sign == '+') {
-            prefix = '+';
-        } else if (format.sign == ' ') {
-            prefix = ' ';
-        }
-    }
-
-    /* Handle the padding logic */
-    if (format.width != -1) {
-        padding = format.width - len - (prefix == '\0' ? 0 : 1) -
-                                       (suffix == '\0' ? 0 : 1);
-        if (padding > 0) {
-#if 0
-            if align == '>':
-                return fill_char * padding + prefix + result + suffix
-            elif align == '='
-                return prefix + fill_char * padding + result + suffix
-            else:
-                return prefix + result + suffix + fill_char * padding
-
-#endif
-                    }
-    }
-
-    return 1;
-#endif
 }
 
 /*


More information about the Python-checkins mailing list