diff options
author | Paul Ganssle <paul@ganssle.io> | 2018-03-12 13:14:33 -0400 |
---|---|---|
committer | Paul Ganssle <paul@ganssle.io> | 2018-03-12 13:19:51 -0400 |
commit | 5f2052faf7532424203c23f979f1f8f7b2653571 (patch) | |
tree | 3cf334f4f8ff192bda129dbebf1e21c74ff98c0c /dateutil/parser | |
parent | 372783dcc72e24c42600cdba1f132b9a65f9d4c5 (diff) | |
download | dateutil-5f2052faf7532424203c23f979f1f8f7b2653571.tar.gz |
Catch non-ValueError exceptions in decimal
Diffstat (limited to 'dateutil/parser')
-rw-r--r-- | dateutil/parser/_parser.py | 17 |
1 files changed, 14 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() |