[Python-checkins] r74709 - in python/branches/py3k: Lib/decimal.py Lib/test/test_decimal.py Misc/NEWS

mark.dickinson python-checkins at python.org
Mon Sep 7 20:08:12 CEST 2009


Author: mark.dickinson
Date: Mon Sep  7 20:08:12 2009
New Revision: 74709

Log:
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.
........


Modified:
   python/branches/py3k/   (props changed)
   python/branches/py3k/Lib/decimal.py
   python/branches/py3k/Lib/test/test_decimal.py
   python/branches/py3k/Misc/NEWS

Modified: python/branches/py3k/Lib/decimal.py
==============================================================================
--- python/branches/py3k/Lib/decimal.py	(original)
+++ python/branches/py3k/Lib/decimal.py	Mon Sep  7 20:08:12 2009
@@ -1553,10 +1553,9 @@
         """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

Modified: python/branches/py3k/Lib/test/test_decimal.py
==============================================================================
--- python/branches/py3k/Lib/test/test_decimal.py	(original)
+++ python/branches/py3k/Lib/test/test_decimal.py	Mon Sep  7 20:08:12 2009
@@ -1543,6 +1543,11 @@
             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)

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Mon Sep  7 20:08:12 2009
@@ -70,6 +70,10 @@
 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.
 


More information about the Python-checkins mailing list