next()
should be wrapped in try-except
1639
1640 # Try reading header
1641 reader = csv.reader(fileobj, store.dialect)
1642 header = next(reader)1643 fileobj.close()
1644
1645 # Check if the file is not two column only, in that case translate-toolkit detection
next()
should be wrapped in try-except
223 raise MemoryImportError(
224 gettext("Could not parse TMX file: %s") % error
225 ) from error
226 header = next(227 storage.document.getroot().iterchildren(storage.namespaced("header"))
228 )
229 lang_cache = {}
next()
should be wrapped in try-except
100 # value can be None or blank stringfor cells having formatting only,
101 # we need to ignore such columns as that would be treated like "" fields
102 # later in the translate-toolkit
103 fields = [cell.value for cell in next(worksheet.rows) if cell.value]104 for row in worksheet.rows:
105 values = [cell.value for cell in row]
106 values = values[: len(fields)]
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)
...