# HG changeset patch # User Augie Fackler # Date 1530023783 14400 # Node ID 67dc32d4e790d6df8d3caeb4dfc77e48b68810e9 # Parent 96f65bdf0bf4b9d4caaf364d176696ae9a92aa63 cleanup: migrate from re.escape to stringutil.reescape This has consistent behavior on Python 2.7, 3.6, and 3.7 and has the benefit of probably being a little faster. Test output changes are largely because / used to be pointlessly escaped. Differential Revision: https://phab.mercurial-scm.org/D3842 diff -r 96f65bdf0bf4 -r 67dc32d4e790 hgext/keyword.py --- a/hgext/keyword.py Tue Jun 26 10:33:52 2018 -0400 +++ b/hgext/keyword.py Tue Jun 26 10:36:23 2018 -0400 @@ -245,7 +245,7 @@ @util.propertycache def escape(self): '''Returns bar-separated and escaped keywords.''' - return '|'.join(map(re.escape, self.templates.keys())) + return '|'.join(map(stringutil.reescape, self.templates.keys())) @util.propertycache def rekw(self): diff -r 96f65bdf0bf4 -r 67dc32d4e790 hgext/lfs/pointer.py --- a/hgext/lfs/pointer.py Tue Jun 26 10:33:52 2018 -0400 +++ b/hgext/lfs/pointer.py Tue Jun 26 10:36:23 2018 -0400 @@ -56,7 +56,7 @@ _requiredre = { 'size': re.compile(br'\A[0-9]+\Z'), 'oid': re.compile(br'\Asha256:[0-9a-f]{64}\Z'), - 'version': re.compile(br'\A%s\Z' % re.escape(VERSION)), + 'version': re.compile(br'\A%s\Z' % stringutil.reescape(VERSION)), } def validate(self): diff -r 96f65bdf0bf4 -r 67dc32d4e790 mercurial/hgweb/webutil.py --- a/mercurial/hgweb/webutil.py Tue Jun 26 10:33:52 2018 -0400 +++ b/mercurial/hgweb/webutil.py Tue Jun 26 10:36:23 2018 -0400 @@ -766,7 +766,7 @@ for key, pattern in websubdefs: # grab the delimiter from the character after the "s" unesc = pattern[1:2] - delim = re.escape(unesc) + delim = stringutil.reescape(unesc) # identify portions of the pattern, taking care to avoid escaped # delimiters. the replace format and flags are optional, but diff -r 96f65bdf0bf4 -r 67dc32d4e790 mercurial/match.py --- a/mercurial/match.py Tue Jun 26 10:33:52 2018 -0400 +++ b/mercurial/match.py Tue Jun 26 10:36:23 2018 -0400 @@ -714,7 +714,7 @@ >>> bprint(_globre(br'**/a')) (?:.*/)?a >>> bprint(_globre(br'a/**/b')) - a\/(?:.*/)?b + a/(?:.*/)?b >>> bprint(_globre(br'[a*?!^][^b][!c]')) [a*?!^][\^b][^c] >>> bprint(_globre(br'{a,b}')) @@ -725,7 +725,7 @@ i, n = 0, len(pat) res = '' group = 0 - escape = util.re.escape + escape = util.stringutil.reescape def peek(): return i < n and pat[i:i + 1] while i < n: @@ -790,13 +790,13 @@ if kind in ('path', 'relpath'): if pat == '.': return '' - return util.re.escape(pat) + '(?:/|$)' + return util.stringutil.reescape(pat) + '(?:/|$)' if kind == 'rootfilesin': if pat == '.': escaped = '' else: # Pattern is a directory name. - escaped = util.re.escape(pat) + '/' + escaped = util.stringutil.reescape(pat) + '/' # Anything after the pattern must be a non-directory. return escaped + '[^/]+$' if kind == 'relglob': diff -r 96f65bdf0bf4 -r 67dc32d4e790 mercurial/sshpeer.py --- a/mercurial/sshpeer.py Tue Jun 26 10:33:52 2018 -0400 +++ b/mercurial/sshpeer.py Tue Jun 26 10:36:23 2018 -0400 @@ -22,6 +22,7 @@ ) from .utils import ( procutil, + stringutil, ) def _serverquote(s): @@ -273,7 +274,7 @@ # Assume version 1 of wire protocol by default. protoname = wireprototypes.SSHV1 - reupgraded = re.compile(b'^upgraded %s (.*)$' % re.escape(token)) + reupgraded = re.compile(b'^upgraded %s (.*)$' % stringutil.reescape(token)) lines = ['', 'dummy'] max_noise = 500 diff -r 96f65bdf0bf4 -r 67dc32d4e790 mercurial/sslutil.py --- a/mercurial/sslutil.py Tue Jun 26 10:33:52 2018 -0400 +++ b/mercurial/sslutil.py Tue Jun 26 10:36:23 2018 -0400 @@ -618,14 +618,14 @@ # The client SHOULD NOT attempt to match a presented identifier # where the wildcard character is embedded within an A-label or # U-label of an internationalized domain name. - pats.append(re.escape(leftmost)) + pats.append(stringutil.reescape(leftmost)) else: # Otherwise, '*' matches any dotless string, e.g. www* - pats.append(re.escape(leftmost).replace(br'\*', '[^.]*')) + pats.append(stringutil.reescape(leftmost).replace(br'\*', '[^.]*')) # add the remaining fragments, ignore any wildcards for frag in remainder: - pats.append(re.escape(frag)) + pats.append(stringutil.reescape(frag)) pat = re.compile(br'\A' + br'\.'.join(pats) + br'\Z', re.IGNORECASE) return pat.match(hostname) is not None diff -r 96f65bdf0bf4 -r 67dc32d4e790 tests/test-walk.t --- a/tests/test-walk.t Tue Jun 26 10:33:52 2018 -0400 +++ b/tests/test-walk.t Tue Jun 26 10:36:23 2018 -0400 @@ -92,11 +92,11 @@ f mammals/skunk skunk $ hg debugwalk -v -I '*k' * matcher: - + f mammals/skunk skunk $ hg debugwalk -v -I 'glob:*k' * matcher: - + f mammals/skunk skunk $ hg debugwalk -v -I 'relglob:*k' * matcher: @@ -260,7 +260,7 @@ f mammals/skunk skunk $ hg debugwalk -v Procyonidae * matcher: - + f mammals/Procyonidae/cacomistle Procyonidae/cacomistle f mammals/Procyonidae/coatimundi Procyonidae/coatimundi f mammals/Procyonidae/raccoon Procyonidae/raccoon @@ -268,7 +268,7 @@ $ cd Procyonidae $ hg debugwalk -v . * matcher: - + f mammals/Procyonidae/cacomistle cacomistle f mammals/Procyonidae/coatimundi coatimundi f mammals/Procyonidae/raccoon raccoon @@ -316,7 +316,7 @@ f beans/turtle beans/turtle $ hg debugwalk -v -I '{*,{b,m}*/*}k' * matcher: - + f beans/black beans/black f fenugreek fenugreek f mammals/skunk mammals/skunk @@ -330,25 +330,25 @@ $ hg debugwalk -v -Inon-existent -Ibeans/black * matcher: - + f beans/black beans/black $ hg debugwalk -v -Ibeans beans/black * matcher: , + m1=, m2=> f beans/black beans/black exact $ hg debugwalk -v -Ibeans/black beans * matcher: , - m2=> + m2=> f beans/black beans/black $ hg debugwalk -v -Xbeans/black beans * matcher: , - m2=> + m2=> f beans/borlotti beans/borlotti f beans/kidney beans/kidney f beans/navy beans/navy @@ -358,7 +358,7 @@ * matcher: , - m2=> + m2=> f beans/borlotti beans/borlotti f beans/kidney beans/kidney f beans/navy beans/navy @@ -367,33 +367,33 @@ $ hg debugwalk -v -Xbeans/black beans/black * matcher: , - m2=> + m1=, + m2=> $ hg debugwalk -v -Xbeans/black -Ibeans/black * matcher: , - m2=> + m1=, + m2=> $ hg debugwalk -v -Xbeans beans/black * matcher: , + m1=, m2=> $ hg debugwalk -v -Xbeans -Ibeans/black * matcher: , + m1=, m2=> $ hg debugwalk -v 'glob:mammals/../beans/b*' * matcher: - + f beans/black beans/black f beans/borlotti beans/borlotti $ hg debugwalk -v '-X*/Procyonidae' mammals * matcher: , - m2=> + m2=> f mammals/skunk mammals/skunk $ hg debugwalk -v path:mammals * matcher: @@ -436,12 +436,12 @@ $ hg debugwalk -v beans/black -X 'path:beans' * matcher: , + m1=, m2=> $ hg debugwalk -v -I 'path:beans/black' -X 'path:beans' * matcher: , + m1=, m2=> Test absolute paths: @@ -485,11 +485,11 @@ glob: $ENOENT$ $ hg debugwalk -v glob:glob:glob * matcher: - + f glob:glob glob:glob exact $ hg debugwalk -v path:glob:glob * matcher: - + f glob:glob glob:glob exact $ rm glob:glob $ hg addremove @@ -511,11 +511,11 @@ $ hg debugwalk -v path:beans/black * matcher: - + f beans/black beans/black exact $ hg debugwalk -v path:beans//black * matcher: - + f beans/black beans/black exact $ hg debugwalk -v relglob:Procyonidae @@ -523,20 +523,20 @@ $ hg debugwalk -v 'relglob:Procyonidae/**' * matcher: - + f mammals/Procyonidae/cacomistle mammals/Procyonidae/cacomistle f mammals/Procyonidae/coatimundi mammals/Procyonidae/coatimundi f mammals/Procyonidae/raccoon mammals/Procyonidae/raccoon $ hg debugwalk -v 'relglob:Procyonidae/**' fennel * matcher: - + f fennel fennel exact f mammals/Procyonidae/cacomistle mammals/Procyonidae/cacomistle f mammals/Procyonidae/coatimundi mammals/Procyonidae/coatimundi f mammals/Procyonidae/raccoon mammals/Procyonidae/raccoon $ hg debugwalk -v beans 'glob:beans/*' * matcher: - + f beans/black beans/black f beans/borlotti beans/borlotti f beans/kidney beans/kidney @@ -598,7 +598,7 @@ $ hg debugwalk -v ignored/file * matcher: - + f ignored/file ignored/file exact Test listfile and listfile0 @@ -612,7 +612,7 @@ $ $PYTHON -c "open('listfile', 'wb').write(b'fenugreek\nnew\r\nmammals/skunk\n')" $ hg debugwalk -v -I 'listfile:listfile' * matcher: - + f fenugreek fenugreek f mammals/skunk mammals/skunk f new new @@ -620,17 +620,17 @@ $ cd .. $ hg debugwalk -v -R t t/mammals/skunk * matcher: - + f mammals/skunk t/mammals/skunk exact $ mkdir t2 $ cd t2 $ hg debugwalk -v -R ../t ../t/mammals/skunk * matcher: - + f mammals/skunk ../t/mammals/skunk exact $ hg debugwalk -v --cwd ../t mammals/skunk * matcher: - + f mammals/skunk mammals/skunk exact $ cd ..