summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorMartin <martin@leo.fri1.uni-lj.si>2015-10-09 16:35:59 +0200
committerMartin <martin@leo.fri1.uni-lj.si>2015-10-09 16:35:59 +0200
commit3c1a60618c837ad7c19675b2e1626a480c194beb (patch)
tree90555c634ead46272d338ca96764e4a6d9cb99d0 /python
parent76bfb287b51bd97c9ca0bfc4e7760b7ee8e15b47 (diff)
Added exception handling to get_tokens functions.
Diffstat (limited to 'python')
-rw-r--r--python/util.py11
1 files changed, 7 insertions, 4 deletions
diff --git a/python/util.py b/python/util.py
index 78fd362..ce89558 100644
--- a/python/util.py
+++ b/python/util.py
@@ -2,12 +2,15 @@
import io
import re
-from tokenize import tokenize
+from tokenize import tokenize, TokenError
def get_tokens(code):
""" Gets a list of tokens. """
- stream = io.BytesIO(code.encode('utf-8'))
- return [t.string for t in tokenize(stream.readline) if t.string]
+ try:
+ stream = io.BytesIO(code.encode('utf-8'))
+ return [t.string for t in tokenize(stream.readline) if t.string]
+ except TokenError:
+ return []
# Check if tokens contain a sequence of tokens (given as a list of strings).
def has_token_sequence(tokens, sequence):
@@ -37,7 +40,7 @@ def get_numbers(s):
''', s, re.VERBOSE)
return [float(v) for v in str_vals]
-def string_almost_equal(s, a, prec=333):
+def string_almost_equal(s, a, prec=3):
""" Searches string s for a value that is almost equal to a. """
for v in get_numbers(s):
if almost_equal(v, a, prec):