Skip to content

parser raises TypeError in wrapper logic #981

@pganssle

Description

@pganssle

Apparently sometimes the parser catches a type of ValueError other than the ones we raise - specifically IllegalMonthError, which takes as an argument something other than a string. Minimal reproducer:

>>> from dateutil import parser
>>> parser.parse("0-100")
>>> dateutil.parser.parse("0-100")

This raises the following error:

Details
---------------------------------------------------------------------------
IllegalMonthError                         Traceback (most recent call last)
/usr/lib/python3.7/site-packages/dateutil/parser/_parser.py in parse(self, timestr, default, ignoretz, tzinfos, **kwargs)
    654         try:
--> 655             ret = self._build_naive(res, default)
    656         except ValueError as e:

/usr/lib/python3.7/site-packages/dateutil/parser/_parser.py in _build_naive(self, res, default)
   1237
-> 1238             if cday > monthrange(cyear, cmonth)[1]:
   1239                 repl['day'] = monthrange(cyear, cmonth)[1]

/usr/lib/python3.7/calendar.py in monthrange(year, month)
    123     if not 1 <= month <= 12:
--> 124         raise IllegalMonthError(month)
    125     day1 = weekday(year, month, 1)

IllegalMonthError: bad month number 0; must be 1-12

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-3-f7db2d9dc78c> in <module>
----> 1 dateutil.parser.parse("0-100")

/usr/lib/python3.7/site-packages/dateutil/parser/_parser.py in parse(timestr, parserinfo, **kwargs)
   1372         return parser(parserinfo).parse(timestr, **kwargs)
   1373     else:
-> 1374         return DEFAULTPARSER.parse(timestr, **kwargs)
   1375
   1376

/usr/lib/python3.7/site-packages/dateutil/parser/_parser.py in parse(self, timestr, default, ignoretz, tzinfos, **kwargs)
    655             ret = self._build_naive(res, default)
    656         except ValueError as e:
--> 657             six.raise_from(ParserError(e.args[0] + ": %s", timestr), e)
    658
    659         if not ignoretz:

TypeError: unsupported operand type(s) for +: 'int' and 'str'

The core of the issue is this line:

        try:
            ret = self._build_naive(res, default)
        except ValueError as e:
            six.raise_from(ParserError(e.args[0] + ": %s", timestr), e)

Since e.args[0] is apparently occasionally an integer.

I guess we can solve this this way:

except ValueError as e:
    six.raise_from(ParseError(str(e) + ": %s", timestr), e)

This will need a regression test. This is apparently the cause of matplotlib/matplotlib#15726.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions