@@ -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
121120cache = {}
122121
123122def 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