[Python-checkins] r68238 - in python/trunk: Doc/library/decimal.rst Lib/fractions.py Lib/heapq.py Lib/test/test_fractions.py Misc/NEWS

georg.brandl python-checkins at python.org
Sat Jan 3 23:03:12 CET 2009


Author: georg.brandl
Date: Sat Jan  3 23:03:11 2009
New Revision: 68238

Log:
Manually merge r68095,68186,68187,68188,68190 from 2.6 branch.


Modified:
   python/trunk/Doc/library/decimal.rst
   python/trunk/Lib/fractions.py
   python/trunk/Lib/heapq.py
   python/trunk/Lib/test/test_fractions.py
   python/trunk/Misc/NEWS

Modified: python/trunk/Doc/library/decimal.rst
==============================================================================
--- python/trunk/Doc/library/decimal.rst	(original)
+++ python/trunk/Doc/library/decimal.rst	Sat Jan  3 23:03:11 2009
@@ -1923,13 +1923,14 @@
     def float_to_decimal(f):
         "Convert a floating point number to a Decimal with no loss of information"
         n, d = f.as_integer_ratio()
-        with localcontext() as ctx:
-            ctx.traps[Inexact] = True
-            while True:
-                try:
-                   return Decimal(n) / Decimal(d)
-                except Inexact:
-                    ctx.prec += 1
+        numerator, denominator = Decimal(n), Decimal(d)
+        ctx = Context(prec=60)
+        result = ctx.divide(numerator, denominator)
+        while ctx.flags[Inexact]:
+            ctx.flags[Inexact] = False
+            ctx.prec *= 2
+            result = ctx.divide(numerator, denominator)
+        return result
 
 .. doctest::
 

Modified: python/trunk/Lib/fractions.py
==============================================================================
--- python/trunk/Lib/fractions.py	(original)
+++ python/trunk/Lib/fractions.py	Sat Jan  3 23:03:11 2009
@@ -111,7 +111,7 @@
 
         """
         if isinstance(f, numbers.Integral):
-            f = float(f)
+            return cls(f)
         elif not isinstance(f, float):
             raise TypeError("%s.from_float() only takes floats, not %r (%s)" %
                             (cls.__name__, f, type(f).__name__))

Modified: python/trunk/Lib/heapq.py
==============================================================================
--- python/trunk/Lib/heapq.py	(original)
+++ python/trunk/Lib/heapq.py	Sat Jan  3 23:03:11 2009
@@ -354,6 +354,10 @@
 
     Equivalent to:  sorted(iterable, key=key)[:n]
     """
+    if key is None:
+        it = izip(iterable, count())                        # decorate
+        result = _nsmallest(n, it)
+        return map(itemgetter(0), result)                   # undecorate
     in1, in2 = tee(iterable)
     it = izip(imap(key, in1), count(), in2)                 # decorate
     result = _nsmallest(n, it)
@@ -365,6 +369,10 @@
 
     Equivalent to:  sorted(iterable, key=key, reverse=True)[:n]
     """
+    if key is None:
+        it = izip(iterable, imap(neg, count()))             # decorate
+        result = _nlargest(n, it)
+        return map(itemgetter(0), result)                   # undecorate
     in1, in2 = tee(iterable)
     it = izip(imap(key, in1), imap(neg, count()), in2)      # decorate
     result = _nlargest(n, it)

Modified: python/trunk/Lib/test/test_fractions.py
==============================================================================
--- python/trunk/Lib/test/test_fractions.py	(original)
+++ python/trunk/Lib/test/test_fractions.py	Sat Jan  3 23:03:11 2009
@@ -139,6 +139,8 @@
     def testFromFloat(self):
         self.assertRaises(TypeError, F.from_float, 3+4j)
         self.assertEquals((10, 1), _components(F.from_float(10)))
+        bigint = 1234567890123456789
+        self.assertEquals((bigint, 1), _components(F.from_float(bigint)))
         self.assertEquals((0, 1), _components(F.from_float(-0.0)))
         self.assertEquals((10, 1), _components(F.from_float(10.0)))
         self.assertEquals((-5, 2), _components(F.from_float(-2.5)))

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Sat Jan  3 23:03:11 2009
@@ -127,6 +127,12 @@
 - Issue #4646: distutils was choking on empty options arg in the setup 
   function. Original patch by Thomas Heller.
 
+- Fractions.from_float() no longer loses precision for integers too big to
+  cast as floats.
+
+- Issue 4790: The nsmallest() and nlargest() functions in the heapq module
+  did unnecessary work in the common case where no key function was specified.
+
 - Issue #3767: Convert Tk object to string in tkColorChooser.
 
 - Issue #3248: Allow placing ScrolledText in a PanedWindow.


More information about the Python-checkins mailing list