summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2009-01-03 22:07:57 (GMT)
committerGeorg Brandl <georg@python.org>2009-01-03 22:07:57 (GMT)
commit3a9b062f5bb0f7d9c2a408c31509cdee4f6aa4c8 (patch)
tree1bc74abad9db6c9a36875d97c1229998ea26fa4f /Lib
parent3f5f8228c02618480a2fd6bc8583621a94bacf2f (diff)
downloadcpython-3a9b062f5bb0f7d9c2a408c31509cdee4f6aa4c8.zip
cpython-3a9b062f5bb0f7d9c2a408c31509cdee4f6aa4c8.tar.gz
cpython-3a9b062f5bb0f7d9c2a408c31509cdee4f6aa4c8.tar.bz2
Manually merge r68096,68189 from 3.0 branch.
Diffstat (limited to 'Lib')
-rwxr-xr-xLib/fractions.py2
-rw-r--r--Lib/heapq.py14
-rw-r--r--Lib/test/test_fractions.py2
3 files changed, 13 insertions, 5 deletions
diff --git a/Lib/fractions.py b/Lib/fractions.py
index bc06524..ed1e9a0 100755
--- a/Lib/fractions.py
+++ b/Lib/fractions.py
@@ -109,7 +109,7 @@ class Fraction(numbers.Rational):
"""
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__))
diff --git a/Lib/heapq.py b/Lib/heapq.py
index 380fe12..2d34046 100644
--- a/Lib/heapq.py
+++ b/Lib/heapq.py
@@ -354,9 +354,12 @@ def nsmallest(n, iterable, key=None):
Equivalent to: sorted(iterable, key=key)[:n]
"""
+ if key is None:
+ it = zip(iterable, count()) # decorate
+ result = _nsmallest(n, it)
+ return list(map(itemgetter(0), result)) # undecorate
in1, in2 = tee(iterable)
- keys = in1 if key is None else map(key, in1)
- it = zip(keys, count(), in2) # decorate
+ it = zip(map(key, in1), count(), in2) # decorate
result = _nsmallest(n, it)
return list(map(itemgetter(2), result)) # undecorate
@@ -366,9 +369,12 @@ def nlargest(n, iterable, key=None):
Equivalent to: sorted(iterable, key=key, reverse=True)[:n]
"""
+ if key is None:
+ it = zip(iterable, map(neg, count())) # decorate
+ result = _nlargest(n, it)
+ return list(map(itemgetter(0), result)) # undecorate
in1, in2 = tee(iterable)
- keys = in1 if key is None else map(key, in1)
- it = zip(keys, map(neg, count()), in2) # decorate
+ it = zip(map(key, in1), map(neg, count()), in2) # decorate
result = _nlargest(n, it)
return list(map(itemgetter(2), result)) # undecorate
diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py
index 4fadf6c..6851d2d 100644
--- a/Lib/test/test_fractions.py
+++ b/Lib/test/test_fractions.py
@@ -136,6 +136,8 @@ class FractionTest(unittest.TestCase):
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)))