From 9e5e7a28e5574b45b92e5a282abbd796cbb18b28 Mon Sep 17 00:00:00 2001 From: Bruno Cauet Date: Wed, 18 Feb 2015 19:31:07 +0100 Subject: [PATCH] InvalidQueryError: resist to any query Even though queries may not contain non-utf8 code points InvalidQueryError ought to be prudent, for such an invalid query would raise an InvalidQueryError which therefore has to be able to manipulate the invalid query. --- beets/dbcore/query.py | 8 +++++++- test/test_library.py | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/beets/dbcore/query.py b/beets/dbcore/query.py index cd891148e..e80010ccf 100644 --- a/beets/dbcore/query.py +++ b/beets/dbcore/query.py @@ -37,7 +37,13 @@ class InvalidQueryError(ParsingError): def __init__(self, query, explanation): if isinstance(query, list): query = " ".join(query) - message = "'{0}': {1}".format(query, explanation) + try: + message = "'{0}': {1}".format(query, explanation) + except UnicodeDecodeError: + # queries are unicode. however if for an unholy reason it's not + # the case, an InvalidQueryError may be raised -- and report it + # correctly than fail again here + message = "{0!r}: {1}".format(query, explanation) super(InvalidQueryError, self).__init__(message) diff --git a/test/test_library.py b/test/test_library.py index 6bb88076e..d2193b25f 100644 --- a/test/test_library.py +++ b/test/test_library.py @@ -1195,6 +1195,13 @@ class ParseQueryTest(unittest.TestCase): self.assertIsInstance(raised.exception, beets.dbcore.query.ParsingError) + def test_parse_byte_string(self): + with self.assertRaises(beets.dbcore.InvalidQueryError) as raised: + beets.library.parse_query_string(b'f\xf2o', None) + self.assertIn("can't decode", unicode(raised.exception)) + self.assertIsInstance(raised.exception, + beets.dbcore.query.ParsingError) + def suite(): return unittest.TestLoader().loadTestsFromName(__name__)