summaryrefslogtreecommitdiffstats
path: root/Modules/_pickle.c
diff options
context:
space:
mode:
authorEric Smith <eric@trueblade.com>2009-04-16 20:16:10 (GMT)
committerEric Smith <eric@trueblade.com>2009-04-16 20:16:10 (GMT)
commit0923d1d8d7e428297461ed5145f06915c462b25b (patch)
treeb1fee964b1030c99285ae9d95e7e4dfb60dcded0 /Modules/_pickle.c
parentb08a53a99def3fa949643974f713b5b189e21bc7 (diff)
downloadcpython-0923d1d8d7e428297461ed5145f06915c462b25b.zip
cpython-0923d1d8d7e428297461ed5145f06915c462b25b.tar.gz
cpython-0923d1d8d7e428297461ed5145f06915c462b25b.tar.bz2
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.
Diffstat (limited to 'Modules/_pickle.c')
-rw-r--r--Modules/_pickle.c31
1 files changed, 23 insertions, 8 deletions
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 @@ save_float(PicklerObject *self, PyObject *obj)
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;