-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
bpo-44258: support PEP 515 for Fraction's initialization from string #26422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
aa8249a
8c96355
8c62c64
38b16a9
b131e51
db9652e
6336aef
b11ef1e
5f7e32a
983b67d
076b396
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,17 +21,17 @@ | |
| _PyHASH_INF = sys.hash_info.inf | ||
|
|
||
| _RATIONAL_FORMAT = re.compile(r""" | ||
| \A\s* # optional whitespace at the start, then | ||
| (?P<sign>[-+]?) # an optional sign, then | ||
| (?=\d|\.\d) # lookahead for digit or .digit | ||
| (?P<num>\d*) # numerator (possibly empty) | ||
| (?: # followed by | ||
| (?:/(?P<denom>\d+))? # an optional denominator | ||
| | # or | ||
| (?:\.(?P<decimal>\d*))? # an optional fractional part | ||
| (?:E(?P<exp>[-+]?\d+))? # and optional exponent | ||
| \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 | ||
| (?:/(?P<den>(\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 | ||
| \s*\Z # and optional whitespace to finish | ||
| """, re.VERBOSE | re.IGNORECASE) | ||
|
|
||
|
|
||
|
|
@@ -115,15 +115,16 @@ def __new__(cls, numerator=0, denominator=None, *, _normalize=True): | |
| raise ValueError('Invalid literal for Fraction: %r' % | ||
| numerator) | ||
| numerator = int(m.group('num') or '0') | ||
| denom = m.group('denom') | ||
| if denom: | ||
| denominator = int(denom) | ||
| denominator = m.group('den') | ||
|
||
| if denominator: | ||
| denominator = int(denominator) | ||
| else: | ||
| denominator = 1 | ||
| decimal = m.group('decimal') | ||
| if decimal: | ||
| scale = 10**len(decimal) | ||
| numerator = numerator * scale + int(decimal) | ||
| decimal = int(decimal) | ||
skirpichev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| scale = 10**len(str(decimal)) | ||
| numerator = numerator * scale + decimal | ||
| denominator *= scale | ||
| exp = m.group('exp') | ||
| if exp: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Support PEP 515 for Fraction's initialization from string. |
Uh oh!
There was an error while loading. Please reload this page.