8000 bpo-30537: use PyNumber in itertools.islice instead of PyLong (#1918) · python/cpython@0ecdc52 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0ecdc52

Browse files
wrobertsrhettinger
authored andcommitted
bpo-30537: use PyNumber in itertools.islice instead of PyLong (#1918)
* bpo-30537: use PyNumber in itertools instead of PyLong * bpo-30537: revert changes except to islice_new * bpo-30537: test itertools.islice and add entry to Misc/NEWS
1 parent 5edf827 commit 0ecdc52

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

Lib/test/test_itertools.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,19 @@ def test_islice(self):
12431243
support.gc_collect()
12441244
self.assertIsNone(wr())
12451245

1246+
# Issue #30537: islice can accept integer-like objects as
1247+
# arguments
1248+
class IntLike(object):
1249+
def __init__(self, val):
1250+
self.val = val
1251+
def __index__(self):
1252+
return self.val
1253+
self.assertEqual(list(islice(range(100), IntLike(10))), list(range(10)))
1254+
self.assertEqual(list(islice(range(100), IntLike(10), IntLike(50))),
1255+
list(range(10, 50)))
1256+
self.assertEqual(list(islice(range(100), IntLike(10), IntLike(50), IntLike(5))),
1257+
list(range(10,50,5)))
1258+
12461259
def test_takewhile(self):
12471260
data = [1, 3, 5, 20, 2, 4, 6, 8]
12481261
self.assertEqual(list(takewhile(underten, data)), [1, 3, 5])

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ Core and Builtins
128128

129129
- bpo-29546: Improve from-import error message with location
130130

131+
- bpo-30537: itertools.islice now accepts integer-like objects (having
132+
an __index__ method) as start, stop, and slice arguments
133+
131134
- Issue #29319: Prevent RunMainFromImporter overwriting sys.path[0].
132135

133136
- Issue #29337: Fixed possible BytesWarning when compare the code objects.

Modules/itertoolsmodule.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,7 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
14171417
numargs = PyTuple_Size(args);
14181418
if (numargs == 2) {
14191419
if (a1 != Py_None) {
1420-
stop = PyLong_AsSsize_t(a1);
1420+
stop = PyNumber_AsSsize_t(a1, PyExc_OverflowError);
14211421
if (stop == -1) {
14221422
if (PyErr_Occurred())
14231423
PyErr_Clear();
@@ -1429,11 +1429,11 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
14291429
}
14301430
} else {
14311431
if (a1 != Py_None)
1432-
start = PyLong_AsSsize_t(a1);
1432+
start = PyNumber_AsSsize_t(a1, PyExc_OverflowError);
14331433
if (start == -1 && PyErr_Occurred())
14341434
PyErr_Clear();
14351435
if (a2 != Py_None) {
1436-
stop = PyLong_AsSsize_t(a2);
1436+
stop = PyNumber_AsSsize_t(a2, PyExc_OverflowError);
14371437
if (stop == -1) {
14381438
if (PyErr_Occurred())
14391439
PyErr_Clear();
@@ -1453,7 +1453,7 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
14531453

14541454
if (a3 != NULL) {
14551455
if (a3 != Py_None)
1456-
step = PyLong_AsSsize_t(a3);
1456+
step = PyNumber_AsSsize_t(a3, PyExc_OverflowError);
14571457
if (step == -1 && PyErr_Occurred())
14581458
PyErr_Clear();
14591459
}

0 commit comments

Comments
 (0)
0