Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Inconsistency infractions.Fraction() initialization error with invalid string #114014

Closed
Labels
stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
@thatbirdguythatuknownot

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.

_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 ofd* instead of\d*.

CPython versions tested on:

3.13

Operating systems tested on:

Windows

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp