Skip to content

Commit

Permalink
Merge pull request #2851 from DimitriPapadopoulos/lgtm_errors
Browse files Browse the repository at this point in the history
LGTM.com errors
  • Loading branch information
dannysepler committed Oct 10, 2021
2 parents 2538164 + 8ce97b2 commit 9f468d3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
4 changes: 2 additions & 2 deletions nltk/internals.py
Expand Up @@ -238,11 +238,11 @@ def read_str(s, start_position):
break

# Process it, using eval. Strings with invalid escape sequences
# might raise ValueEerror.
# might raise ValueError.
try:
return eval(s[start_position : match.end()]), match.end()
except ValueError as e:
raise ReadError("invalid string (%s)" % e) from e
raise ReadError("valid escape sequence", start_position) from e


_READ_INT_RE = re.compile(r"-?\d+")
Expand Down
20 changes: 20 additions & 0 deletions nltk/test/internals.doctest
Expand Up @@ -138,3 +138,23 @@ It works for classic classes, too:
False
>>> overridden(D().f)
True


read_str()
~~~~~~~~~~~~
>>> from nltk.internals import read_str

Test valid scenarios

>>> read_str("'valid string'", 0)
('valid string', 14)

Now test invalid scenarios
>>> read_str("should error", 0)
Traceback (most recent call last):
...
nltk.internals.ReadError: Expected open quote at 0
>>> read_str("'should error", 0)
Traceback (most recent call last):
...
nltk.internals.ReadError: Expected close quote at 1
5 changes: 4 additions & 1 deletion nltk/util.py
Expand Up @@ -1013,7 +1013,7 @@ def skipgrams(sequence, n, k, **kwargs):
######################################################################

# inherited from pywordnet, by Oliver Steele
def binary_search_file(file, key, cache={}, cacheDepth=-1):
def binary_search_file(file, key, cache=None, cacheDepth=-1):
"""
Return the line from the file with first word key.
Searches through a sorted file using the binary search algorithm.
Expand All @@ -1036,6 +1036,9 @@ def binary_search_file(file, key, cache={}, cacheDepth=-1):
end = file.tell() - 1
file.seek(0)

if cache is None:
cache = {}

while start < end:
lastState = start, end
middle = (start + end) // 2
Expand Down

0 comments on commit 9f468d3

Please sign in to comment.