summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Dickinson <dickinsm@gmail.com>2009-09-08 19:23:44 (GMT)
committerMark Dickinson <dickinsm@gmail.com>2009-09-08 19:23:44 (GMT)
commit8fde3da126674a9c5c529da8e4d9d7094030739e (patch)
treeb047382092f2bc8397a75ad7d4ce2e80fdc61870
parent0e8b8877db1f11357dd28441ae542866a0d192e3 (diff)
downloadcpython-8fde3da126674a9c5c529da8e4d9d7094030739e.zip
cpython-8fde3da126674a9c5c529da8e4d9d7094030739e.tar.gz
cpython-8fde3da126674a9c5c529da8e4d9d7094030739e.tar.bz2
Merged revisions 74709 via svnmerge from
svn+ssh://pythondev@www.python.org/python/branches/py3k ................ r74709 | mark.dickinson | 2009-09-07 19:08:12 +0100 (Mon, 07 Sep 2009) | 9 lines Merged revisions 74708 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74708 | mark.dickinson | 2009-09-07 19:04:58 +0100 (Mon, 07 Sep 2009) | 2 lines #Issue 6795: Fix infinite recursion in long(Decimal('nan')); change int(Decimal('nan')) to raise ValueError instead of either returning NaN or raising InvalidContext. ........ ................
-rw-r--r--Lib/decimal.py5
-rw-r--r--Lib/test/test_decimal.py5
-rw-r--r--Misc/NEWS4
3 files changed, 11 insertions, 3 deletions
diff --git a/Lib/decimal.py b/Lib/decimal.py
index e3af3f0..e3fa8cb 100644
--- a/Lib/decimal.py
+++ b/Lib/decimal.py
@@ -1553,10 +1553,9 @@ class Decimal(object):
"""Converts self to an int, truncating if necessary."""
if self._is_special:
if self._isnan():
- context = getcontext()
- return context._raise_error(InvalidContext)
+ raise ValueError("Cannot convert NaN to integer")
elif self._isinfinity():
- raise OverflowError("Cannot convert infinity to int")
+ raise OverflowError("Cannot convert infinity to integer")
s = (-1)**self._sign
if self._exp >= 0:
return s*int(self._int)*10**self._exp
diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py
index f0c4431..e3e50f0 100644
--- a/Lib/test/test_decimal.py
+++ b/Lib/test/test_decimal.py
@@ -1543,6 +1543,11 @@ class DecimalPythonAPItests(unittest.TestCase):
r = d.to_integral(ROUND_DOWN)
self.assertEqual(Decimal(int(d)), r)
+ self.assertRaises(ValueError, int, Decimal('-nan'))
+ self.assertRaises(ValueError, int, Decimal('snan'))
+ self.assertRaises(OverflowError, int, Decimal('inf'))
+ self.assertRaises(OverflowError, int, Decimal('-inf'))
+
def test_trunc(self):
for x in range(-250, 250):
s = '%0.2f' % (x / 100.0)
diff --git a/Misc/NEWS b/Misc/NEWS
index 3a9b0b9..31a8e0e 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -52,6 +52,10 @@ C-API
Library
-------
+- Issue #6795: int(Decimal('nan')) now raises ValueError instead of
+ returning NaN or raising InvalidContext. Also, fix infinite recursion
+ in long(Decimal('nan')).
+
- Issue #6850: Fix bug in Decimal._parse_format_specifier for formats
with no type specifier.