[Python-checkins] r69451 - in python/branches/py3k: Lib/test/test_builtin.py Misc/NEWS Objects/typeobject.c

benjamin.peterson python-checkins at python.org
Sun Feb 8 22:07:20 CET 2009


Author: benjamin.peterson
Date: Sun Feb  8 22:07:20 2009
New Revision: 69451

Log:
fix len() when __len__() returns a non number type #5137

Modified:
   python/branches/py3k/Lib/test/test_builtin.py
   python/branches/py3k/Misc/NEWS
   python/branches/py3k/Objects/typeobject.c

Modified: python/branches/py3k/Lib/test/test_builtin.py
==============================================================================
--- python/branches/py3k/Lib/test/test_builtin.py	(original)
+++ python/branches/py3k/Lib/test/test_builtin.py	Sun Feb  8 22:07:20 2009
@@ -611,6 +611,18 @@
             def __len__(self):
                 raise ValueError
         self.assertRaises(ValueError, len, BadSeq())
+        class InvalidLen:
+            def __len__(self):
+                return None
+        self.assertRaises(TypeError, len, InvalidLen())
+        class FloatLen:
+            def __len__(self):
+                return 4.5
+        self.assertRaises(TypeError, len, FloatLen())
+        class HugeLen:
+            def __len__(self):
+                return sys.maxsize + 1
+        self.assertRaises(OverflowError, len, HugeLen())
 
     def test_map(self):
         self.assertEqual(

Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS	(original)
+++ python/branches/py3k/Misc/NEWS	Sun Feb  8 22:07:20 2009
@@ -12,6 +12,9 @@
 Core and Builtins
 -----------------
 
+- Issue #5137: Make len() correctly raise a TypeError when a __len__ method
+  returns a non-number type.
+
 - Issue #5182: Removed memoryview.__str__.
 
 - Issue #1717: Removed builtin cmp() function, dropped tp_compare

Modified: python/branches/py3k/Objects/typeobject.c
==============================================================================
--- python/branches/py3k/Objects/typeobject.c	(original)
+++ python/branches/py3k/Objects/typeobject.c	Sun Feb  8 22:07:20 2009
@@ -4618,7 +4618,7 @@
 
 	if (res == NULL)
 		return -1;
-	len = PyLong_AsSsize_t(res);
+	len = PyNumber_AsSsize_t(res, PyExc_OverflowError);
 	Py_DECREF(res);
 	if (len < 0) {
 		if (!PyErr_Occurred())


More information about the Python-checkins mailing list