Currently there's a mix of regex usage styles in autotest. Precompiled
regex should be preferred, specially when they are used many times.

Note 1: "many times" means not only multiple ocurrences in a source
file, but specially many times during the application execution, so
code in common_lib is very relevant.

Note 2: Python maintains a limited cache for not previously compiled
regex, but we should not count on cache hits, specially as autotest grows.

Signed-off-by: Cleber Rosa <[email protected]>
---
 client/common_lib/base_job.py |   13 +++++++------
 1 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/client/common_lib/base_job.py b/client/common_lib/base_job.py
index 3c77d38..4a2271c 100644
--- a/client/common_lib/base_job.py
+++ b/client/common_lib/base_job.py
@@ -422,6 +422,9 @@ class status_log_entry(object):
     TIMESTAMP_FIELD = 'timestamp'
     LOCALTIME_FIELD = 'localtime'
 
+    # non-space whitespace is forbidden in any fields
+    BAD_CHAR_REGEX = re.compile(r'[\t\n\r\v\f]')
+
     def __init__(self, status_code, subdir, operation, message, fields,
                  timestamp=None):
         """Construct a status.log entry.
@@ -439,18 +442,16 @@ class status_log_entry(object):
 
         @raise ValueError: if any of the parameters are invalid
         """
-        # non-space whitespace is forbidden in any fields
-        bad_char_regex = r'[\t\n\r\v\f]'
 
         if not log.is_valid_status(status_code):
             raise ValueError('status code %r is not valid' % status_code)
         self.status_code = status_code
 
-        if subdir and re.search(bad_char_regex, subdir):
+        if subdir and self.BAD_CHAR_REGEX.search(subdir):
             raise ValueError('Invalid character in subdir string')
         self.subdir = subdir
 
-        if operation and re.search(bad_char_regex, operation):
+        if operation and self.BAD_CHAR_REGEX.search(operation):
             raise ValueError('Invalid character in operation string')
         self.operation = operation
 
@@ -460,7 +461,7 @@ class status_log_entry(object):
         message_lines = message.split('\n')
         self.message = message_lines[0].replace('\t', ' ' * 8)
         self.extra_message_lines = message_lines[1:]
-        if re.search(bad_char_regex, self.message):
+        if self.BAD_CHAR_REGEX.search(self.message):
             raise ValueError('Invalid character in message %r' % self.message)
 
         if not fields:
@@ -468,7 +469,7 @@ class status_log_entry(object):
         else:
             self.fields = fields.copy()
         for key, value in self.fields.iteritems():
-            if re.search(bad_char_regex, key + value):
+            if self.BAD_CHAR_REGEX.search(key + value):
                 raise ValueError('Invalid character in %r=%r field'
                                  % (key, value))
 
-- 
1.7.1

_______________________________________________
Autotest mailing list
[email protected]
http://test.kernel.org/cgi-bin/mailman/listinfo/autotest

Reply via email to