summaryrefslogtreecommitdiffstats
path: root/Lib/regsub.py
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>1997-02-18 18:52:55 (GMT)
committerBarry Warsaw <barry@python.org>1997-02-18 18:52:55 (GMT)
commitb67a25c0794d4a2c4f4907a8eb021750c11b1de4 (patch)
tree83573561ad811a8afd7d3cc15be97e9ee75210ff /Lib/regsub.py
parent909d7c3284c84dd50574aca78c59ed4fea3de4b0 (diff)
downloadcpython-b67a25c0794d4a2c4f4907a8eb021750c11b1de4.zip
cpython-b67a25c0794d4a2c4f4907a8eb021750c11b1de4.tar.gz
cpython-b67a25c0794d4a2c4f4907a8eb021750c11b1de4.tar.bz2
Store the current regex syntax along with the regular expression
string as the key to the cache. This means that changing the syntax will return the correct compiled pattern. clear_cache(): New function.
Diffstat (limited to 'Lib/regsub.py')
-rw-r--r--Lib/regsub.py25
1 files changed, 15 insertions, 10 deletions
diff --git a/Lib/regsub.py b/Lib/regsub.py
index c87ac26..8fb3306 100644
--- a/Lib/regsub.py
+++ b/Lib/regsub.py
@@ -109,27 +109,32 @@ def capwords(str, pat='[^a-zA-Z0-9_]+'):
# Manage a cache of compiled regular expressions.
-# If the pattern is a string a compiled version of it is returned.
-# If the pattern has been used before we return an already compiled
+#
+# If the pattern is a string a compiled version of it is returned. If
+# the pattern has been used before we return an already compiled
# version from the cache; otherwise we compile it now and save the
-# compiled version in the cache.
-# Instead of a string, a compiled regular expression can also be
-# passed.
-# WARNING: if the pattern syntax is changed, the cache should be
-# flushed!
+# compiled version in the cache, along with the syntax it was compiled
+# with. Instead of a string, a compiled regular expression can also
+# be passed.
cache = {}
def compile(pat):
if type(pat) <> type(''):
return pat # Assume it is a compiled regex
- if cache.has_key(pat):
- prog = cache[pat] # Get it from the cache
+ key = (pat, regex.get_syntax())
+ if cache.has_key(key):
+ prog = cache[key] # Get it from the cache
else:
- prog = cache[pat] = regex.compile(pat)
+ prog = cache[key] = regex.compile(pat)
return prog
+def clear_cache():
+ global cache
+ cache = {}
+
+
# Expand \digit in the replacement.
# Each occurrence of \digit is replaced by the substring of str
# indicated by regs[digit]. To include a literal \ in the