Invalid strings in Dateutil 2.2 now cause TypeError instead of ValueError

Bug #1247643 reported by James Crasta
66
This bug affects 12 people
Affects Status Importance Assigned to Milestone
dateutil
Fix Released
Undecided
Unassigned

Bug Description

Traceback in dateutil 2.1 looks like this:

>>> import dateutil
>>> dateutil.__version__
'2.1'
>>> from dateutil import parser
>>> parser.parse('2013-01-01')
datetime.datetime(2013, 1, 1, 0, 0)
>>> parser.parse('yesterday')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/james/scripts/test_env/lib/python2.7/site-packages/dateutil/parser.py", line 720, in parse
    return DEFAULTPARSER.parse(timestr, **kwargs)
  File "/Users/james/scripts/test_env/lib/python2.7/site-packages/dateutil/parser.py", line 310, in parse
    raise ValueError("unknown string format")
ValueError: unknown string format

And in dateutil 2.2 looks like this:
>>> import dateutil
>>> dateutil.__version__
'2.2'
>>> from dateutil import parser
>>> parser.parse('yesterday')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/james/scripts/python_env/lib/python2.7/site-packages/dateutil/parser.py", line 748, in parse
    return DEFAULTPARSER.parse(timestr, **kwargs)
  File "/Users/james/scripts/python_env/lib/python2.7/site-packages/dateutil/parser.py", line 310, in parse
    res, skipped_tokens = self._parse(timestr, **kwargs)
TypeError: 'NoneType' object is not iterable

It works for a number of unknown strings, like 'fooflaf', 'hello my name is bob' or anything else seemingly that starts without a number.

Revision history for this message
DragonDave (2-dragon) wrote :

Here's a patch which works for me and passes all the tests.
Essentially, the issue is that _parse returns None in a number of instances, but the interface has changed and a 2-tuple is now expected. It's essentially saying:

a, b = None

which fails miserably. Instead we return (None, None).

Revision history for this message
Jon Dufresne (jdufresne) wrote :

Recently ran into this issue. The end result is I can't reliably pass user data to dateutil.parser.parse(), as it may contain an invalid string, but I can't catch the correct exception to handle it.

Any chance of the fix being accepted and rolled into a new version?

Revision history for this message
jarondl (jarondl) wrote :

fixed on GH: https://github.com/dateutil/dateutil/commit/08679489e84595298b98257039e1afdbdbd0ab46
the GH branch will soon become the official one

Revision history for this message
jarondl (jarondl) wrote :

Fixed on 2.3

Changed in dateutil:
status: New → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.