match: let regex match function return a boolean
authorDenis Laxalde <denis@laxalde.org>
Sun, 07 Apr 2019 16:53:47 +0200
changeset 42087 2e2699af5649
parent 42086 1721b92f2b5e
child 42088 770e87999701
match: let regex match function return a boolean Match function for regex pattern kind is built through _buildregexmatch() and _buildmatch() using _rematcher() that returns a re.match function, which either returns a match object or None. This does not conform to Mercurial's matcher interface for __call__() or exact(), which are expected to return a boolean value. We fix this by building a lambda around _rematcher() in _buildregexmatch(). Accordingly, we update doctest examples to remove bool() calls that are now useless.
mercurial/match.py
--- a/mercurial/match.py	Sun Apr 07 17:16:58 2019 +0200
+++ b/mercurial/match.py	Sun Apr 07 16:53:47 2019 +0200
@@ -181,11 +181,11 @@
 
     1. Calling the matcher with a file name returns True if any pattern
     matches that file name:
-    >>> bool(m('a'))
+    >>> m('a')
     True
-    >>> bool(m('main.c'))
+    >>> m('main.c')
     True
-    >>> bool(m('test.py'))
+    >>> m('test.py')
     False
 
     2. Using the exact() method only returns True if the file name matches one
@@ -490,17 +490,17 @@
     ...     ('glob', '*.h', ''),
     ... ]
     >>> m = patternmatcher('foo', kindpats)
-    >>> bool(m('main.c'))  # matches re:.*\.c$
+    >>> m('main.c')  # matches re:.*\.c$
     True
-    >>> bool(m('b.txt'))
+    >>> m('b.txt')
     False
-    >>> bool(m('foo/a'))  # matches path:foo/a
+    >>> m('foo/a')  # matches path:foo/a
     True
-    >>> bool(m('a'))  # does not match path:b, since 'root' is 'foo'
+    >>> m('a')  # does not match path:b, since 'root' is 'foo'
     False
-    >>> bool(m('b'))  # matches relpath:b, since 'root' is 'foo'
+    >>> m('b')  # matches relpath:b, since 'root' is 'foo'
     True
-    >>> bool(m('lib.h'))  # matches glob:*.h
+    >>> m('lib.h')  # matches glob:*.h
     True
 
     >>> m.files()
@@ -871,13 +871,13 @@
     >>> from . import pycompat
     >>> m1 = match(b'root', b'', [b'a.txt', b'sub/b.txt'])
     >>> m2 = subdirmatcher(b'sub', m1)
-    >>> bool(m2(b'a.txt'))
+    >>> m2(b'a.txt')
     False
-    >>> bool(m2(b'b.txt'))
+    >>> m2(b'b.txt')
     True
-    >>> bool(m2.matchfn(b'a.txt'))
+    >>> m2.matchfn(b'a.txt')
     False
-    >>> bool(m2.matchfn(b'b.txt'))
+    >>> m2.matchfn(b'b.txt')
     True
     >>> m2.files()
     ['b.txt']
@@ -950,11 +950,11 @@
 
     >>> m1 = match(util.localpath(b'root/d/e'), b'f', [b'../a.txt', b'b.txt'])
     >>> m2 = prefixdirmatcher(b'd/e', m1)
-    >>> bool(m2(b'a.txt'),)
+    >>> m2(b'a.txt')
     False
-    >>> bool(m2(b'd/e/a.txt'))
+    >>> m2(b'd/e/a.txt')
     True
-    >>> bool(m2(b'd/e/b.txt'))
+    >>> m2(b'd/e/b.txt')
     False
     >>> m2.files()
     ['d/e/a.txt', 'd/e/f/b.txt']
@@ -1287,7 +1287,8 @@
             groupsize += piecesize + 1
 
         if startidx == 0:
-            func = _rematcher(fullregexp)
+            matcher = _rematcher(fullregexp)
+            func = lambda s: matcher(s) is not None
         else:
             group = regexps[startidx:]
             allgroups.append(_joinregexes(group))