Index: Python/pystrtod.c =================================================================== --- Python/pystrtod.c (revision 72259) +++ Python/pystrtod.c (working copy) @@ -755,7 +755,7 @@ PyErr_BadInternalCall(); return NULL; } - precision = 12; + precision = PyFloat_STR_PRECISION; format_code = 'g'; break; default: @@ -1228,7 +1228,7 @@ PyErr_BadInternalCall(); return NULL; } - precision = 12; + precision = PyFloat_STR_PRECISION; break; default: Index: Include/floatobject.h =================================================================== --- Include/floatobject.h (revision 72258) +++ Include/floatobject.h (working copy) @@ -21,6 +21,8 @@ #define PyFloat_Check(op) PyObject_TypeCheck(op, &PyFloat_Type) #define PyFloat_CheckExact(op) (Py_TYPE(op) == &PyFloat_Type) +#define PyFloat_STR_PRECISION 12 + #ifdef Py_NAN #define Py_RETURN_NAN return PyFloat_FromDouble(Py_NAN) #endif Index: Objects/stringlib/formatter.h =================================================================== --- Objects/stringlib/formatter.h (revision 72258) +++ Objects/stringlib/formatter.h (working copy) @@ -881,6 +881,7 @@ int has_decimal; double val; Py_ssize_t precision = format->precision; + Py_ssize_t default_precision = 6; STRINGLIB_CHAR type = format->type; int add_pct = 0; STRINGLIB_CHAR *p; @@ -910,6 +911,7 @@ /* Omitted type specifier. This is like 'g' but with at least one digit after the decimal point. */ type = 'g'; + default_precision = PyFloat_STR_PRECISION; flags |= Py_DTSF_ADD_DOT_0; } @@ -933,7 +935,7 @@ } if (precision < 0) - precision = 6; + precision = default_precision; #if PY_VERSION_HEX < 0x03010000 /* 3.1 no longer converts large 'f' to 'g'. */ @@ -1039,6 +1041,7 @@ int re_has_decimal; int im_has_decimal; Py_ssize_t precision = format->precision; + Py_ssize_t default_precision = 6; STRINGLIB_CHAR type = format->type; STRINGLIB_CHAR *p_re; STRINGLIB_CHAR *p_im; @@ -1100,6 +1103,7 @@ if (type == '\0') { /* Omitted type specifier. Should be like str(self). */ type = 'g'; + default_precision = PyFloat_STR_PRECISION; add_parens = 1; if (re == 0.0) skip_re = 1; @@ -1115,7 +1119,7 @@ type = 'f'; if (precision < 0) - precision = 6; + precision = default_precision; /* 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: Lib/test/test_complex.py =================================================================== --- Lib/test/test_complex.py (revision 72258) +++ Lib/test/test_complex.py (working copy) @@ -445,6 +445,16 @@ self.assertEqual(format(3+0j, ''), str(3+0j)) self.assertEqual(format(3.2+0j, ''), str(3.2+0j)) + # empty presentation type should still be analogous to str, + # even when format string is nonempty (issue #5920). + self.assertEqual(format(3.2+0j, '-'), str(3.2+0j)) + self.assertEqual(format(3.2+0j, '<'), str(3.2+0j)) + z = 4/7. - 100j/7. + self.assertEqual(format(z, ''), str(z)) + self.assertEqual(format(z, '-'), str(z)) + self.assertEqual(format(z, '<'), str(z)) + self.assertEqual(format(z, '10'), str(z)) + self.assertEqual(format(1+3j, 'g'), '1+3j') self.assertEqual(format(3j, 'g'), '0+3j') self.assertEqual(format(1.5+3.5j, 'g'), '1.5+3.5j') Index: Lib/test/test_float.py =================================================================== --- Lib/test/test_float.py (revision 72258) +++ Lib/test/test_float.py (working copy) @@ -284,6 +284,13 @@ self.assertEqual(format(0.01, ''), '0.01') self.assertEqual(format(0.01, 'g'), '0.01') + # empty presentation type should format in the same way as str + # (issue 5920) + x = 100/7. + self.assertEqual(format(x, ''), str(x)) + self.assertEqual(format(x, '-'), str(x)) + self.assertEqual(format(x, '>'), str(x)) + self.assertEqual(format(x, '2'), str(x)) self.assertEqual(format(1.0, 'f'), '1.000000')