Skip to content

Commit a0a218f

Browse files
author
eric.smith
committed
Issue 3382: Make '%F' and float.__format__('F') convert results to upper case.
git-svn-id: http://svn.python.org/projects/python/trunk@65069 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent d2cf4ba commit a0a218f

8 files changed

Lines changed: 53 additions & 23 deletions

File tree

Doc/library/stdtypes.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,9 +1296,9 @@ The conversion types are:
12961296
+------------+-----------------------------------------------------+-------+
12971297
| ``'E'`` | Floating point exponential format (uppercase). | \(3) |
12981298
+------------+-----------------------------------------------------+-------+
1299-
| ``'f'`` | Floating point decimal format. | \(3) |
1299+
| ``'f'`` | Floating point decimal format (lowercase). | \(3) |
13001300
+------------+-----------------------------------------------------+-------+
1301-
| ``'F'`` | Floating point decimal format. | \(3) |
1301+
| ``'F'`` | Floating point decimal format (uppercase). | \(3) |
13021302
+------------+-----------------------------------------------------+-------+
13031303
| ``'g'`` | Floating point format. Uses lowercase exponential | \(4) |
13041304
| | format if exponent is less than -4 or not less than | |

Doc/library/string.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -429,13 +429,14 @@ The available presentation types for floating point and decimal values are:
429429
| ``'e'`` | Exponent notation. Prints the number in scientific |
430430
| | notation using the letter 'e' to indicate the exponent. |
431431
+---------+----------------------------------------------------------+
432-
| ``'E'`` | Exponent notation. Same as ``'e'`` except it uses an |
433-
| | upper case 'E' as the separator character. |
432+
| ``'E'`` | Exponent notation. Same as ``'e'`` except it converts |
433+
| | the number to upper case. |
434434
+---------+----------------------------------------------------------+
435435
| ``'f'`` | Fixed point. Displays the number as a fixed-point |
436436
| | number. |
437437
+---------+----------------------------------------------------------+
438-
| ``'F'`` | Fixed point. Same as ``'f'``. |
438+
| ``'F'`` | Fixed point. Same as ``'f'`` except it converts the |
439+
| | number to upper case. |
439440
+---------+----------------------------------------------------------+
440441
| ``'g'`` | General format. This prints the number as a fixed-point |
441442
| | number, unless the number is too large, in which case |

Lib/test/test_format.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,18 @@ def test_format(self):
8888
testboth("%#.*F", (110, -1.e+100/3.))
8989
overflowrequired = 0
9090

91+
# check for %f and %F
92+
testboth("%f", (1.0,), "1.000000")
93+
testboth("%F", (1.0,), "1.000000")
94+
testboth("%f", (1e100,), "1e+100")
95+
testboth("%F", (1e100,), "1E+100")
96+
testboth("%f", (1e100,), "1e+100")
97+
testboth("%F", (1e100,), "1E+100")
98+
testboth("%f", (float('nan'),), "nan")
99+
testboth("%F", (float('nan'),), "NAN")
100+
testboth("%f", (float('inf'),), "inf")
101+
testboth("%F", (float('inf'),), "INF")
102+
91103
# Formatting of long integers. Overflow is not ok
92104
overflowok = 0
93105
testboth("%x", 10L, "a")

Lib/test/test_types.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,9 +587,17 @@ def test(f, format_spec, result):
587587
test( 1.0, '+f', '+1.000000')
588588
test(-1.0, '+f', '-1.000000')
589589
test(1.1234e90, 'f', '1.1234e+90')
590-
test(1.1234e90, 'F', '1.1234e+90')
590+
test(1.1234e90, 'F', '1.1234E+90')
591591
test(1.1234e200, 'f', '1.1234e+200')
592-
test(1.1234e200, 'F', '1.1234e+200')
592+
test(1.1234e200, 'F', '1.1234E+200')
593+
test(1e100, 'x<20f', '1e+100xxxxxxxxxxxxxx')
594+
test(1e100, 'x<20F', '1E+100xxxxxxxxxxxxxx')
595+
test(float('nan'), 'f', 'nan')
596+
test(float('nan'), 'F', 'NAN')
597+
test(float('inf'), 'f', 'inf')
598+
test(float('inf'), 'F', 'INF')
599+
test(float('-inf'), 'f', '-inf')
600+
test(float('-inf'), 'F', '-INF')
593601

594602
test( 1.0, 'e', '1.000000e+00')
595603
test(-1.0, 'e', '-1.000000e+00')

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 2.6 beta 2?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #3382: Make '%F' and float.__format__('F') convert results
14+
to uppercase.
15+
1316
- Issue #3156: Fix inconsistent behavior of the bytearray type: all
1417
its methods now allow for items objects that can be converted to
1518
an integer using operator.index().

