[CalendarServer-changes] [13116] twext/trunk/twext/who

source_changes at macosforge.org source_changes at macosforge.org
Wed Apr 2 07:46:20 PDT 2014


Revision: 13116
          http://trac.calendarserver.org//changeset/13116
Author:   sagen at apple.com
Date:     2014-04-02 07:46:19 -0700 (Wed, 02 Apr 2014)
Log Message:
-----------
Handle MatchExpressions with non-unicode values and caseInsensitive match flag

Modified Paths:
--------------
    twext/trunk/twext/who/expression.py
    twext/trunk/twext/who/test/test_expression.py

Modified: twext/trunk/twext/who/expression.py
===================================================================
--- twext/trunk/twext/who/expression.py	2014-04-02 01:16:23 UTC (rev 13115)
+++ twext/trunk/twext/who/expression.py	2014-04-02 14:46:19 UTC (rev 13116)
@@ -135,6 +135,16 @@
         predicate = lambda x: x
         normalize = lambda x: x
 
+        def maybeLower(x):
+            """
+            Not all values have lower(), so handle that case by returning the
+            original
+            """
+            try:
+                return x.lower()
+            except AttributeError:
+                return x
+
         if flags is None:
             flags = FlagConstant()
         else:
@@ -142,7 +152,7 @@
                 if flag == MatchFlags.NOT:
                     predicate = lambda x: not x
                 elif flag == MatchFlags.caseInsensitive:
-                    normalize = lambda x: x.lower()
+                    normalize = maybeLower
                 else:
                     raise NotImplementedError(
                         "Unknown query flag: {0}".format(describe(flag))
@@ -271,8 +281,6 @@
         def match(a, b):
             matchType = self.matchType
 
-            # import pdb; pdb.set_trace()
-
             if matchType == MatchType.equals:
                 return a == b
 

Modified: twext/trunk/twext/who/test/test_expression.py
===================================================================
--- twext/trunk/twext/who/test/test_expression.py	2014-04-02 01:16:23 UTC (rev 13115)
+++ twext/trunk/twext/who/test/test_expression.py	2014-04-02 14:46:19 UTC (rev 13116)
@@ -19,6 +19,7 @@
 """
 
 from twisted.trial import unittest
+from uuid import UUID
 
 from ..idirectory import FieldName
 from ..expression import MatchExpression, MatchType, MatchFlags
@@ -361,3 +362,20 @@
         self.assertFalse(expression.match(u"Wilfredo"))
         self.assertFalse(expression.match(u"Morgen"))
         self.assertTrue(expression.match(u"Glyph"))
+
+
+    def test_non_unicode_value(self):
+        """
+        L{MatchExpression.match} with L{MatchType.equals},
+        L{MatchFlags.caseInsensitive} and a UUID value (which cannot be
+        lower()'d)
+        """
+        uuidValue = UUID("95F868E5-EFBD-4BFE-8DFB-25C3BC5CCDDA")
+        expression = MatchExpression(
+            FieldName.guid, uuidValue,
+            matchType=MatchType.equals,
+            flags=MatchFlags.caseInsensitive,
+
+        )
+        self.assertFalse(expression.match(u"Bogus"))
+        self.assertTrue(expression.match(uuidValue))
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140402/4432c7b1/attachment-0001.html>


More information about the calendarserver-changes mailing list