[Python-checkins] r65518 - in python/trunk: Lib/test/test_long.py Misc/NEWS Objects/longobject.c

mark.dickinson python-checkins at python.org
Mon Aug 4 23:30:10 CEST 2008


Author: mark.dickinson
Date: Mon Aug  4 23:30:09 2008
New Revision: 65518

Log:
Issue #1481296: (again!) Make conversion of a float NaN to an int or 
long raise ValueError instead of returning 0.  Also, change the error 
message for conversion of an infinity to an integer, replacing 'long' by 
'integer', so that it's appropriate for both long(float('inf')) and 
int(float('inf')).



Modified:
   python/trunk/Lib/test/test_long.py
   python/trunk/Misc/NEWS
   python/trunk/Objects/longobject.c

Modified: python/trunk/Lib/test/test_long.py
==============================================================================
--- python/trunk/Lib/test/test_long.py	(original)
+++ python/trunk/Lib/test/test_long.py	Mon Aug  4 23:30:09 2008
@@ -745,7 +745,8 @@
 
     def test_nan_inf(self):
         self.assertRaises(OverflowError, long, float('inf'))
-        self.assertEqual(long(float('nan')), 0L)
+        self.assertRaises(OverflowError, long, float('-inf'))
+        self.assertRaises(ValueError, long, float('nan'))
 
 def test_main():
     test_support.run_unittest(LongTest)

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Mon Aug  4 23:30:09 2008
@@ -12,6 +12,9 @@
 Core and Builtins
 -----------------
 
+- Issue #1481296: Make long(float('nan')) and int(float('nan')) raise
+  ValueError consistently across platforms.
+
 - Issue #3479: On platforms where sizeof(int) is smaller than sizeof(long)
   (64bit Unix, for example), unichr() would truncate its argument and return
   u'\x00' for unichr(2**32). Now it properly raises an OverflowError.

Modified: python/trunk/Objects/longobject.c
==============================================================================
--- python/trunk/Objects/longobject.c	(original)
+++ python/trunk/Objects/longobject.c	Mon Aug  4 23:30:09 2008
@@ -176,11 +176,13 @@
 	neg = 0;
 	if (Py_IS_INFINITY(dval)) {
 		PyErr_SetString(PyExc_OverflowError,
-			"cannot convert float infinity to long");
+			"cannot convert float infinity to integer");
 		return NULL;
 	}
 	if (Py_IS_NAN(dval)) {
-		return PyLong_FromLong(0L);
+		PyErr_SetString(PyExc_ValueError,
+			"cannot convert float NaN to integer");
+		return NULL;
 	}
 	if (dval < 0.0) {
 		neg = 1;


More information about the Python-checkins mailing list