From 79d2e486a34911ada6d81701f14dd3c4b9790afd Mon Sep 17 00:00:00 2001 From: Mark Bailey Date: Mon, 23 Dec 2019 22:46:03 +0000 Subject: [PATCH] Fix TypeError in parser wrapper logic In attempting to pass-through the string representation of an exception we are wrapping, we made the erroneous assumption that `args[0]` would always be a string (or something that can concatenate cleanly with a string). This turns out not to be the case with `IllegalMonthError`, where it is an integer, so to avoid raising an erroneous `TypeError`, we first convert the wrapped exception to a string. See GH issue #981. --- changelog.d/987.bugfix.rst | 4 ++++ dateutil/parser/_parser.py | 2 +- dateutil/test/test_parser.py | 4 ++++ 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 changelog.d/987.bugfix.rst diff --git a/changelog.d/987.bugfix.rst b/changelog.d/987.bugfix.rst new file mode 100644 index 000000000..8bc3cdd57 --- /dev/null +++ b/changelog.d/987.bugfix.rst @@ -0,0 +1,4 @@ +Fixed a bug in the parser where non-``ValueError`` exceptions would be raised +during exception handling; this would happen, for example, if an +``IllegalMonthError`` was raised in ``dateutil`` code. Fixed by Mark Bailey. +(gh issue #981, pr #987). diff --git a/dateutil/parser/_parser.py b/dateutil/parser/_parser.py index 458aa6a32..7fcfa5457 100644 --- a/dateutil/parser/_parser.py +++ b/dateutil/parser/_parser.py @@ -654,7 +654,7 @@ def parse(self, timestr, default=None, try: ret = self._build_naive(res, default) except ValueError as e: - six.raise_from(ParserError(e.args[0] + ": %s", timestr), e) + six.raise_from(ParserError(str(e) + ": %s", timestr), e) if not ignoretz: ret = self._build_tzaware(ret, res, tzinfos) diff --git a/dateutil/test/test_parser.py b/dateutil/test/test_parser.py index 726156c8d..605705e4c 100644 --- a/dateutil/test/test_parser.py +++ b/dateutil/test/test_parser.py @@ -743,6 +743,10 @@ def test_out_of_bound_day(self): with pytest.raises(ParserError): parse("Feb 30, 2007") + def test_illegal_month_error(self): + with pytest.raises(ParserError): + parse("0-100") + def test_day_sanity(self, fuzzy): dstr = "2014-15-25" with pytest.raises(ParserError):