Patchwork [2,of,9] util: move compilere to a class

login
register
mail settings
Submitter Siddharth Agarwal
Date July 15, 2014, 11:15 p.m.
Message ID <57ab201374d8a0c5b048.1405466124@dev1738.prn1.facebook.com>
Download mbox | patch
Permalink /patch/5175/
State Accepted
Commit cad9dadc9d26cbb63ea80c56cbd4a565c5154dac
Headers show

Comments

Siddharth Agarwal - July 15, 2014, 11:15 p.m.
# HG changeset patch
# User Siddharth Agarwal <sid0@fb.com>
# Date 1405460443 25200
#      Tue Jul 15 14:40:43 2014 -0700
# Node ID 57ab201374d8a0c5b0483260486d3e331247dfe9
# Parent  ed42268099cf2d5492034ed55003326708b76a98
util: move compilere to a class

We do this to allow us to use descriptors for other related methods.

For now, util.compilere does the same thing. Upcoming patches will remove it.

Patch

diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -716,29 +716,33 @@ 
 except ImportError:
     _re2 = False
 
-def compilere(pat, flags=0):
-    '''Compile a regular expression, using re2 if possible
+class _re(object):
+    def compile(self, pat, flags=0):
+        '''Compile a regular expression, using re2 if possible
 
-    For best performance, use only re2-compatible regexp features. The
-    only flags from the re module that are re2-compatible are
-    IGNORECASE and MULTILINE.'''
-    global _re2
-    if _re2 is None:
-        try:
-            # check if match works, see issue3964
-            _re2 = bool(re2.match(r'\[([^\[]+)\]', '[ui]'))
-        except ImportError:
-            _re2 = False
-    if _re2 and (flags & ~(remod.IGNORECASE | remod.MULTILINE)) == 0:
-        if flags & remod.IGNORECASE:
-            pat = '(?i)' + pat
-        if flags & remod.MULTILINE:
-            pat = '(?m)' + pat
-        try:
-            return re2.compile(pat)
-        except re2.error:
-            pass
-    return remod.compile(pat, flags)
+        For best performance, use only re2-compatible regexp features. The
+        only flags from the re module that are re2-compatible are
+        IGNORECASE and MULTILINE.'''
+        global _re2
+        if _re2 is None:
+            try:
+                # check if match works, see issue3964
+                _re2 = bool(re2.match(r'\[([^\[]+)\]', '[ui]'))
+            except ImportError:
+                _re2 = False
+        if _re2 and (flags & ~(remod.IGNORECASE | remod.MULTILINE)) == 0:
+            if flags & remod.IGNORECASE:
+                pat = '(?i)' + pat
+            if flags & remod.MULTILINE:
+                pat = '(?m)' + pat
+            try:
+                return re2.compile(pat)
+            except re2.error:
+                pass
+        return remod.compile(pat, flags)
+
+re = _re()
+compilere = re.compile
 
 _fspathcache = {}
 def fspath(name, root):