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

eric.smith python-checkins at python.org
Thu Mar 8 04:03:01 CET 2007


Author: eric.smith
Date: Thu Mar  8 04:02:55 2007
New Revision: 54217

Modified:
   sandbox/trunk/pep3101/test_simpleformat.py
   sandbox/trunk/pep3101/unicodeformat.c
Log:
Fixed a comment that got stepped on due to reformatting.  Improved some other comments.  Added test cases, primarily focusing on longs.

Modified: sandbox/trunk/pep3101/test_simpleformat.py
==============================================================================
--- sandbox/trunk/pep3101/test_simpleformat.py	(original)
+++ sandbox/trunk/pep3101/test_simpleformat.py	Thu Mar  8 04:02:55 2007
@@ -171,6 +171,11 @@
 
         # XXX I'm not sure this is correct, maybe it should be "     (123)"
         self.formatEqualsWithUnicode("(     123)", "{0:=()10d}", -123)
+        self.formatEqualsWithUnicode("     (123)", "{0:>()10d}", -123)
+        self.formatEqualsWithUnicode("(123)     ", "{0:<()10d}", -123)
+        self.formatEqualsWithUnicode("(     123)", "{0:=()10d}", long(-123))
+        self.formatEqualsWithUnicode("     (123)", "{0:>()10d}", long(-123))
+        self.formatEqualsWithUnicode("(123)     ", "{0:<()10d}", long(-123))
 
         self.formatEqualsWithUnicode("1" + "0" * 100, "{0:d}", 10**100)
         self.formatEqualsWithUnicode("-1" + "0" * 100, "{0:d}", -10**100)
@@ -179,6 +184,11 @@
         self.formatEqualsWithUnicode("(       1" + "0" * 100 + ")", "{0:()110d}", -10**100)
         self.formatEqualsWithUnicode("(       1" + "0" * 100 + ")", "{0:()110d}", -10**100)
 
+        for base in (0, 10**10, 10**100):
+            for i in range(base+1, base+2000):
+                self.formatEqualsWithUnicode(str(i), "{0:d}", i)
+                self.formatEqualsWithUnicode("(" + str(i) + ")", "{0:()d}", -i)
+
     def test_octal_specifiers(self):
         n = int("31415", 8)
 
@@ -199,17 +209,25 @@
         self.formatRaises(TypeError, "{0:x}", "non-number")
 
         self.formatEqualsWithUnicodeUC("0", "{0:x}", 0)
+        self.formatEqualsWithUnicodeUC("0", "{0:x}", long(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)
+        self.formatEqualsWithUnicodeUC("(deadbeef)", "{0:()x}", -n)
+        self.formatEqualsWithUnicodeUC("( deadbeef)", "{0:()11x}", -n)
+        self.formatEqualsWithUnicodeUC(" (deadbeef)", "{0:>()11x}", -n)
+        self.formatEqualsWithUnicodeUC(" (deadbeef)", "{0:>()11x}", long(-n))
+        self.formatEqualsWithUnicodeUC("(deadbeef) ", "{0:<()11x}", -n)
+        self.formatEqualsWithUnicodeUC("(deadbeef) ", "{0:<()11x}", long(-n))
 
     def test_char_specifiers(self):
         self.formatEqualsWithUnicode("A", "{0:c}", "A")
         self.formatEqualsWithUnicode("8", "{0:c}", "8")
         self.formatEqualsWithUnicode(";", "{0:c}", ";")
+        self.formatEqualsWithUnicode(";", "{0:c}", ord(";"))
         self.formatEqualsWithUnicode(";", "{0:c}", long(ord(";")))
         self.formatEquals(u"f", u"{0:c}", u"f")
 
@@ -276,16 +294,19 @@
         self.formatEqualsWithUnicode("-" + "1" * 100, "{0:b}", -(2**100 - 1))
         self.formatEqualsWithUnicode("(" + "1" * 100 + ")", "{0:()b}", -(2**100 - 1))
         self.formatEqualsWithUnicode("(" + " " * 98 + "1" * 100 + ")", "{0:=()200b}", -(2**100 - 1))
-        for n in range(1, 100):
+        for n in range(1, 1000):
             # build an n bit integer, alternating ones and zeros
             # as n gets large, these will become longs
             s = "10" * (n / 2) + (n % 2 and "1" or "")
             i = int(s, 2)
-            self.formatEquals(s, "{0:b}", i)
+            self.formatEqualsWithUnicode(s, "{0:b}", i)
 
             # make sure negative also works, hoping to catch an
-            #  overflow off the end of the digits
-            self.formatEquals("(" + s + ")", "{0:()b}", -i)
+            # overflow off the end of the digits.  an internal
+            # implementation detail is that the parens are inserted
+            # into the output buffer before the digits are, so any
+            # error in writing the digits might write over the parens
+            self.formatEqualsWithUnicode("(" + s + ")", "{0:()b}", -i)
 
     def test_number_specifier(self):
         def test(value):

Modified: sandbox/trunk/pep3101/unicodeformat.c
==============================================================================
--- sandbox/trunk/pep3101/unicodeformat.c	(original)
+++ sandbox/trunk/pep3101/unicodeformat.c	Thu Mar  8 04:02:55 2007
@@ -67,8 +67,8 @@
 
 
 /* MAXLEN_INT_STRING is the maximum length of an integer represented
- * as a string.  The analysis in stringobject.c shows that 24 is the
- * worst case.  Allocate more, just in case. */
+ * as a string, in base 10.  The analysis in stringobject.c shows that
+ * 24 is the worst case.  Allocate more, just in case. */
 /* fmt = '%#.' + `prec` + 'l' + `type`
    worst case length = 3 + 19 (worst len of INT_MAX on 64-bit machine)
                        + 1 + 1 = 24 */
@@ -1117,7 +1117,9 @@
    complement, and we need to undo that. */
 static int
 _format_long_binary(PyObject *v, FmtState *fs, const
-InternalFormatSpec *format) { /* we know that v is a PyLongObject */
+                    InternalFormatSpec *format)
+{
+    /* we know that v is a PyLongObject */
     PyLongObject* l = (PyLongObject*)v;
 
     IntegerFieldWidths spec;
@@ -1129,7 +1131,9 @@
     unsigned char* byte_buffer;
     unsigned char* start_byte_buffer;
     char sign = is_negative ? '-' : '\0';
-    Py_ssize_t n_digits = _PyLong_NumBits(v);
+    Py_ssize_t n_digits = _PyLong_NumBits(v);  /* same number of
+                                                  output digits as we
+                                                  have binary bits */
     int is_zero = n_digits == 0;
     int carry_bit;
 


More information about the Python-checkins mailing list