aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Ganssle <paul@ganssle.io>2018-03-12 13:14:33 -0400
committerPaul Ganssle <paul@ganssle.io>2018-03-12 13:19:51 -0400
commit5f2052faf7532424203c23f979f1f8f7b2653571 (patch)
tree3cf334f4f8ff192bda129dbebf1e21c74ff98c0c
parent372783dcc72e24c42600cdba1f132b9a65f9d4c5 (diff)
downloaddateutil-5f2052faf7532424203c23f979f1f8f7b2653571.tar.gz
Catch non-ValueError exceptions in decimal
-rw-r--r--dateutil/parser/_parser.py17
-rw-r--r--dateutil/test/test_parser.py8
2 files changed, 22 insertions, 3 deletions
diff --git a/dateutil/parser/_parser.py b/dateutil/parser/_parser.py
index 1b5aabf..0eac592 100644
--- a/dateutil/parser/_parser.py
+++ b/dateutil/parser/_parser.py
@@ -836,7 +836,11 @@ class parser(object):
def _parse_numeric_token(self, tokens, idx, info, ymd, res, fuzzy):
# Token is a number
value_repr = tokens[idx]
- value = Decimal(value_repr)
+ try:
+ value = self._to_decimal(value_repr)
+ except Exception as e:
+ six.raise_from(ValueError('Unknown numeric token'), e)
+
len_li = len(value_repr)
len_l = len(tokens)
@@ -895,7 +899,7 @@ class parser(object):
elif idx + 2 < len_l and tokens[idx + 1] == ':':
# HH:MM[:SS[.ss]]
res.hour = int(value)
- value = Decimal(tokens[idx + 2]) # TODO: try/except for this?
+ value = self._to_decimal(tokens[idx + 2]) # TODO: try/except for this?
(res.minute, res.second) = self._parse_min_sec(value)
if idx + 4 < len_l and tokens[idx + 3] == ':':
@@ -996,7 +1000,7 @@ class parser(object):
def _assign_hms(self, res, value_repr, hms):
# See GH issue #427, fixing float rounding
- value = Decimal(value_repr)
+ value = self._to_decimal(value_repr)
if hms == 0:
# Hour
@@ -1196,6 +1200,13 @@ class parser(object):
return dt
+ def _to_decimal(self, val):
+ try:
+ return Decimal(val)
+ except Exception as e:
+ msg = "Could not convert %s to decimal" % val
+ six.raise_from(ValueError(msg), e)
+
DEFAULTPARSER = parser()
diff --git a/dateutil/test/test_parser.py b/dateutil/test/test_parser.py
index e3e24ba..3c7b403 100644
--- a/dateutil/test/test_parser.py
+++ b/dateutil/test/test_parser.py
@@ -1091,3 +1091,11 @@ def test_parse_tzinfos_fold():
])
def test_rounding_floatlike_strings(dtstr, dt):
assert parse(dtstr, default=datetime(2003, 9, 25)) == dt
+
+
+def test_decimal_error():
+ # GH 632 - decimal.Decimal raises some non-ValueError exception when
+ # constructed with an invalid value
+ with pytest.raises(ValueError):
+ parse('1: test')
+