Skip to content

Commit

Permalink
Merge pull request #679 from amureki/issues/662/decimal_error
Browse files Browse the repository at this point in the history
Catch infinite decimal values in `_is_decimal` (fix #662)
  • Loading branch information
pganssle committed Apr 14, 2018
2 parents 7783055 + f943a7a commit e6644a4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ switch, and thus all their contributions are dual-licensed.
- Pierre Gergondet <pierre.gergondet@MASKED> (gh: @gergondet)
- Quentin Pradet <quentin@MASKED>
- Roy Williams <rwilliams@MASKED>
- Rustem Saiargaliev (gh: @amureki) **D**
- Savraj <savraj@MASKED>
- Sergey Vishnikin <armicron@MASKED>
- Stefan Bonchev **D**
Expand Down
1 change: 1 addition & 0 deletions changelog.d/0679.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed an issue where decimal.Decimal would cast `NaN` or infinite value in a parser.parse, which will raise decimal.Decimal-specific errors. Reported and fixed by @amureki (gh issue #662, gh pr #679).
7 changes: 6 additions & 1 deletion dateutil/parser/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1202,10 +1202,15 @@ def _assign_tzname(self, dt, tzname):

def _to_decimal(self, val):
try:
return Decimal(val)
decimal_value = Decimal(val)
# See GH 662, edge case, infinite value should not be converted via `_to_decimal`
if not decimal_value.is_finite():
raise ValueError("Converted decimal value is infinite or NaN")
except Exception as e:
msg = "Could not convert %s to decimal" % val
six.raise_from(ValueError(msg), e)
else:
return decimal_value


DEFAULTPARSER = parser()
Expand Down
8 changes: 4 additions & 4 deletions dateutil/test/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1093,9 +1093,9 @@ 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
@pytest.mark.parametrize('value', ['1: test', 'Nan'])
def test_decimal_error(value):
# GH 632, GH 662 - decimal.Decimal raises some non-ValueError exception when
# constructed with an invalid value
with pytest.raises(ValueError):
parse('1: test')

parse(value)

0 comments on commit e6644a4

Please sign in to comment.