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

Skip to content

Commit ba298e4

Browse files
committed
Merged revisions 68314 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r68314 | mark.dickinson | 2009-01-04 21:10:56 +0000 (Sun, 04 Jan 2009) | 5 lines Fix Decimal.from_float to use valid Python 2.3 syntax, as per comments at top of decimal.py. (But note that the from_float method itself with still not be usable before Python 2.7.) See issue 4796 for discussion. ........
1 parent 9d625c2 commit ba298e4

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

Lib/decimal.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,8 @@ def __new__(cls, value="0", context=None):
655655

656656
raise TypeError("Cannot convert %r to Decimal" % value)
657657

658-
@classmethod
658+
# @classmethod, but @decorator is not valid Python 2.3 syntax, so
659+
# don't use it (see notes on Py2.3 compatibility at top of file)
659660
def from_float(cls, f):
660661
"""Converts a float to a decimal number, exactly.
661662
@@ -681,11 +682,18 @@ def from_float(cls, f):
681682
return cls(f)
682683
if _math.isinf(f) or _math.isnan(f): # raises TypeError if not a float
683684
return cls(repr(f))
684-
sign = 0 if _math.copysign(1.0, f) == 1.0 else 1
685+
if _math.copysign(1.0, f) == 1.0:
686+
sign = 0
687+
else:
688+
sign = 1
685689
n, d = abs(f).as_integer_ratio()
686690
k = d.bit_length() - 1
687691
result = _dec_from_triple(sign, str(n*5**k), -k)
688-
return result if cls is Decimal else cls(result)
692+
if cls is Decimal:
693+
return result
694+
else:
695+
return cls(result)
696+
from_float = classmethod(from_float)
689697

690698
def _isnan(self):
691699
"""Returns whether the number is not actually one.

0 commit comments

Comments
 (0)