Open
Description
Bug report
Bug description:
Currently, specification allows only [0-9]
digits. Though, actual implementation permits unicode symbols for float/Decimal's, but not Fraction's:
>>> f"{decimal.Decimal('123'):.١١f}" # arabic 11 in precision
'123.00000000000'
>>> f"{fractions.Fraction('123'):.١١f}"
Traceback (most recent call last):
File "<python-input-9>", line 1, in <module>
f"{fractions.Fraction('123'):.١١f}"
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/sk/src/cpython/Lib/fractions.py", line 600, in __format__
raise ValueError(
...<2 lines>...
)
ValueError: Invalid format specifier '.١١f' for object of type 'Fraction'
>>> f"{float(fractions.Fraction('123')):.١١f}"
'123.00000000000'
Quick tests shows no measurable performance penalty with unicode support:
$ python -m timeit -s 'from fractions import Fraction as F' 'format(F(123), ".11f")'
10000 loops, best of 5: 39.2 usec per loop
$ python -m timeit -s 'from fractions import Fraction as F' 'format(F(123), ".١١f")' # with patch
5000 loops, best of 5: 40.2 usec per loop
a patch
diff --git a/Lib/fractions.py b/Lib/fractions.py
index 063f28478c..b4120b2beb 100644
--- a/Lib/fractions.py
+++ b/Lib/fractions.py
@@ -170,7 +170,7 @@ def _round_to_figures(n, d, figures):
(?P<zeropad>0(?=[0-9]))?
(?P<minimumwidth>0|[1-9][0-9]*)?
(?P<thousands_sep>[,_])?
- (?:\.(?P<precision>0|[1-9][0-9]*))?
+ (?:\.(?P<precision>0|\d*))?
(?P<presentation_type>[eEfFgG%])
""", re.DOTALL | re.VERBOSE).fullmatch
CPython versions tested on:
CPython main branch
Operating systems tested on:
No response