This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Doc for itertools, 'islice()' implementation have unwanted behavior for recipe 'consume()'
Type: behavior Stage: resolved
Components: Documentation Versions: Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: cheryl.sabella Nosy List: Nofar Schnider, alex0307, cheryl.sabella, docs@python, rhettinger
Priority: normal Keywords: patch

Created on 2016-06-04 05:33 by alex0307, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
islice_my.py alex0307, 2016-06-04 05:33 A simple fix of islice()
Pull Requests
URL Status Linked Edit
PR 6195 merged cheryl.sabella, 2018-03-22 23:27
PR 6266 merged miss-islington, 2018-03-27 01:30
PR 6267 merged miss-islington, 2018-03-27 01:31
PR 6339 merged cheryl.sabella, 2018-04-01 22:53
Messages (11)
msg267236 - (view) Author: alex0307 (alex0307) Date: 2016-06-04 05:33
In the doc for itertools, the python implementation of islice() doesn't actually perform the iteration when given an empty slice (e.g. islice(it,3,3)). 

This is not the expected behavior for 'consume()' in the recipe given below, i.e. consume(it,3) couldn't actually advance the iterator for 3 steps ahead. Instead, the iterator stays at its starting position.

As a simple fix, in the implementation of islice(), the length of the slice object should be checked first. If it's zero, the iterator should be advanced (slice.start-1) times, and return (See islice_my.py)
msg275432 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-09-09 20:44
The recipe should be modified to consume the initial values.  Something like this:

    start, stop, step = s.start or 0, s.stop or sys.maxsize, s.step or 1
    for i in zip(range(0, start), it):
        pass

The recipe needs to pass these tests:

    it = iter('abcdefghi')
    assert list(islice(it, 4, 4)) == []
    assert list(it) == ['e', 'f', 'g', 'h', 'i']
    assert(list(islice('ABCDEFG', 2)) == ['A', 'B'])
    assert(list(islice('ABCDEFG', 2, 4)) == ['C', 'D'])
    assert(list(islice('ABCDEFG', 2, None)) == ['C', 'D', 'E', 'F', 'G'])
    assert(list(islice('ABCDEFG', 0, None, 2)) == ['A', 'C', 'E', 'G'])
msg314164 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2018-03-20 20:20
Hi Nofar,

Are you still interested in working on this issue?  Thanks!
msg314235 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-03-22 06:38
Cheryl, I think you take this one.  It hasn't been touched in a long time and Nofar is focused on another issue.
msg314269 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2018-03-22 16:05
Thanks, Raymond.  I'll take a look.
msg314490 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-03-27 01:29
New changeset da1734c58d2f97387ccc9676074717d38b044128 by Raymond Hettinger (Cheryl Sabella) in branch 'master':
bpo-27212: Modify islice recipe to consume initial values preceding start (GH-6195)
https://github.com/python/cpython/commit/da1734c58d2f97387ccc9676074717d38b044128
msg314491 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-03-27 02:23
New changeset f328caf4caafd4521c356af8cb8a299f27603c90 by Raymond Hettinger (Miss Islington (bot)) in branch '3.7':
bpo-27212: Modify islice recipe to consume initial values preceding start (GH-6195) (#GH-6266)
https://github.com/python/cpython/commit/f328caf4caafd4521c356af8cb8a299f27603c90
msg314492 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-03-27 02:24
New changeset c8698cff7ccc8dc730c58523c7ef4113ea8d3049 by Raymond Hettinger (Miss Islington (bot)) in branch '3.6':
bpo-27212: Modify islice recipe to consume initial values preceding start (GH-6195) (GH-6267)
https://github.com/python/cpython/commit/c8698cff7ccc8dc730c58523c7ef4113ea8d3049
msg314800 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-04-02 05:29
New changeset 325191bd6b3a2d5e3012adee868f19baf3e2c3ab by Raymond Hettinger (Cheryl Sabella) in branch '2.7':
[2.7] bpo-27212: Modify islice recipe to consume initial values preceding start (GH-6195) (GH-6339)
https://github.com/python/cpython/commit/325191bd6b3a2d5e3012adee868f19baf3e2c3ab
msg314801 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-04-02 05:29
Thanks Cheryl.  This was nice work :-)
msg314816 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2018-04-02 17:36
Thanks!  :-)
History
Date User Action Args
2022-04-11 14:58:32adminsetgithub: 71399
2018-04-02 17:36:40cheryl.sabellasetmessages: + msg314816
2018-04-02 05:29:54rhettingersetstatus: open -> closed
resolution: fixed
messages: + msg314801

stage: patch review -> resolved
2018-04-02 05:29:05rhettingersetmessages: + msg314800
2018-04-01 22:53:37cheryl.sabellasetpull_requests: + pull_request6051
2018-03-27 02:24:04rhettingersetmessages: + msg314492
2018-03-27 02:23:36rhettingersetmessages: + msg314491
2018-03-27 01:31:43miss-islingtonsetpull_requests: + pull_request5992
2018-03-27 01:30:51miss-islingtonsetpull_requests: + pull_request5991
2018-03-27 01:29:36rhettingersetmessages: + msg314490
2018-03-22 23:27:53cheryl.sabellasetkeywords: + patch
stage: patch review
pull_requests: + pull_request5942
2018-03-22 16:05:44cheryl.sabellasetmessages: + msg314269
2018-03-22 06:38:35rhettingersetassignee: Nofar Schnider -> cheryl.sabella
messages: + msg314235
2018-03-20 20:20:53cheryl.sabellasetnosy: + cheryl.sabella
messages: + msg314164
2016-09-09 20:45:52rhettingersetassignee: rhettinger -> Nofar Schnider

nosy: + Nofar Schnider
2016-09-09 20:44:00rhettingersetmessages: + msg275432
2016-06-04 05:41:29serhiy.storchakasetassignee: docs@python -> rhettinger

nosy: + rhettinger
2016-06-04 05:33:46alex0307create