summaryrefslogtreecommitdiff
path: root/Lib/re.py
diff options
context:
space:
mode:
authorEzio Melotti <ezio.melotti@gmail.com>2011-03-25 14:25:36 +0200
committerEzio Melotti <ezio.melotti@gmail.com>2011-03-25 14:25:36 +0200
commit923a71c65f6be3e64f520ffab2d213714e563ca8 (patch)
treec6e567fa2bc5deb47db16f5f784d4068b23ab0d9 /Lib/re.py
parentcf694eaed00e0aff04efa1cd4206bf802af27d6d (diff)
parent9835e7067415d95345d77f28619897213ba0f775 (diff)
downloadcpython-923a71c65f6be3e64f520ffab2d213714e563ca8.tar.gz
#2650: Merge with 3.1.
Diffstat (limited to 'Lib/re.py')
-rw-r--r--Lib/re.py40
1 files changed, 12 insertions, 28 deletions
diff --git a/Lib/re.py b/Lib/re.py
index 309afef125..abd7ea27b3 100644
--- a/Lib/re.py
+++ b/Lib/re.py
@@ -118,6 +118,7 @@ This module also defines an exception 'error'.
import sys
import sre_compile
import sre_parse
+import functools
# public symbols
__all__ = [ "match", "search", "sub", "subn", "split", "findall",
@@ -205,9 +206,9 @@ def compile(pattern, flags=0):
return _compile(pattern, flags)
def purge():
- "Clear the regular expression cache"
- _cache.clear()
- _cache_repl.clear()
+ "Clear the regular expression caches"
+ _compile_typed.cache_clear()
+ _compile_repl.cache_clear()
def template(pattern, flags=0):
"Compile a template pattern, returning a pattern object"
@@ -248,20 +249,14 @@ def escape(pattern):
# --------------------------------------------------------------------
# internals
-_cache = {}
-_cache_repl = {}
-
_pattern_type = type(sre_compile.compile("", 0))
-_MAXCACHE = 100
+def _compile(pattern, flags):
+ return _compile_typed(type(pattern), pattern, flags)
-def _compile(*key):
+@functools.lru_cache(maxsize=500)
+def _compile_typed(text_bytes_type, pattern, flags):
# internal: compile pattern
- cachekey = (type(key[0]),) + key
- p = _cache.get(cachekey)
- if p is not None:
- return p
- pattern, flags = key
if isinstance(pattern, _pattern_type):
if flags:
raise ValueError(
@@ -269,23 +264,12 @@ def _compile(*key):
return pattern
if not sre_compile.isstring(pattern):
raise TypeError("first argument must be string or compiled pattern")
- p = sre_compile.compile(pattern, flags)
- if len(_cache) >= _MAXCACHE:
- _cache.clear()
- _cache[cachekey] = p
- return p
+ return sre_compile.compile(pattern, flags)
-def _compile_repl(*key):
+@functools.lru_cache(maxsize=500)
+def _compile_repl(repl, pattern):
# internal: compile replacement pattern
- p = _cache_repl.get(key)
- if p is not None:
- return p
- repl, pattern = key
- p = sre_parse.parse_template(repl, pattern)
- if len(_cache_repl) >= _MAXCACHE:
- _cache_repl.clear()
- _cache_repl[key] = p
- return p
+ return sre_parse.parse_template(repl, pattern)
def _expand(pattern, match, template):
# internal: match.expand implementation hook