diff Objects/complexobject.c @ 53787:c6376d3fa968

Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
author Eric Smith <eric@trueblade.com>
date Tue, 05 May 2009 14:04:18 +0000
parents e2b553d7205f
children adbc662ff4c1
line wrap: on
line diff
--- a/Objects/complexobject.c	Tue May 05 13:07:30 2009 +0000
+++ b/Objects/complexobject.c	Tue May 05 14:04:18 2009 +0000
@@ -330,7 +330,7 @@
 
 
 static PyObject *
-complex_format(PyComplexObject *v, char format_code)
+complex_format(PyComplexObject *v, int precision, char format_code)
 {
 	PyObject *result = NULL;
 	Py_ssize_t len;
@@ -350,7 +350,7 @@
 	if (v->cval.real == 0. && copysign(1.0, v->cval.real)==1.0) {
 		re = "";
 		im = PyOS_double_to_string(v->cval.imag, format_code,
-					   0, 0, NULL);
+					   precision, 0, NULL);
 		if (!im) {
 			PyErr_NoMemory();
 			goto done;
@@ -358,7 +358,7 @@
 	} else {
 		/* Format imaginary part with sign, real part without */
 		pre = PyOS_double_to_string(v->cval.real, format_code,
-					    0, 0, NULL);
+					    precision, 0, NULL);
 		if (!pre) {
 			PyErr_NoMemory();
 			goto done;
@@ -366,7 +366,7 @@
 		re = pre;
 
 		im = PyOS_double_to_string(v->cval.imag, format_code,
-					   0, Py_DTSF_SIGN, NULL);
+					   precision, Py_DTSF_SIGN, NULL);
 		if (!im) {
 			PyErr_NoMemory();
 			goto done;
@@ -395,13 +395,13 @@
 static PyObject *
 complex_repr(PyComplexObject *v)
 {
-    return complex_format(v, 'r');
+    return complex_format(v, 0, 'r');
 }
 
 static PyObject *
 complex_str(PyComplexObject *v)
 {
-    return complex_format(v, 's');
+    return complex_format(v, PyFloat_STR_PRECISION, 'g');
 }
 
 static long