Malformed date queries no longer crash

This commit is contained in:
Adrian Sampson 2014-10-04 16:50:50 -07:00
parent 44ff3f782a
commit 2b1353a4f1
3 changed files with 17 additions and 3 deletions

View file

@ -418,10 +418,14 @@ class Period(object):
return None
ordinal = string.count('-')
if ordinal >= len(cls.date_formats):
raise ValueError('date is not in one of the formats '
+ ', '.join(cls.date_formats))
# Too many components.
return None
date_format = cls.date_formats[ordinal]
date = datetime.strptime(string, date_format)
try:
date = datetime.strptime(string, date_format)
except ValueError:
# Parsing failed.
return None
precision = cls.precisions[ordinal]
return cls(date, precision)

View file

@ -26,6 +26,8 @@ Fixes:
the player.
* The importer no longer tries to highlight partial differences in numeric
quantities (track numbers and durations), which was often confusing.
* Date-based queries that are malformed (not parse-able) no longer crash
beets and instead fail silently.
1.3.8 (September 17, 2014)

View file

@ -112,6 +112,14 @@ class DateQueryTest(_common.LibTestCase):
self.assertEqual(len(matched), 0)
class DateQueryConstructTest(unittest.TestCase):
def test_long_numbers(self):
DateQuery('added', '1409830085..1412422089')
def test_too_many_components(self):
DateQuery('added', '12-34-56-78')
def suite():
return unittest.TestLoader().loadTestsFromName(__name__)