Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3a9b062

Browse files
committed
Manually merge r68096,68189 from 3.0 branch.
1 parent 3f5f822 commit 3a9b062

4 files changed

Lines changed: 19 additions & 5 deletions

File tree

Lib/fractions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def from_float(cls, f):
109109
110110
"""
111111
if isinstance(f, numbers.Integral):
112-
f = float(f)
112+
return cls(f)
113113
elif not isinstance(f, float):
114114
raise TypeError("%s.from_float() only takes floats, not %r (%s)" %
115115
(cls.__name__, f, type(f).__name__))

Lib/heapq.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,12 @@ def nsmallest(n, iterable, key=None):
354354
355355
Equivalent to: sorted(iterable, key=key)[:n]
356356
"""
357+
if key is None:
358+
it = zip(iterable, count()) # decorate
359+
result = _nsmallest(n, it)
360+
return list(map(itemgetter(0), result)) # undecorate
357361
in1, in2 = tee(iterable)
358-
keys = in1 if key is None else map(key, in1)
359-
it = zip(keys, count(), in2) # decorate
362+
it = zip(map(key, in1), count(), in2) # decorate
360363
result = _nsmallest(n, it)
361364
return list(map(itemgetter(2), result)) # undecorate
362365

@@ -366,9 +369,12 @@ def nlargest(n, iterable, key=None):
366369
367370
Equivalent to: sorted(iterable, key=key, reverse=True)[:n]
368371
"""
372+
if key is None:
373+
it = zip(iterable, map(neg, count())) # decorate
374+
result = _nlargest(n, it)
375+
return list(map(itemgetter(0), result)) # undecorate
369376
in1, in2 = tee(iterable)
370-
keys = in1 if key is None else map(key, in1)
371-
it = zip(keys, map(neg, count()), in2) # decorate
377+
it = zip(map(key, in1), map(neg, count()), in2) # decorate
372378
result = _nlargest(n, it)
373379
return list(map(itemgetter(2), result)) # undecorate
374380

Lib/test/test_fractions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ def testImmutable(self):
136136
def testFromFloat(self):
137137
self.assertRaises(TypeError, F.from_float, 3+4j)
138138
self.assertEquals((10, 1), _components(F.from_float(10)))
139+
bigint = 1234567890123456789
140+
self.assertEquals((bigint, 1), _components(F.from_float(bigint)))
139141
self.assertEquals((0, 1), _components(F.from_float(-0.0)))
140142
self.assertEquals((10, 1), _components(F.from_float(10.0)))
141143
self.assertEquals((-5, 2), _components(F.from_float(-2.5)))

Misc/NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,15 @@ Library
9292
- Issue #4796: Added Decimal.from_float() and Context.create_decimal_from_float()
9393
to the decimal module.
9494

95+
- Fractions.from_float() no longer loses precision for integers too big to
96+
cast as floats.
97+
9598
- Issue #4812: add missing underscore prefix to some internal-use-only
9699
constants in the decimal module. (Dec_0 becomes _Dec_0, etc.)
97100

101+
- Issue 4790: The nsmallest() and nlargest() functions in the heapq module
102+
did unnecessary work in the common case where no key function was specified.
103+
98104
- Issue #4702: Throwing a DistutilsPlatformError instead of IOError in case
99105
no MSVC compiler is found under Windows. Original patch by Philip Jenvey.
100106

0 commit comments

Comments
 (0)