Skip to content

Commit

Permalink
Change StopIteration to return in generators
Browse files Browse the repository at this point in the history
Python 3.7 changed the way generators work.  Code like this:

```
def gen():
    yield 'a'
    yield 'b'
    raise StopIteration

for i in gen():
    print(i)
```

causes an exception in python 3.7:

```
(py3) root@f36c5a2313f9:/wbia/wildbook-ia# python3.6 /tmp/test_generator.py
a
b
(py3) root@f36c5a2313f9:/wbia/wildbook-ia# python3.7 /tmp/test_generator.py
a
b
Traceback (most recent call last):
  File "/tmp/test_generator.py", line 4, in gen
    raise StopIteration()
StopIteration

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/tmp/test_generator.py", line 6, in <module>
    for i in gen():
RuntimeError: generator raised StopIteration
```

It can be fixed by doing `return` instead of `raise StopIteration`.
  • Loading branch information
karenc authored and bluemellophone committed Oct 13, 2020
1 parent 4822ce2 commit 94e59a3
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion wbia/algo/graph/nx_edge_augmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ def greedy_k_edge_augmentation(G, k, avail=None, weight=None, seed=None):

done = is_k_edge_connected(G, k)
if done:
raise StopIteration()
return
if avail is None:
# all edges are available
avail_uv = list(complement_edges(G))
Expand Down
2 changes: 1 addition & 1 deletion wbia/algo/graph/nx_edge_kcomponents.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ def general_k_edge_subgraphs(G, k):
if G.number_of_nodes() < k:
for node in G.nodes():
yield G.subgraph([node]).copy()
raise StopIteration()
return

# Intermediate results
R0 = {G.subgraph(cc).copy() for cc in find_ccs(G)}
Expand Down
2 changes: 1 addition & 1 deletion wbia/algo/verif/clf_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1514,7 +1514,7 @@ def gen_one_vs_rest_labels(labels):
"""
if labels.target_type == 'binary':
yield labels
raise StopIteration()
return
task_names_1vR = labels.one_vs_rest_task_names()
for k in range(labels.n_classes):
class_name = labels.class_names[k]
Expand Down
2 changes: 1 addition & 1 deletion wbia/guitool/PrefWidget2.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ def _set_to_external(self, cfg):

def iter_children(self):
if self.children is None:
raise StopIteration()
return
for child in self.children:
yield child

Expand Down

0 comments on commit 94e59a3

Please sign in to comment.