[Python-checkins] r69237 - in python/trunk: Lib/test/test_iterlen.py Objects/abstract.c

raymond.hettinger python-checkins at python.org
Tue Feb 3 03:23:19 CET 2009


Author: raymond.hettinger
Date: Tue Feb  3 03:23:19 2009
New Revision: 69237

Log:
Validate that __length_hint__ returns a usable result.

Modified:
   python/trunk/Lib/test/test_iterlen.py
   python/trunk/Objects/abstract.c

Modified: python/trunk/Lib/test/test_iterlen.py
==============================================================================
--- python/trunk/Lib/test/test_iterlen.py	(original)
+++ python/trunk/Lib/test/test_iterlen.py	Tue Feb  3 03:23:19 2009
@@ -208,6 +208,11 @@
     def __length_hint__(self):
         raise RuntimeError('hello')
 
+class NoneLengthHint(object):
+    def __iter__(self): return iter(range(10))
+    def __length_hint__(self):
+        return None
+
 class TestLengthHintExceptions(unittest.TestCase):
 
     def test_issue1242657(self):
@@ -225,6 +230,11 @@
         self.assertRaises(RuntimeError, b.extend, BadLen())
         self.assertRaises(RuntimeError, b.extend, BadLengthHint())
 
+    def test_invalid_hint(self):
+        # Make sure an invalid result doesn't muck-up the works
+        self.assertEqual(list(NoneLengthHint()), list(range(10)))
+
+
 def test_main():
     unittests = [
         TestRepeat,

Modified: python/trunk/Objects/abstract.c
==============================================================================
--- python/trunk/Objects/abstract.c	(original)
+++ python/trunk/Objects/abstract.c	Tue Feb  3 03:23:19 2009
@@ -123,7 +123,7 @@
 		PyErr_Clear();
 		return defaultvalue;
 	}
-	rv = PyInt_AsLong(ro);
+	rv = rv = PyLong_Check(ro) ? PyLong_AsSsize_t(ro) : defaultvalue;
 	Py_DECREF(ro);
 	return rv;
 }


More information about the Python-checkins mailing list