changeset 3408:7fd796b2e573

queryparser: catch exceptions, raise exception on '""'
author Johannes Berg <johannes AT sipsolutions DOT net>
date Sat, 22 Mar 2008 12:43:01 +0100
parents c3aa12fcdf12
children 1c5bb02539cf
files MoinMoin/search/_tests/test_search.py MoinMoin/search/queryparser.py
diffstat 2 files changed, 17 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/search/_tests/test_search.py	Sat Mar 22 11:54:56 2008 +0100
+++ b/MoinMoin/search/_tests/test_search.py	Sat Mar 22 12:43:01 2008 +0100
@@ -45,7 +45,6 @@
             ("(HelpOn) and (Administration) or (Configuration)", '[["HelpOn" "Administration"] or "Configuration"]'),
             ("(a) and (b) or (c) or -d", '[["a" "b"] or "c" or -"d"]'),
             ("a b c d e or f g h", '[["a" "b" "c" "d" "e"] or ["f" "g" "h"]]'),
-            ('""', '""""'),
             ('"no', '""no"'),
             ('no"', '"no""'),
             ("'no", "\"'no\""),
@@ -55,6 +54,13 @@
             result = parser.parse_query(query)
             assert str(result) == wanted
 
+    def testQueryParserExceptions(self):
+        """ search: test the query parser """
+        parser = search.QueryParser()
+        def _test(q):
+            py.test.raises(ValueError, parser.parse_query, q)
+        for query in ['""', '(', ')', '(a or b']:
+            yield _test, query
 
 class TestSearch:
     """ search: test search """
--- a/MoinMoin/search/queryparser.py	Sat Mar 22 11:54:56 2008 +0100
+++ b/MoinMoin/search/queryparser.py	Sat Mar 22 12:43:01 2008 +0100
@@ -973,9 +973,6 @@
                                 terms = sub[0]
                             orexpr = OrExpression(terms)
                         terms = AndExpression(orexpr)
-                    else:
-                        # XXX raise InvalidQueryException
-                        pass
                     remaining = self._analyse_items(items)
                     if remaining.__class__ == OrExpression:
                         for sub in remaining.subterms():
@@ -991,7 +988,7 @@
                     # being parsed rather than rejecting an empty string
                     # before parsing...
                     if not item:
-                        item = '""'
+                        raise ValueError("Term too short")
                     terms.append(TextSearch(item))
             elif isinstance(item, tuple):
                 negate = item[0] == M
@@ -1054,11 +1051,14 @@
         """ transform an string into a tree of Query objects """
         if isinstance(query, str):
             query = query.decode(config.charset)
-        items = wikiutil.parse_quoted_separated_ext(query,
-                                                    name_value_separator=':',
-                                                    prefixes='-',
-                                                    multikey=True,
-                                                    brackets=('()', ),
-                                                    quotes='\'"')
+        try:
+            items = wikiutil.parse_quoted_separated_ext(query,
+                                                        name_value_separator=':',
+                                                        prefixes='-',
+                                                        multikey=True,
+                                                        brackets=('()', ),
+                                                        quotes='\'"')
+        except wikiutil.BracketError:
+            raise ValueError()
         query = self._analyse_items(items)
         return query