Index: Objects/unicodeobject.c =================================================================== --- Objects/unicodeobject.c (revision 54152) +++ Objects/unicodeobject.c (working copy) @@ -7276,7 +7276,8 @@ always given), therefore increase the length by one. */ - if ((type == 'g' && buflen <= (size_t)10 + (size_t)prec) || + if (((type == 'g' || type == 'G') && + buflen <= (size_t)10 + (size_t)prec) || (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { PyErr_SetString(PyExc_OverflowError, "formatted float is too long (precision too large?)"); Index: Objects/stringobject.c =================================================================== --- Objects/stringobject.c (revision 54152) +++ Objects/stringobject.c (working copy) @@ -4174,7 +4174,8 @@ always given), therefore increase the length by one. */ - if ((type == 'g' && buflen <= (size_t)10 + (size_t)prec) || + if (((type == 'g' || type == 'G') && + buflen <= (size_t)10 + (size_t)prec) || (type == 'f' && buflen <= (size_t)53 + (size_t)prec)) { PyErr_SetString(PyExc_OverflowError, "formatted float is too long (precision too large?)"); Index: Lib/test/test_format.py =================================================================== --- Lib/test/test_format.py (revision 54152) +++ Lib/test/test_format.py (working copy) @@ -7,6 +7,7 @@ # test on unicode strings as well overflowok = 1 +overflowrequired = 0 def testformat(formatstr, args, output=None): if verbose: @@ -23,15 +24,21 @@ if verbose: print('overflow (this is fine)') else: - if output and result != output: + if overflowrequired: if verbose: print('no') + print("overflow expected on %s %% %s" % \ + (repr(formatstr), repr(args))) + elif output and result != output: + if verbose: + print('no') print("%s %% %s == %s != %s" %\ - (repr(formatstr), repr(args), repr(result), repr(output))) + (repr(formatstr), repr(args), repr(result), repr(output))) else: if verbose: print('yes') + def testboth(formatstr, *args): testformat(formatstr, *args) if have_unicode: @@ -55,6 +62,14 @@ # test some ridiculously large precision, expect overflow testboth('%12.*f', (123456, 1.0)) +# check for internal overflow validation on length of precision +overflowrequired = 1 +testboth("%#.*g", (110, -1.e+100/3.)) +testboth("%#.*G", (110, -1.e+100/3.)) +testboth("%#.*f", (110, -1.e+100/3.)) +testboth("%#.*F", (110, -1.e+100/3.)) +overflowrequired = 0 + # Formatting of long integers. Overflow is not ok overflowok = 0 testboth("%x", 10, "a")