The other half of Issue #1580: use short float repr where possible.
Addresses the float -> string conversion, using David Gay's code which
was added in Mark Dickinson's checkin r71663.
Also addresses these, which are intertwined with the short repr
changes:
- Issue #5772: format(1e100, '<') produces '1e+100', not '1.0e+100'
- Issue #5515: 'n' formatting with commas no longer works poorly
with leading zeros.
- PEP 378 Format Specifier for Thousands Separator: implemented
for floats.
diff --git a/Python/marshal.c b/Python/marshal.c
index e5e5ce4..0d55132 100644
--- a/Python/marshal.c
+++ b/Python/marshal.c
@@ -236,12 +236,15 @@
w_string((char*)buf, 8, p);
}
else {
- char buf[256]; /* Plenty to format any double */
- n = _PyFloat_Repr(PyFloat_AS_DOUBLE(v),
- buf, sizeof(buf));
+ char *buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v),
+ 'r', 0, 0, NULL);
+ if (!buf)
+ return;
+ n = strlen(buf);
w_byte(TYPE_FLOAT, p);
w_byte((int)n, p);
w_string(buf, (int)n, p);
+ PyMem_Free(buf);
}
}
#ifndef WITHOUT_COMPLEX
@@ -263,17 +266,24 @@
w_string((char*)buf, 8, p);
}
else {
- char buf[256]; /* Plenty to format any double */
+ char *buf;
w_byte(TYPE_COMPLEX, p);
- n = _PyFloat_Repr(PyComplex_RealAsDouble(v),
- buf, sizeof(buf));
+ buf = PyOS_double_to_string(PyComplex_RealAsDouble(v),
+ 'r', 0, 0, NULL);
+ if (!buf)
+ return;
n = strlen(buf);
w_byte((int)n, p);
w_string(buf, (int)n, p);
- n = _PyFloat_Repr(PyComplex_ImagAsDouble(v),
- buf, sizeof(buf));
+ PyMem_Free(buf);
+ buf = PyOS_double_to_string(PyComplex_ImagAsDouble(v),
+ 'r', 0, 0, NULL);
+ if (!buf)
+ return;
+ n = strlen(buf);
w_byte((int)n, p);
w_string(buf, (int)n, p);
+ PyMem_Free(buf);
}
}
#endif