Commit fcb5484f authored by Mark Dickinson's avatar Mark Dickinson

Issue #1766304: The range.__contains__ optimization should only be

applied to ints, not to instances of subclasses of int.
parent b6f9dce0
...@@ -97,6 +97,12 @@ class RangeTest(unittest.TestCase): ...@@ -97,6 +97,12 @@ class RangeTest(unittest.TestCase):
# ..except if explicitly told so. # ..except if explicitly told so.
self.assertTrue(int(C2()) in range(3)) 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): def test_strided_limits(self):
r = range(0, 101, 2) r = range(0, 101, 2)
......
...@@ -275,7 +275,7 @@ range_reduce(rangeobject *r, PyObject *args) ...@@ -275,7 +275,7 @@ range_reduce(rangeobject *r, PyObject *args)
static int static int
range_contains(rangeobject *r, PyObject *ob) { range_contains(rangeobject *r, PyObject *ob) {
if (PyLong_Check(ob)) { if (PyLong_CheckExact(ob) || PyBool_Check(ob)) {
int cmp1, cmp2, cmp3; int cmp1, cmp2, cmp3;
PyObject *tmp1 = NULL; PyObject *tmp1 = NULL;
PyObject *tmp2 = NULL; PyObject *tmp2 = NULL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment