[4.2.x] Fixed #34363 -- Fixed floatformat crash on zero with trailing zeros.

Regression in 08c5a78726.
Follow up to 4b066bde69.
Backport of dcd9746983 from main
This commit is contained in:
Panagiotis H.M. Issaris 2023-02-22 20:46:16 +01:00 committed by Mariusz Felisiak
parent 48b2ba8a55
commit ce69dba000
2 changed files with 6 additions and 1 deletions

View File

@ -3,7 +3,7 @@ import random as random_module
import re import re
import types import types
import warnings import warnings
from decimal import ROUND_HALF_UP, Context, Decimal, InvalidOperation from decimal import ROUND_HALF_UP, Context, Decimal, InvalidOperation, getcontext
from functools import wraps from functools import wraps
from inspect import unwrap from inspect import unwrap
from operator import itemgetter from operator import itemgetter
@ -184,6 +184,7 @@ def floatformat(text, arg=-1):
units = len(tupl[1]) units = len(tupl[1])
units += -tupl[2] if m else tupl[2] units += -tupl[2] if m else tupl[2]
prec = abs(p) + units + 1 prec = abs(p) + units + 1
prec = max(getcontext().prec, prec)
# Avoid conversion to scientific notation by accessing `sign`, `digits`, # Avoid conversion to scientific notation by accessing `sign`, `digits`,
# and `exponent` from Decimal.as_tuple() directly. # and `exponent` from Decimal.as_tuple() directly.

View File

@ -113,6 +113,10 @@ class FunctionTests(SimpleTestCase):
) )
self.assertEqual(floatformat("0.00", 0), "0") self.assertEqual(floatformat("0.00", 0), "0")
self.assertEqual(floatformat(Decimal("0.00"), 0), "0") self.assertEqual(floatformat(Decimal("0.00"), 0), "0")
self.assertEqual(floatformat("0.0000", 2), "0.00")
self.assertEqual(floatformat(Decimal("0.0000"), 2), "0.00")
self.assertEqual(floatformat("0.000000", 4), "0.0000")
self.assertEqual(floatformat(Decimal("0.000000"), 4), "0.0000")
def test_negative_zero_values(self): def test_negative_zero_values(self):
tests = [ tests = [