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.
This commit is contained in:
Bruno Cauet 2015-02-18 19:31:07 +01:00
parent e00d7b7ddc
commit 9e5e7a28e5
2 changed files with 14 additions and 1 deletions

View file

@ -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)

View file

@ -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__)