diff options
author | Guido van Rossum <guido@python.org> | 1997-07-15 18:59:04 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1997-07-15 18:59:04 (GMT) |
commit | 26d80e67148b7c61fda71490e5b06b0616aeb45e (patch) | |
tree | e8ec59cd69c1c9ded75f797c3dab6cb65aef889b /Lib | |
parent | e8b81313db1af218f03e30a3bc8f30376a35ca3b (diff) | |
download | cpython-26d80e67148b7c61fda71490e5b06b0616aeb45e.zip cpython-26d80e67148b7c61fda71490e5b06b0616aeb45e.tar.gz cpython-26d80e67148b7c61fda71490e5b06b0616aeb45e.tar.bz2 |
Correct inf. while loop.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/re.py | 25 |
1 files changed, 20 insertions, 5 deletions
@@ -58,20 +58,34 @@ def valid_identifier(id): # # +_cache = {} +_MAXCACHE = 20 +def _cachecompile(pattern, flags): + key = (pattern, flags) + try: + return _cache[key] + except KeyError: + pass + value = compile(pattern, flags) + if len(_cache) >= _MAXCACHE: + _cache.clear() + _cache[key] = value + return value + def match(pattern, string, flags=0): - return compile(pattern, flags).match(string) + return _cachecompile(pattern, flags).match(string) def search(pattern, string, flags=0): - return compile(pattern, flags).search(string) + return _cachecompile(pattern, flags).search(string) def sub(pattern, repl, string, count=0): - return compile(pattern).sub(repl, string, count) + return _cachecompile(pattern).sub(repl, string, count) def subn(pattern, repl, string, count=0): - return compile(pattern).subn(repl, string, count) + return _cachecompile(pattern).subn(repl, string, count) def split(pattern, string, maxsplit=0): - return compile(pattern).subn(string, maxsplit) + return _cachecompile(pattern).subn(string, maxsplit) # # @@ -1064,6 +1078,7 @@ def compile(pattern, flags=0): Label(label)] + \ stack[-1] + \ [Label(label + 1)] + max = max - 1 label = label + 2 del stack[-1] stack.append(expr) |