Patchwork [8,of,8] match: let regex match function return a boolean

login
register
mail settings
Submitter Denis Laxalde
Date April 8, 2019, 8:23 a.m.
Message ID <de054cf2bda331787804.1554711832@steppe.logilab.fr>
Download mbox | patch
Permalink /patch/39527/
State Accepted
Headers show

Comments

Denis Laxalde - April 8, 2019, 8:23 a.m.
# HG changeset patch
# User Denis Laxalde <denis@laxalde.org>
# Date 1554648827 -7200
#      Sun Apr 07 16:53:47 2019 +0200
# Node ID de054cf2bda331787804762f411d1656333c43c4
# Parent  5adda8921403ce9aa216a823e69ab668ac674d8f
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.
Augie Fackler - April 8, 2019, 6:16 p.m.
On Mon, Apr 08, 2019 at 10:23:52AM +0200, Denis Laxalde wrote:
> # HG changeset patch
> # User Denis Laxalde <denis@laxalde.org>
> # Date 1554648827 -7200
> #      Sun Apr 07 16:53:47 2019 +0200
> # Node ID de054cf2bda331787804762f411d1656333c43c4
> # Parent  5adda8921403ce9aa216a823e69ab668ac674d8f
> match: let regex match function return a boolean

queued, thanks
via Mercurial-devel - April 11, 2019, 4:31 p.m.
Denis, I think this needs to be bool(x) instead of "x is not None". With
re2, we use compile(exp).test_match, which returns a boolean already, so I
think it will always be true with the current code.

On Mon, Apr 8, 2019 at 11:21 AM Augie Fackler <raf@durin42.com> wrote:

> On Mon, Apr 08, 2019 at 10:23:52AM +0200, Denis Laxalde wrote:
> > # HG changeset patch
> > # User Denis Laxalde <denis@laxalde.org>
> > # Date 1554648827 -7200
> > #      Sun Apr 07 16:53:47 2019 +0200
> > # Node ID de054cf2bda331787804762f411d1656333c43c4
> > # Parent  5adda8921403ce9aa216a823e69ab668ac674d8f
> > match: let regex match function return a boolean
>
> queued, thanks
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel@mercurial-scm.org
> https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel
>

Patch

diff --git a/mercurial/match.py b/mercurial/match.py
--- a/mercurial/match.py
+++ b/mercurial/match.py
@@ -181,11 +181,11 @@  def match(root, cwd, patterns=None, incl
 
     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 @@  class patternmatcher(basematcher):
     ...     ('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 @@  class subdirmatcher(basematcher):
     >>> 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 @@  class prefixdirmatcher(basematcher):
 
     >>> 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 @@  def _buildregexmatch(kindpats, globsuffi
             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))