re2: make errors quiet stable
authorPierre-Yves David <pierre-yves.david@octobus.net>
Thu, 02 May 2024 02:20:42 +0200
branchstable
changeset 51486 6c39edd1d348
parent 51485 fc317bd5b637
child 51487 436aee94f2d7
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.
mercurial/util.py
--- a/mercurial/util.py	Thu May 02 08:46:58 2024 +0200
+++ b/mercurial/util.py	Thu May 02 02:20:42 2024 +0200
@@ -2196,6 +2196,8 @@
 
 
 _re2_input = lambda x: x
+# google-re2 will need to be tell to not output error on its own
+_re2_options = None
 try:
     import re2  # pytype: disable=import-error
 
@@ -2216,6 +2218,7 @@
     def _checkre2():
         global _re2
         global _re2_input
+        global _re2_options
         if _re2 is not None:
             # we already have the answer
             return
@@ -2234,6 +2237,12 @@
             check_input = pycompat.sysstr(check_input)
             _re2 = bool(re2.match(check_pattern, check_input))
             _re2_input = pycompat.sysstr
+        try:
+            quiet = re2.Options()
+            quiet.log_errors = False
+            _re2_options = quiet
+        except AttributeError:
+            pass
 
     def compile(self, pat, flags=0):
         """Compile a regular expression, using re2 if possible
@@ -2249,7 +2258,12 @@
             if flags & remod.MULTILINE:
                 pat = b'(?m)' + pat
             try:
-                return re2.compile(_re2_input(pat))
+                input_regex = _re2_input(pat)
+                if _re2_options is not None:
+                    compiled = re2.compile(input_regex, options=_re2_options)
+                else:
+                    compiled = re2.compile(input_regex)
+                return compiled
             except re2.error:
                 pass
         return remod.compile(pat, flags)