summary refs log tree commit diff
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2017-03-28 13:03:50 +0100
committerErik Johnston <erik@matrix.org>2017-03-28 13:27:21 +0100
commit650f0e69f2ff1c15739868c0e1a639d70ac13dbf (patch)
tree5fd92690d2a4fed1e61b8490bdefba7920577285
parentMerge pull request #2070 from matrix-org/erikj/perf_send (diff)
downloadsynapse-650f0e69f2ff1c15739868c0e1a639d70ac13dbf.tar.xz
Compile the regex's used in ASes
-rw-r--r--synapse/appservice/__init__.py14
-rw-r--r--tests/appservice/test_appservice.py4
2 files changed, 8 insertions, 10 deletions
diff --git a/synapse/appservice/__init__.py b/synapse/appservice/__init__.py
index b0106a3597..1e298ccf36 100644
--- a/synapse/appservice/__init__.py
+++ b/synapse/appservice/__init__.py
@@ -124,22 +124,18 @@ class ApplicationService(object):
                     raise ValueError(
                         "Expected bool for 'exclusive' in ns '%s'" % ns
                     )
-                if not isinstance(regex_obj.get("regex"), basestring):
+                regex = regex_obj.get("regex")
+                if isinstance(regex, basestring):
+                    regex_obj["regex"] = re.compile(regex)
+                else:
                     raise ValueError(
                         "Expected string for 'regex' in ns '%s'" % ns
                     )
         return namespaces
 
     def _matches_regex(self, test_string, namespace_key, return_obj=False):
-        if not isinstance(test_string, basestring):
-            logger.error(
-                "Expected a string to test regex against, but got %s",
-                test_string
-            )
-            return False
-
         for regex_obj in self.namespaces[namespace_key]:
-            if re.match(regex_obj["regex"], test_string):
+            if regex_obj["regex"].match(test_string):
                 if return_obj:
                     return regex_obj
                 return True
diff --git a/tests/appservice/test_appservice.py b/tests/appservice/test_appservice.py
index aa8cc50550..7586ea9053 100644
--- a/tests/appservice/test_appservice.py
+++ b/tests/appservice/test_appservice.py
@@ -19,10 +19,12 @@ from twisted.internet import defer
 from mock import Mock
 from tests import unittest
 
+import re
+
 
 def _regex(regex, exclusive=True):
     return {
-        "regex": regex,
+        "regex": re.compile(regex),
         "exclusive": exclusive
     }