diff options
author | Neal Norwitz <nnorwitz@gmail.com> | 2006-08-15 06:29:03 (GMT) |
---|---|---|
committer | Neal Norwitz <nnorwitz@gmail.com> | 2006-08-15 06:29:03 (GMT) |
commit | 271a8689e98bc725ca9207dee52443d61f99211f (patch) | |
tree | 8b9ff92c386916fe9762d598080332d848689e1f | |
parent | 6e482569c8cba727d803e2283ae1481c5c308d54 (diff) | |
download | cpython-271a8689e98bc725ca9207dee52443d61f99211f.zip cpython-271a8689e98bc725ca9207dee52443d61f99211f.tar.gz cpython-271a8689e98bc725ca9207dee52443d61f99211f.tar.bz2 |
Subclasses of int/long are allowed to define an __index__.
-rw-r--r-- | Lib/test/test_index.py | 17 | ||||
-rw-r--r-- | Objects/abstract.c | 6 |
2 files changed, 11 insertions, 12 deletions
diff --git a/Lib/test/test_index.py b/Lib/test/test_index.py index 6ad7d5e..ecb566d 100644 --- a/Lib/test/test_index.py +++ b/Lib/test/test_index.py @@ -48,11 +48,12 @@ class BaseTestCase(unittest.TestCase): 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 @@ class SeqTestCase(unittest.TestCase): 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' diff --git a/Objects/abstract.c b/Objects/abstract.c index c8e9ddc..a18bb78 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -945,16 +945,14 @@ PyNumber_Index(PyObject *item) 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)", |