[issue11908] Weird `slice.stop or sys.maxint`

New submission from Ram Rachum <cool-rr@cool-rr.com>: In the documentation for `itertools.islice` I see this line: it = iter(xrange(s.start or 0, s.stop or sys.maxint, s.step or 1)) Is it really okay to do `s.stop or sys.maxint`? I'm assuming this was targeting `None`, but what if `s.stop == 0`? And `s.step` could (pathologically) be `0` too, no? ---------- assignee: docs@python components: Documentation messages: 134276 nosy: cool-RR, docs@python priority: normal severity: normal status: open title: Weird `slice.stop or sys.maxint` type: behavior versions: Python 2.5, Python 2.6, Python 2.7, Python 3.1, Python 3.2, Python 3.3, Python 3.4 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue11908> _______________________________________

Changes by Raymond Hettinger <raymond.hettinger@gmail.com>: ---------- assignee: docs@python -> rhettinger nosy: +rhettinger _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue11908> _______________________________________

ysj.ray <ysj.ray@gmail.com> added the comment: `step` argument for xrange() could not be 0. But `s.stop or sys.maxint` is really a problem, in the case of `s.stop == 0`. So the given `Equivalent to` python code in the doc is not precisely equivalent to the c implementation. The doc needs a fix. ---------- nosy: +ysj.ray _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue11908> _______________________________________

Raymond Hettinger <raymond.hettinger@gmail.com> added the comment: I've got from here. Thanks. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue11908> _______________________________________

Changes by Terry J. Reedy <tjreedy@udel.edu>: ---------- versions: -Python 2.5, Python 2.6, Python 3.4 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue11908> _______________________________________

Changes by Ezio Melotti <ezio.melotti@gmail.com>: ---------- keywords: +easy nosy: +ezio.melotti stage: -> needs patch versions: -Python 3.1 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue11908> _______________________________________

Brian Brazil <brian.brazil@gmail.com> added the comment: The attached patch fixes this ---------- keywords: +patch nosy: +bbrazil Added file: http://bugs.python.org/file26283/issue11908-islice-docs.patch _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue11908> _______________________________________

Serhiy Storchaka added the comment: LGTM. However note, that for 2.7 the patch should be modified (maxsize -> maxint, range -> xrange). ---------- nosy: +serhiy.storchaka stage: needs patch -> commit review versions: +Python 3.4 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue11908> _______________________________________

Changes by Mark Dickinson <dickinsm@gmail.com>: ---------- nosy: +mark.dickinson _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue11908> _______________________________________

Changes by Raymond Hettinger <raymond.hettinger@gmail.com>: ---------- priority: normal -> low _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue11908> _______________________________________

Raymond Hettinger added the comment: ysj: The "equivalent" means "roughly equivalent" not "precisely equivalent". The purpose of the code in the docs is to help communicate what islice() is all about. Practicality beats purity in this regard. I know of no one who has ever been mislead by the islice() docs. I don't really like the proposed patch -- it improves a bit on technical accuracy but forgoes readability and still falls short on what stop=None does and on which arguments are required. That said, I don't really like the current version of the "roughly equivalent code either". It should probably be rewritten completely (no longer using slice() and it should show the actual looping logic. For clarity, it may be helpful to split it into two code-equivalents, one for the finite case (where stop is specified) and one for the infinite case (where stop is None). As it stands, the examples are beating the code equivalent when it comes to communicating what islice() does. I don't want to lengthen the code fragment unless it becomes *both* more communicative and more accurate. ---------- _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue11908> _______________________________________

Changes by Serhiy Storchaka <storchaka@gmail.com>: ---------- stage: commit review -> _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue11908> _______________________________________

Changes by Terry J. Reedy <tjreedy@udel.edu>: ---------- versions: -Python 3.2 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue11908> _______________________________________

Raymond Hettinger added the comment: Closing for the reasons lists above. ---------- resolution: -> rejected status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue11908> _______________________________________
participants (8)
-
Brian Brazil
-
Ezio Melotti
-
Mark Dickinson
-
Ram Rachum
-
Raymond Hettinger
-
Serhiy Storchaka
-
Terry J. Reedy
-
ysj.ray