]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue 3382: Make '%F' and float.__format__('F') convert results to upper case.
authorEric Smith <eric@trueblade.com>
Thu, 17 Jul 2008 17:48:39 +0000 (17:48 +0000)
committerEric Smith <eric@trueblade.com>
Thu, 17 Jul 2008 17:48:39 +0000 (17:48 +0000)
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/stringobject.c
Objects/unicodeobject.c

index e857154b301009c3f1b700fcae6212e06f564e4a..3c0aa189f7fc6b12682c35506fbf7b9ccc62085d 100644 (file)
@@ -1296,9 +1296,9 @@ The conversion types are:
 +------------+-----------------------------------------------------+-------+
 | ``'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 |       |
index 0e345e38de10f3c0db2284d5dfac20460f3243f6..cb9cf93b66d14768af214baf9bf3554b292bc82d 100644 (file)
@@ -429,13 +429,14 @@ The available presentation types for floating point and decimal values are:
    | ``'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    |
index cd46bc21c799665294adcad269befdf1040b5fdd..126867011f2874af319544655e6479245ace6aab 100644 (file)
@@ -88,6 +88,18 @@ class FormatTest(unittest.TestCase):
         testboth("%#.*F", (110, -1.e+100/3.))
         overflowrequired = 0
 
+        # check for %f and %F
+        testboth("%f", (1.0,), "1.000000")
+        testboth("%F", (1.0,), "1.000000")
+        testboth("%f", (1e100,), "1e+100")
+        testboth("%F", (1e100,), "1E+100")
+        testboth("%f", (1e100,), "1e+100")
+        testboth("%F", (1e100,), "1E+100")
+        testboth("%f", (float('nan'),), "nan")
+        testboth("%F", (float('nan'),), "NAN")
+        testboth("%f", (float('inf'),), "inf")
+        testboth("%F", (float('inf'),), "INF")
+
         # Formatting of long integers. Overflow is not ok
         overflowok = 0
         testboth("%x", 10L, "a")
index 03f2ff88d3730528c78ebaccb26da8e9e7dcf3b2..78106a3661f8c470b165cdd95794a4ef65785576 100644 (file)
@@ -587,9 +587,17 @@ class TypesTests(unittest.TestCase):
         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')
index 8b32d6172029e69e908358dcf0c4f43a0659651a..a7f371e781363bab9934896c13f665b2f9a59e0f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 2.6 beta 2?
 Core and Builtins
 -----------------
 
+- Issue #3382: Make '%F' and float.__format__('F') convert results
+  to uppercase.
+
 - Issue #3156: Fix inconsistent behavior of the bytearray type: all
   its methods now allow for items objects that can be converted to
   an integer using operator.index().
index b96443574594c2ec965f46b89caee4eb8b0ebcea..d8cb1715c4ebb541af165c69383ac60f389a5861 100644 (file)
@@ -741,10 +741,6 @@ format_float_internal(PyObject *value,
     /* 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 @@ format_float_internal(PyObject *value,
 
     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"
index 793cc8879360aa5594d9c2a9c094b2d6e29c50ac..b161c8cc696d694c60033f93794176997c7ca141 100644 (file)
@@ -4320,8 +4320,12 @@ formatfloat(char *buf, size_t buflen, int flags,
        }
        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:
@@ -4340,7 +4344,8 @@ formatfloat(char *buf, size_t buflen, int flags,
        */
        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;
@@ -4910,8 +4915,6 @@ PyString_Format(PyObject *format, PyObject *args)
                        case 'F':
                        case 'g':
                        case 'G':
-                               if (c == 'F')
-                                       c = 'f';
                                pbuf = formatbuf;
                                len = formatfloat(pbuf, sizeof(formatbuf),
                                                  flags, prec, c, v);
index a62e92908dc2577d53c95228e8301edb04815ddf..a885ff06205a1b1e9461b23e8b07f01696b0b896 100644 (file)
@@ -8191,8 +8191,12 @@ formatfloat(Py_UNICODE *buf,
        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:
@@ -8211,7 +8215,8 @@ formatfloat(Py_UNICODE *buf,
     */
     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;
@@ -8704,8 +8709,6 @@ PyObject *PyUnicode_Format(PyObject *format,
            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);