comparison mercurial/util.py @ 51607:6c39edd1d348 stable

re2: make errors quiet By default, the re2 library will output error on its own instead of keeping the error in an exception. This make re2 printing spurious error before fallback to the stdlib remodule that may accept the pattern or also fails to parse it and raise a proper error that will be handled by Mercurial. So we also pass an Option object that changes this default.
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Thu, 02 May 2024 02:20:42 +0200
parents 187c5769a629
children 59f846fbc11d
comparison
equal deleted inserted replaced
51606:fc317bd5b637 51607:6c39edd1d348
2194 except OSError: 2194 except OSError:
2195 return True 2195 return True
2196 2196
2197 2197
2198 _re2_input = lambda x: x 2198 _re2_input = lambda x: x
2199 # google-re2 will need to be tell to not output error on its own
2200 _re2_options = None
2199 try: 2201 try:
2200 import re2 # pytype: disable=import-error 2202 import re2 # pytype: disable=import-error
2201 2203
2202 _re2 = None 2204 _re2 = None
2203 except ImportError: 2205 except ImportError:
2214 class _re: 2216 class _re:
2215 @staticmethod 2217 @staticmethod
2216 def _checkre2(): 2218 def _checkre2():
2217 global _re2 2219 global _re2
2218 global _re2_input 2220 global _re2_input
2221 global _re2_options
2219 if _re2 is not None: 2222 if _re2 is not None:
2220 # we already have the answer 2223 # we already have the answer
2221 return 2224 return
2222 2225
2223 check_pattern = br'\[([^\[]+)\]' 2226 check_pattern = br'\[([^\[]+)\]'
2232 # the `fb-re2` project provides a re2 module that acccept sysstr 2235 # the `fb-re2` project provides a re2 module that acccept sysstr
2233 check_pattern = pycompat.sysstr(check_pattern) 2236 check_pattern = pycompat.sysstr(check_pattern)
2234 check_input = pycompat.sysstr(check_input) 2237 check_input = pycompat.sysstr(check_input)
2235 _re2 = bool(re2.match(check_pattern, check_input)) 2238 _re2 = bool(re2.match(check_pattern, check_input))
2236 _re2_input = pycompat.sysstr 2239 _re2_input = pycompat.sysstr
2240 try:
2241 quiet = re2.Options()
2242 quiet.log_errors = False
2243 _re2_options = quiet
2244 except AttributeError:
2245 pass
2237 2246
2238 def compile(self, pat, flags=0): 2247 def compile(self, pat, flags=0):
2239 """Compile a regular expression, using re2 if possible 2248 """Compile a regular expression, using re2 if possible
2240 2249
2241 For best performance, use only re2-compatible regexp features. The 2250 For best performance, use only re2-compatible regexp features. The
2247 if flags & remod.IGNORECASE: 2256 if flags & remod.IGNORECASE:
2248 pat = b'(?i)' + pat 2257 pat = b'(?i)' + pat
2249 if flags & remod.MULTILINE: 2258 if flags & remod.MULTILINE:
2250 pat = b'(?m)' + pat 2259 pat = b'(?m)' + pat
2251 try: 2260 try:
2252 return re2.compile(_re2_input(pat)) 2261 input_regex = _re2_input(pat)
2262 if _re2_options is not None:
2263 compiled = re2.compile(input_regex, options=_re2_options)
2264 else:
2265 compiled = re2.compile(input_regex)
2266 return compiled
2253 except re2.error: 2267 except re2.error:
2254 pass 2268 pass
2255 return remod.compile(pat, flags) 2269 return remod.compile(pat, flags)
2256 2270
2257 @propertycache 2271 @propertycache