summaryrefslogtreecommitdiffstats
path: root/Python/dtoa.c
diff options
context:
space:
mode:
authorMark Dickinson <mdickinson@enthought.com>2020-05-29 13:23:57 (GMT)
committerGitHub <noreply@github.com>2020-05-29 13:23:57 (GMT)
commit895c9c1d438367722f74f437fda96767d770662b (patch)
tree8b4a46c98dda92716458b5d2f24ea907b736e577 /Python/dtoa.c
parent28316422124206f63ddd4b91f2e19c54b6e9cd9d (diff)
downloadcpython-895c9c1d438367722f74f437fda96767d770662b.zip
cpython-895c9c1d438367722f74f437fda96767d770662b.tar.gz
cpython-895c9c1d438367722f74f437fda96767d770662b.tar.bz2
bpo-40780: Fix failure of _Py_dg_dtoa to remove trailing zeros (GH-20435)
* Fix failure of _Py_dg_dtoa to remove trailing zeros * Add regression test and news entry * Add explanation about why it's safe to strip trailing zeros * Make code safer, clean up comments, add change note at top of file * Nitpick: avoid implicit int-to-float conversion in tests
Diffstat (limited to 'Python/dtoa.c')
-rw-r--r--Python/dtoa.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/Python/dtoa.c b/Python/dtoa.c
index 822adc6..e629b29 100644
--- a/Python/dtoa.c
+++ b/Python/dtoa.c
@@ -64,6 +64,9 @@
* 7. _Py_dg_strtod has been modified so that it doesn't accept strings with
* leading whitespace.
*
+ * 8. A corner case where _Py_dg_dtoa didn't strip trailing zeros has been
+ * fixed. (bugs.python.org/issue40780)
+ *
***************************************************************/
/* Please send bug reports for the original dtoa.c code to David M. Gay (dmg
@@ -2563,6 +2566,14 @@ _Py_dg_dtoa(double dd, int mode, int ndigits,
}
++*s++;
}
+ else {
+ /* Strip trailing zeros. This branch was missing from the
+ original dtoa.c, leading to surplus trailing zeros in
+ some cases. See bugs.python.org/issue40780. */
+ while (s > s0 && s[-1] == '0') {
+ --s;
+ }
+ }
break;
}
}