Call to
next()
should be wrapped in try-except
112 assert not (np.isnan(v_max) or np.isnan(n_v_max)), locals()
113 assert v_max == vround(v_max), (v_max, vround(v_max))
114 _i = wot.loc[roots_head[0] :, w.p_remain_stable].iteritems()
115 assert next(_i)[1] > 0 and next(_i)[1] <= 0, (116 "Solution is not the last positive p_remain:",
117 roots_head[0],
118 v_max,
Call to
next()
should be wrapped in try-except
112 assert not (np.isnan(v_max) or np.isnan(n_v_max)), locals()
113 assert v_max == vround(v_max), (v_max, vround(v_max))
114 _i = wot.loc[roots_head[0] :, w.p_remain_stable].iteritems()
115 assert next(_i)[1] > 0 and next(_i)[1] <= 0, (116 "Solution is not the last positive p_remain:",
117 roots_head[0],
118 v_max,
Call to
next()
should be wrapped in try-except
246 fname_genor = utils.generate_filenames(os.path.join(dest_dir, dest_fname))
247 dest_fname = next(fname_genor)
248 while os.path.exists(dest_fname):
249 dest_fname = next(fname_genor)250
251 log.info("Copying `xlwings` template-file: %s --> %s", src_fname, dest_fname)
252 shutil.copy(src_fname, dest_fname)
Call to
next()
should be wrapped in try-except
244 for src_fname in files_to_copy:
245 dest_fname = os.path.basename(src_fname)
246 fname_genor = utils.generate_filenames(os.path.join(dest_dir, dest_fname))
247 dest_fname = next(fname_genor)248 while os.path.exists(dest_fname):
249 dest_fname = next(fname_genor)
250
Description
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.
Bad practice
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)
Recommended:
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)
...