From cf63f2fb88826ae67843b8574bca6ef25c2e791f Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Wed, 22 Apr 2009 17:50:21 +0000 Subject: Issue #5812: Make Fraction('1e6') valid. The Fraction constructor now accepts all strings accepted by the float and Decimal constructors, with the exception of strings representing NaNs or infinities. --- Lib/fractions.py | 45 ++++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 19 deletions(-) (limited to 'Lib/fractions.py') diff --git a/Lib/fractions.py b/Lib/fractions.py index ed1e9a0c10..5242f8f88c 100755 --- a/Lib/fractions.py +++ b/Lib/fractions.py @@ -28,13 +28,14 @@ _RATIONAL_FORMAT = re.compile(r""" (?P[-+]?) # an optional sign, then (?=\d|\.\d) # lookahead for digit or .digit (?P\d*) # numerator (possibly empty) - (?: # followed by an optional - /(?P\d+) # / and denominator + (?: # followed by + (?:/(?P\d+))? # an optional denominator | # or - \.(?P\d*) # decimal point and fractional part - )? + (?:\.(?P\d*))? # an optional fractional part + (?:E(?P[-+]?\d+))? # and optional exponent + ) \s*\Z # and optional whitespace to finish -""", re.VERBOSE) +""", re.VERBOSE | re.IGNORECASE) class Fraction(numbers.Rational): @@ -65,22 +66,28 @@ class Fraction(numbers.Rational): if not isinstance(numerator, int) and denominator == 1: if isinstance(numerator, str): # Handle construction from strings. - input = numerator - m = _RATIONAL_FORMAT.match(input) + m = _RATIONAL_FORMAT.match(numerator) if m is None: - raise ValueError('Invalid literal for Fraction: %r' % input) - numerator = m.group('num') - decimal = m.group('decimal') - if decimal: - # The literal is a decimal number. - numerator = int(numerator + decimal) - denominator = 10**len(decimal) + raise ValueError('Invalid literal for Fraction: %r' % + numerator) + numerator = int(m.group('num') or '0') + denom = m.group('denom') + if denom: + denominator = int(denom) else: - # The literal is an integer or fraction. - numerator = int(numerator) - # Default denominator to 1. - denominator = int(m.group('denom') or 1) - + denominator = 1 + decimal = m.group('decimal') + if decimal: + scale = 10**len(decimal) + numerator = numerator * scale + int(decimal) + denominator *= scale + exp = m.group('exp') + if exp: + exp = int(exp) + if exp >= 0: + numerator *= 10**exp + else: + denominator *= 10**-exp if m.group('sign') == '-': numerator = -numerator -- cgit v1.2.1