]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Pre-compiling regex that removes control characters.
authorBernardo Heynemann <heynemann@gmail.com>
Wed, 6 Feb 2013 17:35:35 +0000 (15:35 -0200)
committerBernardo Heynemann <heynemann@gmail.com>
Wed, 6 Feb 2013 17:35:35 +0000 (15:35 -0200)
Given that this regex might be used a lot in the lifetime of a given server (each request that uses get_argument for unicode strings) it seems sensible to store the compiled version of the regex.

tornado/web.py

index 35f4e09824a679c6e24efd43e0e7364874b7f996..3cccba5645c7c87ac00b9d9ef461a7054504f0b0 100644 (file)
@@ -114,6 +114,7 @@ class RequestHandler(object):
 
     _template_loaders = {}  # {path: template.BaseLoader}
     _template_loader_lock = threading.Lock()
+    _remove_control_chars_regex = re.compile(r"[\x00-\x08\x0e-\x1f]")
 
     def __init__(self, application, request, **kwargs):
         super(RequestHandler, self).__init__()
@@ -342,13 +343,14 @@ class RequestHandler(object):
 
         The returned values are always unicode.
         """
+
         values = []
         for v in self.request.arguments.get(name, []):
             v = self.decode_argument(v, name=name)
             if isinstance(v, unicode_type):
                 # Get rid of any weird control chars (unless decoding gave
                 # us bytes, in which case leave it alone)
-                v = re.sub(r"[\x00-\x08\x0e-\x1f]", " ", v)
+                v = RequestHandler._remove_control_chars_regex.sub(" ", v)
             if strip:
                 v = v.strip()
             values.append(v)