[Python-3000-checkins] r65073 - in python/branches/py3k: Doc/library/stdtypes.rst Doc/library/string.rst Lib/test/test_format.py Lib/test/test_types.py Misc/NEWS Objects/stringlib/formatter.h Objects/unicodeobject.c

eric.smith python-3000-checkins at python.org
Thu Jul 17 20:30:48 CEST 2008


Author: eric.smith
Date: Thu Jul 17 20:30:48 2008
New Revision: 65073

Log:
Merged revisions 65069 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r65069 | eric.smith | 2008-07-17 13:48:39 -0400 (Thu, 17 Jul 2008) | 1 line
  
  Issue 3382: Make '%F' and float.__format__('F') convert results to upper case.
........


Modified:
   python/branches/py3k/   (props changed)
   python/branches/py3k/Doc/library/stdtypes.rst
   python/branches/py3k/Doc/library/string.rst
   python/branches/py3k/Lib/test/test_format.py
   python/branches/py3k/Lib/test/test_types.py
   python/branches/py3k/Misc/NEWS
   python/branches/py3k/Objects/stringlib/formatter.h
   python/branches/py3k/Objects/unicodeobject.c

Modified: python/branches/py3k/Doc/library/stdtypes.rst
==============================================================================
--- python/branches/py3k/Doc/library/stdtypes.rst	(original)
+++ python/branches/py3k/Doc/library/stdtypes.rst	Thu Jul 17 20:30:48 2008
@@ -1223,9 +1223,9 @@
 +------------+-----------------------------------------------------+-------+
 | ``'E'``    | Floating point exponential format (uppercase).      | \(3)  |
 +------------+-----------------------------------------------------+-------+
-| ``'f'``    | Floating point decimal format.                      | \(3)  |
+| ``'f'``    | Floating point decimal format (lowercase).          | \(3)  |
 +------------+-----------------------------------------------------+-------+
-| ``'F'``    | Floating point decimal format.                      | \(3)  |
+| ``'F'``    | Floating point decimal format (uppercase).          | \(3)  |
 +------------+-----------------------------------------------------+-------+
 | ``'g'``    | Floating point format. Uses lowercase exponential   | \(4)  |
 |            | format if exponent is less than -4 or not less than |       |

Modified: python/branches/py3k/Doc/library/string.rst
==============================================================================
--- python/branches/py3k/Doc/library/string.rst	(original)
+++ python/branches/py3k/Doc/library/string.rst	Thu Jul 17 20:30:48 2008
@@ -402,13 +402,14 @@
    | ``'e'`` | Exponent notation. Prints the number in scientific       |
    |         | notation using the letter 'e' to indicate the exponent.  |
    +---------+----------------------------------------------------------+
-   | ``'E'`` | Exponent notation. Same as ``'e'`` except it uses an     |
-   |         | upper case 'E' as the separator character.               |
+   | ``'E'`` | Exponent notation. Same as ``'e'`` except it converts    |
+   |         | the number to upper case.                                |
    +---------+----------------------------------------------------------+
    | ``'f'`` | Fixed point. Displays the number as a fixed-point        |
    |         | number.                                                  |
    +---------+----------------------------------------------------------+
-   | ``'F'`` | Fixed point. Same as ``'f'``.                            |
+   | ``'F'`` | Fixed point. Same as ``'f'`` except it converts the      |
+   |         | number to upper case.                                    |
    +---------+----------------------------------------------------------+
    | ``'g'`` | General format. This prints the number as a fixed-point  |
    |         | number, unless the number is too large, in which case    |

Modified: python/branches/py3k/Lib/test/test_format.py
==============================================================================
--- python/branches/py3k/Lib/test/test_format.py	(original)
+++ python/branches/py3k/Lib/test/test_format.py	Thu Jul 17 20:30:48 2008
@@ -79,6 +79,17 @@
         testformat("%#.*f", (110, -1.e+100/3.))
         testformat("%#.*F", (110, -1.e+100/3.))
         overflowrequired = 0
+        # check for %f and %F
+        testformat("%f", (1.0,), "1.000000")
+        testformat("%F", (1.0,), "1.000000")
+        testformat("%f", (1e100,), "1e+100")
+        testformat("%F", (1e100,), "1E+100")
+        testformat("%f", (1e100,), "1e+100")
+        testformat("%F", (1e100,), "1E+100")
+        testformat("%f", (float('nan'),), "nan")
+        testformat("%F", (float('nan'),), "NAN")
+        testformat("%f", (float('inf'),), "inf")
+        testformat("%F", (float('inf'),), "INF")
         # Formatting of integers. Overflow is not ok
         overflowok = 0
         testformat("%x", 10, "a")

