Bug description:
The fractions.Fraction() error message differs when, for example, 123.dd is done instead of 123.aa; they should be equal.
>>> from fractions import Fraction
>>> Fraction("123.dd")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Fraction("123.dd")
File "C:\Program Files\Python313\Lib\fractions.py", line 251, in __new__
numerator = numerator * scale + int(decimal)
^^^^^^^^^^^^
ValueError: invalid literal for int() with base 10: 'dd'
>>> Fraction("123.aa")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Fraction("123.aa")
File "C:\Program Files\Python313\Lib\fractions.py", line 239, in __new__
raise ValueError('Invalid literal for Fraction: %r' %
ValueError: Invalid literal for Fraction: '123.aa'
I discovered this bug while inspecting the rational parsing regex.
|
_RATIONAL_FORMAT = re.compile(r""" |
|
\A\s* # optional whitespace at the start, |
|
(?P<sign>[-+]?) # an optional sign, then |
|
(?=\d|\.\d) # lookahead for digit or .digit |
|
(?P<num>\d*|\d+(_\d+)*) # numerator (possibly empty) |
|
(?: # followed by |
|
(?:\s*/\s*(?P<denom>\d+(_\d+)*))? # an optional denominator |
|
| # or |
|
(?:\.(?P<decimal>d*|\d+(_\d+)*))? # an optional fractional part |
|
(?:E(?P<exp>[-+]?\d+(_\d+)*))? # and optional exponent |
|
) |
|
\s*\Z # and optional whitespace to finish |
|
""", re.VERBOSE | re.IGNORECASE) |
I think the bug stems from line 65's matching of d* instead of \d*.
CPython versions tested on:
3.13
Operating systems tested on:
Windows
Linked PRs
Reactions are currently unavailable
Bug description:
The fractions.Fraction() error message differs when, for example, 123.dd is done instead of 123.aa; they should be equal.
I discovered this bug while inspecting the rational parsing regex.
cpython/Lib/fractions.py
Lines 57 to 69 in dac1da2
I think the bug stems from line 65's matching of d* instead of \d*.
CPython versions tested on:
3.13
Operating systems tested on:
Windows
Linked PRs