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

eric.smith python-checkins at python.org
Mon Mar 5 01:45:33 CET 2007


Author: eric.smith
Date: Mon Mar  5 01:45:26 2007
New Revision: 54129

Modified:
   sandbox/trunk/pep3101/test_simpleformat.py
   sandbox/trunk/pep3101/unicodeformat.c
Log:
Fix hex and octal formatting.  Added testcases.

Modified: sandbox/trunk/pep3101/test_simpleformat.py
==============================================================================
--- sandbox/trunk/pep3101/test_simpleformat.py	(original)
+++ sandbox/trunk/pep3101/test_simpleformat.py	Mon Mar  5 01:45:26 2007
@@ -30,6 +30,27 @@
         val = pep3101.format(unicode(text), *args, **kwargs)
         self.assertEquals(val, unicode(result))
 
+    def formatEqualsWithUnicodeUC(self, result, text, *args, **kwargs):
+        # 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)
+
+        # a quick check for unicode version
+        val = pep3101.format(unicode(text), *args, **kwargs)
+        self.assertEquals(val, unicode(result))
+
+        # test the uppercase text version
+        val = pep3101.format(text.upper(), *args, **kwargs)
+        self.assertEquals(val, result.upper())
+
+        # test the uppercase unicode version
+        val = pep3101.format(unicode(text.upper()), *args, **kwargs)
+        self.assertEquals(val, unicode(result.upper()))
+
     def formatRaises(self, exc, text, *args, **kwargs):
         exc = exc or Exception #StringFormat.FormatError
         text = str(text)
@@ -169,6 +190,33 @@
         self.formatEqualsWithUnicode("(       1" + "0" * 100 + ")", "{0:()110d}", -10**100)
         self.formatEqualsWithUnicode("(       1" + "0" * 100 + ")", "{0:()110d}", -10**100)
 
+    def test_octal_specifiers(self):
+        n = int("31415", 8)
+
+        self.assertRaises(TypeError, "{0:o", "non-number")
+
+        self.formatEqualsWithUnicode("0", "{0:o}", 0)
+        self.formatEqualsWithUnicode("31415", "{0:o}", n)
+        self.formatEqualsWithUnicode("-31415", "{0:o}", -n)
+        self.formatEqualsWithUnicode(" " * 995 + "31415", "{0:1000o}", n)
+
+        n = int("314153141531415", 8)
+        self.formatEqualsWithUnicode("314153141531415", "{0:o}", n)
+        self.formatEqualsWithUnicode("-314153141531415", "{0:o}", -n)
+
+    def test_hex_specifiers(self):
+        n = int("beef", 16)
+
+        self.assertRaises(TypeError, "{0:x", "non-number")
+
+        self.formatEqualsWithUnicodeUC("0", "{0:x}", 0)
+        self.formatEqualsWithUnicodeUC("beef", "{0:x}", n)
+        self.formatEqualsWithUnicodeUC("-beef", "{0:x}", -n)
+
+        n = int("deadbeef", 16)
+        self.formatEqualsWithUnicodeUC("deadbeef", "{0:x}", n)
+        self.formatEqualsWithUnicodeUC("-deadbeef", "{0:x}", -n)
+
     def test_char_specifiers(self):
         self.formatEquals("A", "{0:c}", "A")
         self.formatEquals("8", "{0:c}", "8")

Modified: sandbox/trunk/pep3101/unicodeformat.c
==============================================================================
--- sandbox/trunk/pep3101/unicodeformat.c	(original)
+++ sandbox/trunk/pep3101/unicodeformat.c	Mon Mar  5 01:45:26 2007
@@ -1008,6 +1008,19 @@
     n_allocated = len;
     *pbuf = fs->outstr.ptr;
 
+    /* if we're hex or octal, check to see if 0 or 0x or 0X was at the
+       front of the string.  if so, skip it. */
+    if (type == 'o' && n_allocated >= 1 && *pbuf[0] == '0') {
+        p_charbuf++;
+        n_allocated -= 1;
+    } else if (type == 'x' && n_allocated >= 2 && *pbuf[1] == 'x') {
+        p_charbuf += 2;
+        n_allocated -= 1;
+    } else if (type == 'X' && n_allocated >= 2 && *pbuf[1] == 'X') {
+        p_charbuf += 2;
+        n_allocated -= 1;
+    }
+
 #if C_UNICODE
     /* allocate space in the output string, as CH_TYPE */
     ok = output_allocate(fs, n_allocated, &ptr);
@@ -1015,7 +1028,7 @@
         strtounicode(ptr, p_charbuf, n_allocated);
     }
 #else
-    ok = output_data(fs, STROBJ_AS_PTR(strobj), n_allocated);
+    ok = output_data(fs, p_charbuf, n_allocated);
 #endif
 
     /* we're done with the string representation */


More information about the Python-checkins mailing list