From 4c73e4da47d8cca55fa3982a23e79f78b5bf1341 Mon Sep 17 00:00:00 2001 From: Will Roberts Date: Fri, 2 Jun 2017 13:28:16 +0200 Subject: [PATCH 1/3] bpo-30537: use PyNumber in itertools instead of PyLong --- Modules/itertoolsmodule.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index fac5b29a6acc22..ab66753b5f9ec7 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -1417,7 +1417,7 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds) numargs = PyTuple_Size(args); if (numargs == 2) { if (a1 != Py_None) { - stop = PyLong_AsSsize_t(a1); + stop = PyNumber_AsSsize_t(a1, PyExc_OverflowError); if (stop == -1) { if (PyErr_Occurred()) PyErr_Clear(); @@ -1429,11 +1429,11 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds) } } else { if (a1 != Py_None) - start = PyLong_AsSsize_t(a1); + start = PyNumber_AsSsize_t(a1, PyExc_OverflowError); if (start == -1 && PyErr_Occurred()) PyErr_Clear(); if (a2 != Py_None) { - stop = PyLong_AsSsize_t(a2); + stop = PyNumber_AsSsize_t(a2, PyExc_OverflowError); if (stop == -1) { if (PyErr_Occurred()) PyErr_Clear(); @@ -1453,7 +1453,7 @@ islice_new(PyTypeObject *type, PyObject *args, PyObject *kwds) if (a3 != NULL) { if (a3 != Py_None) - step = PyLong_AsSsize_t(a3); + step = PyNumber_AsSsize_t(a3, PyExc_OverflowError); if (step == -1 && PyErr_Occurred()) PyErr_Clear(); } @@ -1573,7 +1573,7 @@ islice_reduce(isliceobject *lz) static PyObject * islice_setstate(isliceobject *lz, PyObject *state) { - Py_ssize_t cnt = PyLong_AsSsize_t(state); + Py_ssize_t cnt = PyNumber_AsSsize_t(state, PyExc_OverflowError); if (cnt == -1 && PyErr_Occurred()) return NULL; @@ -2265,7 +2265,7 @@ product_setstate(productobject *lz, PyObject *state) for (i=0; ir; i++) { Py_ssize_t max; PyObject* indexObject = PyTuple_GET_ITEM(state, i); - Py_ssize_t index = PyLong_AsSsize_t(indexObject); + Py_ssize_t index = PyNumber_AsSsize_t(indexObject, PyExc_OverflowError); if (index == -1 && PyErr_Occurred()) return NULL; /* not an integer */ @@ -2925,7 +2925,7 @@ cwr_setstate(cwrobject *lz, PyObject *state) n = PyTuple_GET_SIZE(lz->pool); for (i=0; ir; i++) { PyObject* indexObject = PyTuple_GET_ITEM(state, i); - Py_ssize_t index = PyLong_AsSsize_t(indexObject); + Py_ssize_t index = PyNumber_AsSsize_t(indexObject, PyExc_OverflowError); if (index < 0 && PyErr_Occurred()) return NULL; /* not an integer */ @@ -3072,11 +3072,11 @@ permutations_new(PyTypeObject *type, PyObject *args, PyObject *kwds) r = n; if (robj != Py_None) { - if (!PyLong_Check(robj)) { - PyErr_SetString(PyExc_TypeError, "Expected int as r"); + if (!PyNumber_Check(robj)) { + PyErr_SetString(PyExc_TypeError, "Expected number as r"); goto error; } - r = PyLong_AsSsize_t(robj); + r = PyNumber_AsSsize_t(robj, PyExc_OverflowError); if (r == -1 && PyErr_Occurred()) goto error; } @@ -3307,7 +3307,7 @@ permutations_setstate(permutationsobject *po, PyObject *state) for (i=0; ir; i++) { PyObject* indexObject = PyTuple_GET_ITEM(cycles, i); - Py_ssize_t index = PyLong_AsSsize_t(indexObject); + Py_ssize_t index = PyNumber_AsSsize_t(indexObject, PyExc_OverflowError); if (index < 0 && PyErr_Occurred()) return NULL; /* not an integer */ if (index < 1) From d6f8d1fde6506a66329db7c2a06696baca38da61 Mon Sep 17 00:00:00 2001 From: Will Roberts Date: Mon, 5 Jun 2017 14:23:58 +0200 Subject: [PATCH 2/3] bpo-30537: revert changes except to islice_new --- Modules/itertoolsmodule.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c index ab66753b5f9ec7..ee7bb66fd19aa7 100644 --- a/Modules/itertoolsmodule.c +++ b/Modules/itertoolsmodule.c @@ -1573,7 +1573,7 @@ islice_reduce(isliceobject *lz) static PyObject * islice_setstate(isliceobject *lz, PyObject *state) { - Py_ssize_t cnt = PyNumber_AsSsize_t(state, PyExc_OverflowError); + Py_ssize_t cnt = PyLong_AsSsize_t(state); if (cnt == -1 && PyErr_Occurred()) return NULL; @@ -2265,7 +2265,7 @@ product_setstate(productobject *lz, PyObject *state) for (i=0; ir; i++) { Py_ssize_t max; PyObject* indexObject = PyTuple_GET_ITEM(state, i); - Py_ssize_t index = PyNumber_AsSsize_t(indexObject, PyExc_OverflowError); + Py_ssize_t index = PyLong_AsSsize_t(indexObject); if (index == -1 && PyErr_Occurred()) return NULL; /* not an integer */ @@ -2925,7 +2925,7 @@ cwr_setstate(cwrobject *lz, PyObject *state) n = PyTuple_GET_SIZE(lz->pool); for (i=0; ir; i++) { PyObject* indexObject = PyTuple_GET_ITEM(state, i); - Py_ssize_t index = PyNumber_AsSsize_t(indexObject, PyExc_OverflowError); + Py_ssize_t index = PyLong_AsSsize_t(indexObject); if (index < 0 && PyErr_Occurred()) return NULL; /* not an integer */ @@ -3072,11 +3072,11 @@ permutations_new(PyTypeObject *type, PyObject *args, PyObject *kwds) r = n; if (robj != Py_None) { - if (!PyNumber_Check(robj)) { - PyErr_SetString(PyExc_TypeError, "Expected number as r"); + if (!PyLong_Check(robj)) { + PyErr_SetString(PyExc_TypeError, "Expected int as r"); goto error; } - r = PyNumber_AsSsize_t(robj, PyExc_OverflowError); + r = PyLong_AsSsize_t(robj); if (r == -1 && PyErr_Occurred()) goto error; } @@ -3307,7 +3307,7 @@ permutations_setstate(permutationsobject *po, PyObject *state) for (i=0; ir; i++) { PyObject* indexObject = PyTuple_GET_ITEM(cycles, i); - Py_ssize_t index = PyNumber_AsSsize_t(indexObject, PyExc_OverflowError); + Py_ssize_t index = PyLong_AsSsize_t(indexObject); if (index < 0 && PyErr_Occurred()) return NULL; /* not an integer */ if (index < 1) From ce23e9abd6cb10a233cc099713f29edf2506407a Mon Sep 17 00:00:00 2001 From: Will Roberts Date: Wed, 7 Jun 2017 13:41:55 +0200 Subject: [PATCH 3/3] bpo-30537: test itertools.islice and add entry to Misc/NEWS --- Lib/test/test_itertools.py | 13 +++++++++++++ Misc/NEWS | 3 +++ 2 files changed, 16 insertions(+) diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py index c431f0dc6e1d7f..f8a301b12ac17a 100644 --- a/Lib/test/test_itertools.py +++ b/Lib/test/test_itertools.py @@ -1243,6 +1243,19 @@ def test_islice(self): support.gc_collect() self.assertIsNone(wr()) + # Issue #30537: islice can accept integer-like objects as + # arguments + class IntLike(object): + def __init__(self, val): + self.val = val + def __index__(self): + return self.val + self.assertEqual(list(islice(range(100), IntLike(10))), list(range(10))) + self.assertEqual(list(islice(range(100), IntLike(10), IntLike(50))), + list(range(10, 50))) + self.assertEqual(list(islice(range(100), IntLike(10), IntLike(50), IntLike(5))), + list(range(10,50,5))) + def test_takewhile(self): data = [1, 3, 5, 20, 2, 4, 6, 8] self.assertEqual(list(takewhile(underten, data)), [1, 3, 5]) diff --git a/Misc/NEWS b/Misc/NEWS index 5066f57e143a02..0bb1948e25159d 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -128,6 +128,9 @@ Core and Builtins - bpo-29546: Improve from-import error message with location +- bpo-30537: itertools.islice now accepts integer-like objects (having + an __index__ method) as start, stop, and slice arguments + - Issue #29319: Prevent RunMainFromImporter overwriting sys.path[0]. - Issue #29337: Fixed possible BytesWarning when compare the code objects.