Issue #22090: Fix '%' formatting for infinities and NaNs. · python/cpython@298131a (original) (raw)
3 files changed
lines changed
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -3769,6 +3769,8 @@ def __format__(self, specifier, context=None, _localeconv=None): | ||
3769 | 3769 | if self._is_special: |
3770 | 3770 | sign = _format_sign(self._sign, spec) |
3771 | 3771 | body = str(self.copy_abs()) |
3772 | +if spec['type'] == '%': | |
3773 | +body += '%' | |
3772 | 3774 | return _format_align(sign, body, spec) |
3773 | 3775 | |
3774 | 3776 | # a type of None defaults to 'g' or 'G', depending on context |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1057,6 +1057,11 @@ def test_formatting(self): | ||
1057 | 1057 | |
1058 | 1058 | # issue 6850 |
1059 | 1059 | ('a=-7.0', '0.12345', 'aaaa0.1'), |
1060 | + | |
1061 | +# issue 22090 | |
1062 | + ('<^+15.20%', 'inf', '<<+Infinity%<<<'), | |
1063 | + ('\x07>,%', 'sNaN1234567', 'sNaN1234567%'), | |
1064 | + ('=10.10%', 'NaN123', ' NaN123%'), | |
1060 | 1065 | ] |
1061 | 1066 | for fmt, d, result in test_values: |
1062 | 1067 | self.assertEqual(format(Decimal(d), fmt), result) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -446,7 +446,7 @@ _mpd_to_string(char **result, const mpd_t *dec, int flags, mpd_ssize_t dplace) | ||
446 | 446 | |
447 | 447 | if (mpd_isspecial(dec)) { |
448 | 448 | |
449 | -mem = sizeof "-Infinity"; | |
449 | +mem = sizeof "-Infinity%"; | |
450 | 450 | if (mpd_isnan(dec) && dec->len > 0) { |
451 | 451 | /* diagnostic code */ |
452 | 452 | mem += dec->digits; |
@@ -609,10 +609,10 @@ _mpd_to_string(char **result, const mpd_t *dec, int flags, mpd_ssize_t dplace) | ||
609 | 609 | *cp++ = (flags&MPD_FMT_UPPER) ? 'E' : 'e'; |
610 | 610 | cp = exp_to_string(cp, ldigits-dplace); |
611 | 611 | } |
612 | + } | |
612 | 613 | |
613 | -if (flags&MPD_FMT_PERCENT) { | |
614 | -*cp++ = '%'; | |
615 | - } | |
614 | +if (flags&MPD_FMT_PERCENT) { | |
615 | +*cp++ = '%'; | |
616 | 616 | } |
617 | 617 | |
618 | 618 | assert(cp < decstring+mem); |
@@ -1260,6 +1260,9 @@ mpd_qformat_spec(const mpd_t *dec, const mpd_spec_t *spec, | ||
1260 | 1260 | stackspec.align = '>'; |
1261 | 1261 | spec = &stackspec; |
1262 | 1262 | } |
1263 | +if (type == '%') { | |
1264 | +flags |= MPD_FMT_PERCENT; | |
1265 | + } | |
1263 | 1266 | } |
1264 | 1267 | else { |
1265 | 1268 | uint32_t workstatus = 0; |