Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b67a25c

Browse files
committed
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.
1 parent 909d7c3 commit b67a25c

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

Lib/regsub.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,27 +109,32 @@ def capwords(str, pat='[^a-zA-Z0-9_]+'):
109109

110110

111111
# Manage a cache of compiled regular expressions.
112-
# If the pattern is a string a compiled version of it is returned.
113-
# If the pattern has been used before we return an already compiled
112+
#
113+
# If the pattern is a string a compiled version of it is returned. If
114+
# the pattern has been used before we return an already compiled
114115
# version from the cache; otherwise we compile it now and save the
115-
# compiled version in the cache.
116-
# Instead of a string, a compiled regular expression can also be
117-
# passed.
118-
# WARNING: if the pattern syntax is changed, the cache should be
119-
# flushed!
116+
# compiled version in the cache, along with the syntax it was compiled
117+
# with. Instead of a string, a compiled regular expression can also
118+
# be passed.
120119

121120
cache = {}
122121

123122
def compile(pat):
124123
if type(pat) <> type(''):
125124
return pat # Assume it is a compiled regex
126-
if cache.has_key(pat):
127-
prog = cache[pat] # Get it from the cache
125+
key = (pat, regex.get_syntax())
126+
if cache.has_key(key):
127+
prog = cache[key] # Get it from the cache
128128
else:
129-
prog = cache[pat] = regex.compile(pat)
129+
prog = cache[key] = regex.compile(pat)
130130
return prog
131131

132132

133+
def clear_cache():
134+
global cache
135+
cache = {}
136+
137+
133138
# Expand \digit in the replacement.
134139
# Each occurrence of \digit is replaced by the substring of str
135140
# indicated by regs[digit]. To include a literal \ in the

0 commit comments

Comments
 (0)