summaryrefslogtreecommitdiff
path: root/Doc/library/itertools.rst
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2014-11-22 21:56:23 -0800
committerRaymond Hettinger <python@rcn.com>2014-11-22 21:56:23 -0800
commit828d932a2c4da5eb7c05e85dfe51f5f6db68084d (patch)
treeb5ef3264ae6c338c1dbdf019ded3b98c3a8b123c /Doc/library/itertools.rst
parent1bf472933bb66ba146ed5938cb8beb06e67bd234 (diff)
downloadcpython-git-828d932a2c4da5eb7c05e85dfe51f5f6db68084d.tar.gz
PEP 479: Don't let StopIteration bubble out of calls to next() inside a generator.
Diffstat (limited to 'Doc/library/itertools.rst')
-rw-r--r--Doc/library/itertools.rst20
1 files changed, 16 insertions, 4 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index c5ba2eb596..8c7592d17e 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -104,7 +104,10 @@ loops that truncate the stream.
# accumulate([1,2,3,4,5]) --> 1 3 6 10 15
# accumulate([1,2,3,4,5], operator.mul) --> 1 2 6 24 120
it = iter(iterable)
- total = next(it)
+ try:
+ total = next(it)
+ except StopIteration:
+ return
yield total
for element in it:
total = func(total, element)
@@ -405,7 +408,10 @@ loops that truncate the stream.
def _grouper(self, tgtkey):
while self.currkey == tgtkey:
yield self.currvalue
- self.currvalue = next(self.it) # Exit on StopIteration
+ try:
+ self.currvalue = next(self.it)
+ except StopIteration:
+ return
self.currkey = self.keyfunc(self.currvalue)
@@ -429,7 +435,10 @@ loops that truncate the stream.
# islice('ABCDEFG', 0, None, 2) --> A C E G
s = slice(*args)
it = iter(range(s.start or 0, s.stop or sys.maxsize, s.step or 1))
- nexti = next(it)
+ try:
+ nexti = next(it)
+ except StopIteration:
+ return
for i, element in enumerate(iterable):
if i == nexti:
yield element
@@ -587,7 +596,10 @@ loops that truncate the stream.
def gen(mydeque):
while True:
if not mydeque: # when the local deque is empty
- newval = next(it) # fetch a new value and
+ try:
+ newval = next(it) # fetch a new value and
+ except StopIteration:
+ return
for d in deques: # load it to all the deques
d.append(newval)
yield mydeque.popleft()