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

neal.norwitz python-checkins at python.org
Tue Aug 15 08:29:04 CEST 2006


Author: neal.norwitz
Date: Tue Aug 15 08:29:03 2006
New Revision: 51298

Modified:
   python/trunk/Lib/test/test_index.py
   python/trunk/Objects/abstract.c
Log:
Subclasses of int/long are allowed to define an __index__.


Modified: python/trunk/Lib/test/test_index.py
==============================================================================
--- python/trunk/Lib/test/test_index.py	(original)
+++ python/trunk/Lib/test/test_index.py	Tue Aug 15 08:29:03 2006
@@ -48,11 +48,12 @@
         self.assertEqual(self.o.__index__(), 4)
         self.assertEqual(self.n.__index__(), 5)
 
-    def test_infinite_recursion(self):
-        self.failUnlessRaises(TypeError, operator.index, TrapInt())
-        self.failUnlessRaises(TypeError, operator.index, TrapLong())
-        self.failUnless(slice(TrapInt()).indices(0)==(0,0,1))
-        self.failUnlessRaises(TypeError, slice(TrapLong()).indices, 0)
+    def test_subclasses(self):
+        r = range(10)
+        self.assertEqual(r[TrapInt(5):TrapInt(10)], r[5:10])
+        self.assertEqual(r[TrapLong(5):TrapLong(10)], r[5:10])
+        self.assertEqual(slice(TrapInt()).indices(0), (0,0,1))
+        self.assertEqual(slice(TrapLong(0)).indices(0), (0,0,1))
 
     def test_error(self):
         self.o.ind = 'dumb'
@@ -104,9 +105,9 @@
         self.assertEqual(self.seq.__mul__(self.n), self.seq * 5)
         self.assertEqual(self.seq.__rmul__(self.n), self.seq * 5)
 
-    def test_infinite_recursion(self):
-        self.failUnlessRaises(TypeError, operator.getitem, self.seq, TrapInt())
-        self.failUnlessRaises(TypeError, operator.getitem, self.seq, TrapLong())
+    def test_subclasses(self):
+        self.assertEqual(self.seq[TrapInt()], self.seq[0])
+        self.assertEqual(self.seq[TrapLong()], self.seq[0])
 
     def test_error(self):
         self.o.ind = 'dumb'

Modified: python/trunk/Objects/abstract.c
==============================================================================
--- python/trunk/Objects/abstract.c	(original)
+++ python/trunk/Objects/abstract.c	Tue Aug 15 08:29:03 2006
@@ -945,16 +945,14 @@
 	PyObject *result = NULL;
 	if (item == NULL)
 		return null_error();
-	/* XXX(nnorwitz): should these be CheckExact?  Aren't subclasses ok? */
-	if (PyInt_CheckExact(item) || PyLong_CheckExact(item)) {
+	if (PyInt_Check(item) || PyLong_Check(item)) {
 		Py_INCREF(item);
 		return item;
 	}
 	if (PyIndex_Check(item)) {
 		result = item->ob_type->tp_as_number->nb_index(item);
-		/* XXX(nnorwitz): Aren't subclasses ok here too? */
 		if (result &&
-		    !PyInt_CheckExact(result) && !PyLong_CheckExact(result)) {
+		    !PyInt_Check(result) && !PyLong_Check(result)) {
 			PyErr_Format(PyExc_TypeError,
 				     "__index__ returned non-(int,long) " \
 				     "(type %.200s)",


More information about the Python-checkins mailing list