From 2b1353a4f1b0caa48d6b9c149c49a20eba84201b Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Sat, 4 Oct 2014 16:50:50 -0700 Subject: [PATCH] Malformed date queries no longer crash --- beets/dbcore/query.py | 10 +++++++--- docs/changelog.rst | 2 ++ test/test_datequery.py | 8 ++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/beets/dbcore/query.py b/beets/dbcore/query.py index 5a6dff054..5a116eb2b 100644 --- a/beets/dbcore/query.py +++ b/beets/dbcore/query.py @@ -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) diff --git a/docs/changelog.rst b/docs/changelog.rst index 965a31bf0..be6f77bf5 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -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) diff --git a/test/test_datequery.py b/test/test_datequery.py index 7cff6e292..61f6abe2e 100644 --- a/test/test_datequery.py +++ b/test/test_datequery.py @@ -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__)