From 418f81d9b65ca2c7942ff9c422ad01d5050a808f Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Thu, 24 Sep 2009 20:04:23 +0000 Subject: Issue #1766304: The range.__contains__ optimization should only be applied to ints, not to instances of subclasses of int. --- Lib/test/test_range.py | 6 ++++++ Objects/rangeobject.c | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_range.py b/Lib/test/test_range.py index 1a40dd1..638d943 100644 --- a/Lib/test/test_range.py +++ b/Lib/test/test_range.py @@ -97,6 +97,12 @@ class RangeTest(unittest.TestCase): # ..except if explicitly told so. self.assertTrue(int(C2()) in range(3)) + # Check that the range.__contains__ optimization is only + # used for ints, not for instances of subclasses of int. + class C3(int): + def __eq__(self, other): return True + self.assertTrue(C3(11) in range(10)) + self.assertTrue(C3(11) in list(range(10))) def test_strided_limits(self): r = range(0, 101, 2) diff --git a/Objects/rangeobject.c b/Objects/rangeobject.c index 213f3dd..beff030 100644 --- a/Objects/rangeobject.c +++ b/Objects/rangeobject.c @@ -275,7 +275,7 @@ range_reduce(rangeobject *r, PyObject *args) static int range_contains(rangeobject *r, PyObject *ob) { - if (PyLong_Check(ob)) { + if (PyLong_CheckExact(ob) || PyBool_Check(ob)) { int cmp1, cmp2, cmp3; PyObject *tmp1 = NULL; PyObject *tmp2 = NULL; -- cgit v0.12