Objects/stringlib/formatter.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -741,10 +741,6 @@ format_float_internal(PyObject *value,
741741
/* first, do the conversion as 8-bit chars, using the platform's
742742
snprintf. then, if needed, convert to unicode. */
743743

744-
/* 'F' is the same as 'f', per the PEP */
745-
if (type == 'F')
746-
type = 'f';
747-
748744
x = PyFloat_AsDouble(value);
749745

750746
if (x == -1.0 && PyErr_Occurred())
@@ -758,8 +754,12 @@ format_float_internal(PyObject *value,
758754

759755
if (precision < 0)
760756
precision = 6;
761-
if (type == 'f' && (fabs(x) / 1e25) >= 1e25)
762-
type = 'g';
757+
if ((type == 'f' || type == 'F') && (fabs(x) / 1e25) >= 1e25) {
758+
if (type == 'f')
759+
type = 'g';
760+
else
761+
type = 'G';
762+
}
763763

764764
/* cast "type", because if we're in unicode we need to pass a
765765
8-bit char. this is safe, because we've restricted what "type"

Objects/stringobject.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4320,8 +4320,12 @@ formatfloat(char *buf, size_t buflen, int flags,
43204320
}
43214321
if (prec < 0)
43224322
prec = 6;
4323-
if (type == 'f' && fabs(x)/1e25 >= 1e25)
4324-
type = 'g';
4323+
if ((type == 'f' || type == 'F') && (fabs(x) / 1e25) >= 1e25) {
4324+
if (type == 'f')
4325+
type = 'g';
4326+
else
4327+
type = 'G';
4328+
}
43254329
/* Worst case length calc to ensure no buffer overrun:
43264330
43274331
'g' formats:
@@ -4340,7 +4344,8 @@ formatfloat(char *buf, size_t buflen, int flags,
43404344
*/
43414345
if (((type == 'g' || type == 'G') &&
43424346
buflen <= (size_t)10 + (size_t)prec) ||
4343-
(type == 'f' && buflen <= (size_t)53 + (size_t)prec)) {
4347+
((type == 'f' || type == 'F') &&
4348+
buflen <= (size_t)53 + (size_t)prec)) {
43444349
PyErr_SetString(PyExc_OverflowError,
43454350
"formatted float is too long (precision too large?)");
43464351
return -1;
@@ -4910,8 +4915,6 @@ PyString_Format(PyObject *format, PyObject *args)
49104915
case 'F':
49114916
case 'g':
49124917
case 'G':
4913-
if (c == 'F')
4914-
c = 'f';
49154918
pbuf = formatbuf;
49164919
len = formatfloat(pbuf, sizeof(formatbuf),
49174920
flags, prec, c, v);

Objects/unicodeobject.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8191,8 +8191,12 @@ formatfloat(Py_UNICODE *buf,
81918191
return -1;
81928192
if (prec < 0)
81938193
prec = 6;
8194-
if (type == 'f' && (fabs(x) / 1e25) >= 1e25)
8195-
type = 'g';
8194+
if ((type == 'f' || type == 'F') && (fabs(x) / 1e25) >= 1e25) {
8195+
if (type == 'f')
8196+
type = 'g';
8197+
else
8198+
type = 'G';
8199+
}
81968200
/* Worst case length calc to ensure no buffer overrun:
81978201
81988202
'g' formats:
@@ -8211,7 +8215,8 @@ formatfloat(Py_UNICODE *buf,
82118215
*/
82128216
if (((type == 'g' || type == 'G') &&
82138217
buflen <= (size_t)10 + (size_t)prec) ||
8214-
(type == 'f' && buflen <= (size_t)53 + (size_t)prec)) {
8218+
((type == 'f' || type == 'F') &&
8219+
buflen <= (size_t)53 + (size_t)prec)) {
82158220
PyErr_SetString(PyExc_OverflowError,
82168221
"formatted float is too long (precision too large?)");
82178222
return -1;
@@ -8704,8 +8709,6 @@ PyObject *PyUnicode_Format(PyObject *format,
87048709
case 'F':
87058710
case 'g':
87068711
case 'G':
8707-
if (c == 'F')
8708-
c = 'f';
87098712
pbuf = formatbuf;
87108713
len = formatfloat(pbuf, sizeof(formatbuf)/sizeof(Py_UNICODE),
87118714
flags, prec, c, v);

0 commit comments

Comments
 (0)