]> granicus.if.org Git - python/commitdiff
Issue #5812: make Fraction('1e-6') valid. Backport of r71806.
authorMark Dickinson <dickinsm@gmail.com>
Wed, 22 Apr 2009 18:15:25 +0000 (18:15 +0000)
committerMark Dickinson <dickinsm@gmail.com>
Wed, 22 Apr 2009 18:15:25 +0000 (18:15 +0000)
Doc/library/fractions.rst
Lib/fractions.py
Lib/test/test_fractions.py
Misc/NEWS

index 36df11cc431236203d3d5e73a44ff627073579fe..5e9758efacbe13ba3eb3b69374cfcaa3891692f0 100644 (file)
@@ -27,20 +27,17 @@ another rational number, or from a string.
    *other_fraction* is an instance of :class:`numbers.Rational` and
    returns an :class:`Fraction` instance with the same value.  The
    last version of the constructor expects a string or unicode
-   instance in one of two possible forms.  The first form is::
+   instance.  The usual form for this instance is::
 
       [sign] numerator ['/' denominator]
 
    where the optional ``sign`` may be either '+' or '-' and
    ``numerator`` and ``denominator`` (if present) are strings of
-   decimal digits.  The second permitted form is that of a number
-   containing a decimal point::
-
-      [sign] integer '.' [fraction] | [sign] '.' fraction
-
-   where ``integer`` and ``fraction`` are strings of digits.  In
-   either form the input string may also have leading and/or trailing
-   whitespace.  Here are some examples::
+   decimal digits.  In addition, any string that represents a finite
+   value and is accepted by the :class:`float` constructor is also
+   accepted by the :class:`Fraction` constructor.  In either form the
+   input string may also have leading and/or trailing whitespace.
+   Here are some examples::
 
       >>> from fractions import Fraction
       >>> Fraction(16, -10)
@@ -58,6 +55,8 @@ another rational number, or from a string.
       Fraction(1414213, 1000000)
       >>> Fraction('-.125')
       Fraction(-1, 8)
+      >>> Fraction('7e-6')
+      Fraction(7, 1000000)
 
 
    The :class:`Fraction` class inherits from the abstract base class
index 446ad8ea8279a1c754c1055e0b2c411731861fcb..7db6b5b1f87d425075ed108f8dcacbee2d92d8c8 100755 (executable)
@@ -30,13 +30,14 @@ _RATIONAL_FORMAT = re.compile(r"""
     (?P<sign>[-+]?)            # an optional sign, then
     (?=\d|\.\d)                # lookahead for digit or .digit
     (?P<num>\d*)               # numerator (possibly empty)
-    (?:                        # followed by an optional
-       /(?P<denom>\d+)         # / and denominator
+    (?:                        # followed by
+       (?:/(?P<denom>\d+))?    # an optional denominator
     |                          # or
-       \.(?P<decimal>\d*)      # decimal point and fractional part
-    )?
+       (?:\.(?P<decimal>\d*))? # an optional fractional part
+       (?:E(?P<exp>[-+]?\d+))? # and optional exponent
+    )
     \s*\Z                      # and optional whitespace to finish
-""", re.VERBOSE)
+""", re.VERBOSE | re.IGNORECASE)
 
 
 class Fraction(Rational):
@@ -67,22 +68,28 @@ class Fraction(Rational):
         if type(numerator) not in (int, long) and denominator == 1:
             if isinstance(numerator, basestring):
                 # 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
 
index 0509f92ccbcedb058480f30adf1ce6c835498ac5..a1809129086f405479ace06949cc9c50b7ba14f8 100644 (file)
@@ -80,6 +80,11 @@ class FractionTest(unittest.TestCase):
         self.assertEquals((-16, 5), _components(F(u" -3.2 ")))
         self.assertEquals((-3, 1), _components(F(u" -3. ")))
         self.assertEquals((3, 5), _components(F(u" .6 ")))
+        self.assertEquals((1, 3125), _components(F("32.e-5")))
+        self.assertEquals((1000000, 1), _components(F("1E+06")))
+        self.assertEquals((-12300, 1), _components(F("-1.23e4")))
+        self.assertEquals((0, 1), _components(F(" .0e+0\t")))
+        self.assertEquals((0, 1), _components(F("-0.000e0")))
 
 
         self.assertRaisesMessage(
@@ -88,6 +93,9 @@ class FractionTest(unittest.TestCase):
         self.assertRaisesMessage(
             ValueError, "Invalid literal for Fraction: '3/'",
             F, "3/")
+        self.assertRaisesMessage(
+            ValueError, "Invalid literal for Fraction: '/2'",
+            F, "/2")
         self.assertRaisesMessage(
             ValueError, "Invalid literal for Fraction: '3 /2'",
             F, "3 /2")
@@ -103,10 +111,6 @@ class FractionTest(unittest.TestCase):
             # Avoid treating '.' as a regex special character.
             ValueError, "Invalid literal for Fraction: '3a2'",
             F, "3a2")
-        self.assertRaisesMessage(
-            # Only parse ordinary decimals, not scientific form.
-            ValueError, "Invalid literal for Fraction: '3.2e4'",
-            F, "3.2e4")
         self.assertRaisesMessage(
             # Don't accept combinations of decimals and fractions.
             ValueError, "Invalid literal for Fraction: '3/7.2'",
index aa4fa6d6a5a04b9dfd9df0dc5ee58e3eb14f087e..5515af9ff27797c92575a289dd02a6ce0a3cf766 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -241,6 +241,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #5812: Fraction('1e6') is valid: more generally, any string
+  that's valid for float() is now valid for Fraction(), with the
+  exception of strings representing NaNs and infinities.
+
 - Issue #5795: Fixed test_distutils failure on Debian ppc.
 
 - Issue #5768: Fixed bug in Unicode output logic and test case for same.