Skip to content

Commit 0b458d5

Browse files
committed
count() should return integers python#10474
1 parent 2e579f0 commit 0b458d5

3 files changed

Lines changed: 6 additions & 2 deletions

File tree

Lib/test/test_builtin.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,8 @@ def __index__(self):
10331033
self.assertEqual(range(3).count(1), 1)
10341034
self.assertEqual(range(3).count(2), 1)
10351035
self.assertEqual(range(3).count(3), 0)
1036+
self.assertIs(type(range(3).count(-1)), int)
1037+
self.assertIs(type(range(3).count(1)), int)
10361038

10371039
self.assertEqual(range(10**20).count(1), 1)
10381040
self.assertEqual(range(10**20).count(10**20), 0)

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ What's New in Python 3.2 Beta 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #10474: range().count() should return integers.
14+
1315
- Issue #10255: Fix reference leak in Py_InitializeEx(). Patch by Neil
1416
Schemenauer.
1517

Objects/rangeobject.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,9 @@ range_count(rangeobject *r, PyObject *ob)
338338
{
339339
if (PyLong_CheckExact(ob) || PyBool_Check(ob)) {
340340
if (range_contains_long(r, ob))
341-
Py_RETURN_TRUE;
341+
return PyLong_FromLong(1);
342342
else
343-
Py_RETURN_FALSE;
343+
return PyLong_FromLong(0);
344344
} else {
345345
Py_ssize_t count;
346346
count = _PySequence_IterSearch((PyObject*)r, ob, PY_ITERSEARCH_COUNT);

0 commit comments

Comments
 (0)