Cache SafeRe patterns
This commit is contained in:
parent
d281f112d9
commit
0e930efd95
1 changed files with 9 additions and 2 deletions
|
@ -4,6 +4,8 @@ import re
|
||||||
class UnsafePatternError(Exception):
|
class UnsafePatternError(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
cached_patterns = {}
|
||||||
|
|
||||||
|
|
||||||
def isSafePattern(pattern):
|
def isSafePattern(pattern):
|
||||||
if len(pattern) > 255:
|
if len(pattern) > 255:
|
||||||
|
@ -16,5 +18,10 @@ def isSafePattern(pattern):
|
||||||
|
|
||||||
|
|
||||||
def match(pattern, *args, **kwargs):
|
def match(pattern, *args, **kwargs):
|
||||||
if isSafePattern(pattern):
|
cached_pattern = cached_patterns.get(pattern)
|
||||||
return re.match(pattern, *args, **kwargs)
|
if cached_pattern:
|
||||||
|
return cached_pattern.match(*args, **kwargs)
|
||||||
|
else:
|
||||||
|
if isSafePattern(pattern):
|
||||||
|
cached_patterns[pattern] = re.compile(pattern)
|
||||||
|
return cached_patterns[pattern].match(*args, **kwargs)
|
||||||
|
|
Loading…
Reference in a new issue