Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 418f81d

Browse files
committed
Issue #1766304: The range.__contains__ optimization should only be
applied to ints, not to instances of subclasses of int.
1 parent 067b38e commit 418f81d

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

Lib/test/test_range.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ def __index__(self): return 1
9797
# ..except if explicitly told so.
9898
self.assertTrue(int(C2()) in range(3))
9999

100+
# Check that the range.__contains__ optimization is only
101+
# used for ints, not for instances of subclasses of int.
102+
class C3(int):
103+
def __eq__(self, other): return True
104+
self.assertTrue(C3(11) in range(10))
105+
self.assertTrue(C3(11) in list(range(10)))
100106

101107
def test_strided_limits(self):
102108
r = range(0, 101, 2)

Objects/rangeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ range_reduce(rangeobject *r, PyObject *args)
275275

276276
static int
277277
range_contains(rangeobject *r, PyObject *ob) {
278-
if (PyLong_Check(ob)) {
278+
if (PyLong_CheckExact(ob) || PyBool_Check(ob)) {
279279
int cmp1, cmp2, cmp3;
280280
PyObject *tmp1 = NULL;
281281
PyObject *tmp2 = NULL;

0 commit comments

Comments
 (0)