diff options
author | Stefan Krah <skrah@bytereef.org> | 2014-08-26 18:46:49 (GMT) |
---|---|---|
committer | Stefan Krah <skrah@bytereef.org> | 2014-08-26 18:46:49 (GMT) |
commit | 298131a44896a4fec1ea829814ad52409d59aba5 (patch) | |
tree | 95f1a1123071fdbc602fe25e7514ccd09500a028 | |
parent | d84fd73de2823afd8da5c3e5937d51f7ecad8a34 (diff) | |
download | cpython-298131a44896a4fec1ea829814ad52409d59aba5.zip cpython-298131a44896a4fec1ea829814ad52409d59aba5.tar.gz cpython-298131a44896a4fec1ea829814ad52409d59aba5.tar.bz2 |
Issue #22090: Fix '%' formatting for infinities and NaNs.
-rw-r--r-- | Lib/decimal.py | 2 | ||||
-rw-r--r-- | Lib/test/test_decimal.py | 5 | ||||
-rw-r--r-- | Modules/_decimal/libmpdec/io.c | 11 |
3 files changed, 14 insertions, 4 deletions
diff --git a/Lib/decimal.py b/Lib/decimal.py index 5b98473..e11f1a0 100644 --- a/Lib/decimal.py +++ b/Lib/decimal.py @@ -3769,6 +3769,8 @@ class Decimal(object): if self._is_special: sign = _format_sign(self._sign, spec) body = str(self.copy_abs()) + if spec['type'] == '%': + body += '%' return _format_align(sign, body, spec) # a type of None defaults to 'g' or 'G', depending on context diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py index 8358ba6..b4c8c34 100644 --- a/Lib/test/test_decimal.py +++ b/Lib/test/test_decimal.py @@ -1057,6 +1057,11 @@ class FormatTest(unittest.TestCase): # issue 6850 ('a=-7.0', '0.12345', 'aaaa0.1'), + + # issue 22090 + ('<^+15.20%', 'inf', '<<+Infinity%<<<'), + ('\x07>,%', 'sNaN1234567', 'sNaN1234567%'), + ('=10.10%', 'NaN123', ' NaN123%'), ] for fmt, d, result in test_values: self.assertEqual(format(Decimal(d), fmt), result) diff --git a/Modules/_decimal/libmpdec/io.c b/Modules/_decimal/libmpdec/io.c index b67324d..a45a429 100644 --- a/Modules/_decimal/libmpdec/io.c +++ b/Modules/_decimal/libmpdec/io.c @@ -446,7 +446,7 @@ _mpd_to_string(char **result, const mpd_t *dec, int flags, mpd_ssize_t dplace) if (mpd_isspecial(dec)) { - mem = sizeof "-Infinity"; + mem = sizeof "-Infinity%"; if (mpd_isnan(dec) && dec->len > 0) { /* diagnostic code */ mem += dec->digits; @@ -609,10 +609,10 @@ _mpd_to_string(char **result, const mpd_t *dec, int flags, mpd_ssize_t dplace) *cp++ = (flags&MPD_FMT_UPPER) ? 'E' : 'e'; cp = exp_to_string(cp, ldigits-dplace); } + } - if (flags&MPD_FMT_PERCENT) { - *cp++ = '%'; - } + if (flags&MPD_FMT_PERCENT) { + *cp++ = '%'; } assert(cp < decstring+mem); @@ -1260,6 +1260,9 @@ mpd_qformat_spec(const mpd_t *dec, const mpd_spec_t *spec, stackspec.align = '>'; spec = &stackspec; } + if (type == '%') { + flags |= MPD_FMT_PERCENT; + } } else { uint32_t workstatus = 0; |