next()
should be wrapped in try-except
607 rv = self._after
608 self._after = missing
609 else:
610 rv = next(self._iterator) 611
612 self.index0 += 1
613 self._before = self._current
next()
should be wrapped in try-except
372 }
373 and self.stream.look().test("name:context")
374 ):
375 node.with_context = next(self.stream).value == "with" 376 self.stream.skip()
377 return True
378 return False
next()
should be wrapped in try-except
951 negated = False
952 name = self.stream.expect("name").value
953 while self.stream.current.type == "dot":
954 next(self.stream) 955 name += "." + self.stream.expect("name").value
956 dyn_args = dyn_kwargs = None
957 kwargs = []
next()
should be wrapped in try-except
945 def parse_test(self, node: nodes.Expr) -> nodes.Expr:
946 token = next(self.stream)
947 if self.stream.current.test("name:not"):
948 next(self.stream) 949 negated = True
950 else:
951 negated = False
next()
should be wrapped in try-except
943 return node
944
945 def parse_test(self, node: nodes.Expr) -> nodes.Expr:
946 token = next(self.stream) 947 if self.stream.current.test("name:not"):
948 next(self.stream)
949 negated = True
Calls to next()
should be inside try-except
block.
When the iterator is exhausted, StopIteration
exception is raised. When used inside a generator, this can cause unexpected behavior. If not handled, it will propagate out of the generator causing termination. PEP-479 has been accepted to fix this problem. It will modify the behavior of StopIteration
in generators.
Each call to next()
should be wrapped in a try-except
block to explicitly handle StopIteration
exceptions.
def get_team_structure(team):
for team in class:
students = iter(team)
# StopIteration exception will be raised if team doesn't have members.
leader = next(students)
members = [m for m in students]
yield leader, members
def print_teams(teams):
for leader, members in get_team_structure(teams):
print("Leader: "leader)
for member in members:
print(member)
def get_team_structure(team):
for team in class:
# StopIteration exception will be raised if team has no members.
students = iter(team)
# Added `try` block, to guard against the exception
try:
leader = next(students)
except StopIteration:
# team has no members, skip it
continue
members = [m for m in students]
yield leader, members
def print_teams(teams):
for leader, members in get_team_structure(teams):
print("Leader: "leader)
for member in members:
print(member)
...