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/Modules/_pickle.c b/Modules/_pickle.c
index bda8efd..20ab525 100644
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -1016,16 +1016,31 @@
return -1;
if (pickler_write(self, pdata, 9) < 0)
return -1;
- }
+ }
else {
- char pdata[250];
- pdata[0] = FLOAT;
- PyOS_ascii_formatd(pdata + 1, sizeof(pdata) - 2, "%.17g", x);
- /* Extend the formatted string with a newline character */
- strcat(pdata, "\n");
+ int result = -1;
+ char *buf = NULL;
+ char op = FLOAT;
- if (pickler_write(self, pdata, strlen(pdata)) < 0)
- return -1;
+ if (pickler_write(self, &op, 1) < 0)
+ goto done;
+
+ buf = PyOS_double_to_string(x, 'r', 0, 0, NULL);
+ if (!buf) {
+ PyErr_NoMemory();
+ goto done;
+ }
+
+ if (pickler_write(self, buf, strlen(buf)) < 0)
+ goto done;
+
+ if (pickler_write(self, "\n", 1) < 0)
+ goto done;
+
+ result = 0;
+done:
+ PyMem_Free(buf);
+ return result;
}
return 0;