Modified: python/branches/py3k/Lib/test/test_types.py
==============================================================================
--- python/branches/py3k/Lib/test/test_types.py	(original)
+++ python/branches/py3k/Lib/test/test_types.py	Thu Jul 17 20:30:48 2008
@@ -514,9 +514,17 @@
         test( 1.0, '+f', '+1.000000')
         test(-1.0, '+f', '-1.000000')
         test(1.1234e90, 'f', '1.1234e+90')
-        test(1.1234e90, 'F', '1.1234e+90')
+        test(1.1234e90, 'F', '1.1234E+90')
         test(1.1234e200, 'f', '1.1234e+200')
-        test(1.1234e200, 'F', '1.1234e+200')
+        test(1.1234e200, 'F', '1.1234E+200')
+        test(1e100, 'x<20f', '1e+100xxxxxxxxxxxxxx')
+        test(1e100, 'x<20F', '1E+100xxxxxxxxxxxxxx')
+        test(float('nan'), 'f', 'nan')
+        test(float('nan'), 'F', 'NAN')
+        test(float('inf'), 'f', 'inf')
+        test(float('inf'), 'F', 'INF')
+        test(float('-inf'), 'f', '-inf')
+        test(float('-inf'), 'F', '-INF')
 
         test( 1.0, 'e', '1.000000e+00')
         test(-1.0, 'e', '-1.000000e+00')

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Thu Jul 17 20:30:48 2008
@@ -12,6 +12,9 @@
 Core and Builtins
 -----------------
 
+- Issue #3382: Make '%F' and float.__format__('F') convert results
+  to uppercase.
+
 - Issue #3008: the float type has a new instance method 'float.hex'
   and a new class method 'float.fromhex' to convert floating-point
   numbers to and from hexadecimal strings, respectively.

Modified: python/branches/py3k/Objects/stringlib/formatter.h
==============================================================================
--- python/branches/py3k/Objects/stringlib/formatter.h	(original)
+++ python/branches/py3k/Objects/stringlib/formatter.h	Thu Jul 17 20:30:48 2008
@@ -741,10 +741,6 @@
     /* first, do the conversion as 8-bit chars, using the platform's
        snprintf.  then, if needed, convert to unicode. */
 
-    /* 'F' is the same as 'f', per the PEP */
-    if (type == 'F')
-        type = 'f';
-
     x = PyFloat_AsDouble(value);
 
     if (x == -1.0 && PyErr_Occurred())
@@ -758,8 +754,12 @@
 
     if (precision < 0)
         precision = 6;
-    if (type == 'f' && (fabs(x) / 1e25) >= 1e25)
-        type = 'g';
+    if ((type == 'f' || type == 'F') && (fabs(x) / 1e25) >= 1e25) {
+	if (type == 'f')
+	    type = 'g';
+	else
+	    type = 'G';
+    }
 
     /* cast "type", because if we're in unicode we need to pass a
        8-bit char.  this is safe, because we've restricted what "type"

Modified: python/branches/py3k/Objects/unicodeobject.c
==============================================================================
--- python/branches/py3k/Objects/unicodeobject.c	(original)
+++ python/branches/py3k/Objects/unicodeobject.c	Thu Jul 17 20:30:48 2008
@@ -8588,8 +8588,12 @@
 	return -1;
     if (prec < 0)
 	prec = 6;
-    if (type == 'f' && (fabs(x) / 1e25) >= 1e25)
-	type = 'g';
+    if ((type == 'f' || type == 'F') && (fabs(x) / 1e25) >= 1e25) {
+        if (type == 'f')
+            type = 'g';
+        else
+            type = 'G';
+    }
     /* Worst case length calc to ensure no buffer overrun:
 
        'g' formats:
@@ -8608,7 +8612,8 @@
     */
     if (((type == 'g' || type == 'G') && 
           buflen <= (size_t)10 + (size_t)prec) ||
-	(type == 'f' && buflen <= (size_t)53 + (size_t)prec)) {
+	((type == 'f' || type == 'F')  &&
+          buflen <= (size_t)53 + (size_t)prec)) {
 	PyErr_SetString(PyExc_OverflowError,
 			"formatted float is too long (precision too large?)");
 	return -1;
@@ -9091,8 +9096,6 @@
 	    case 'F':
 	    case 'g':
 	    case 'G':
-		if (c == 'F')
-			c = 'f';
 		pbuf = formatbuf;
 		len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE),
 			flags, prec, c, v);


More information about the Python-3000-checkins mailing list