Patchwork [4,of,5] check-code: introduce function for using re2 when available

login
register
mail settings
Submitter Simon Heimberg
Date June 8, 2013, 6:57 p.m.
Message ID <e12f745ed2b4c38ac0ac.1370717822@lapsi.heimberg.home>
Download mbox | patch
Permalink /patch/1712/
State Accepted
Commit 30ea54660d14ec8dd20e3fcc52b1f79fe2a1d0fa
Headers show

Comments

Simon Heimberg - June 8, 2013, 6:57 p.m.
# HG changeset patch
# User Simon Heimberg <simohe@besonet.ch>
# Date 1370715614 -7200
# Node ID e12f745ed2b4c38ac0acb2c8f7c2cebe3b130f3f
# Parent  9ea9c7411f1540ee7d6960087774388d28438b39
check-code: introduce function for using re2 when available

Do it similar as in mercurial.util. For simplicity only support flag
multiline which is the only one used.

Patch

diff -r 9ea9c7411f15 -r e12f745ed2b4 contrib/check-code.py
--- a/contrib/check-code.py	Sam Jun 08 20:20:14 2013 +0200
+++ b/contrib/check-code.py	Sam Jun 08 20:20:14 2013 +0200
@@ -10,6 +10,20 @@ 
 import re, glob, os, sys
 import keyword
 import optparse
+try:
+    import re2
+except ImportError:
+    re2 = None
+
+def compilere(pat, multiline=False):
+    if multiline:
+        pat = '(?m)' + pat
+    if re2:
+        try:
+            return re2.compile(pat)
+        except re2.error:
+            pass
+    return re.compile(pat)
 
 def repquote(m):
     t = re.sub(r"\w", "x", m.group('text'))