Skip to content

Commit

Permalink
Merge pull request #2153 from purificant/pep479
Browse files Browse the repository at this point in the history
PEP 479 amends
  • Loading branch information
stevenbird committed Oct 20, 2018
2 parents ab77c63 + 8fbf705 commit e7f4635
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 6 additions & 1 deletion nltk/toolbox.py
Expand Up @@ -69,7 +69,12 @@ def raw_fields(self):
# need to get first line outside the loop for correct handling
# of the first marker if it spans multiple lines
file_iter = iter(self._file)
line = next(file_iter)
# PEP 479, prevent RuntimeError when StopIteration is raised inside generator
try:
line = next(file_iter)
except StopIteration:
# no more data is available, terminate the generator
return
mobj = re.match(first_line_pat, line)
mkr, line_value = mobj.groups()
value_lines = [line_value,]
Expand Down
8 changes: 7 additions & 1 deletion nltk/util.py
Expand Up @@ -464,7 +464,13 @@ def ngrams(sequence, n, pad_left=False, pad_right=False,

history = []
while n > 1:
history.append(next(sequence))
# PEP 479, prevent RuntimeError from being raised when StopIteration bubbles out of generator
try:
next_item = next(sequence)
except StopIteration:
# no more data, terminate the generator
return
history.append(next_item)
n -= 1
for item in sequence:
history.append(item)
Expand Down

0 comments on commit e7f4635

Please sign in to comment.