From 454816d8bdd6ec9bb986aa31760518e622dc1ea7 Mon Sep 17 00:00:00 2001 From: Eric Smith Date: Thu, 17 Jul 2008 17:48:39 +0000 Subject: Issue 3382: Make '%F' and float.__format__('F') convert results to upper case. --- Doc/library/stdtypes.rst | 4 ++-- Doc/library/string.rst | 7 ++++--- Lib/test/test_format.py | 12 ++++++++++++ Lib/test/test_types.py | 12 ++++++++++-- Misc/NEWS | 3 +++ Objects/stringlib/formatter.h | 12 ++++++------ Objects/stringobject.c | 13 ++++++++----- Objects/unicodeobject.c | 13 ++++++++----- 8 files changed, 53 insertions(+), 23 deletions(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index e857154..3c0aa18 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -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 | | diff --git a/Doc/library/string.rst b/Doc/library/string.rst index 0e345e3..cb9cf93 100644 --- a/Doc/library/string.rst +++ b/Doc/library/string.rst @@ -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 | diff --git a/Lib/test/test_format.py b/Lib/test/test_format.py index cd46bc2..1268670 100644 --- a/Lib/test/test_format.py +++ b/Lib/test/test_format.py @@ -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") diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py index 03f2ff8..78106a3 100644 --- a/Lib/test/test_types.py +++ b/Lib/test/test_types.py @@ -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') diff --git a/Misc/NEWS b/Misc/NEWS index 8b32d61..a7f371e 100644 --- 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(). diff --git a/Objects/stringlib/formatter.h b/Objects/stringlib/formatter.h index b964435..d8cb171 100644 --- a/Objects/stringlib/formatter.h +++ b/Objects/stringlib/formatter.h @@ -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" diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 793cc88..b161c8c 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -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); diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index a62e929..a885ff0 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -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); -- cgit v0.12