Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Closed
Description
Bug description:
Thefractions.Fraction() error message differs when, for example,123.dd is done instead of123.aa; they should be equal.
>>>from fractionsimport 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.
Lines 57 to 69 indac1da2
| _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