changeset 38479:67dc32d4e790

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
author Augie Fackler <augie@google.com>
date Tue, 26 Jun 2018 10:36:23 -0400
parents 96f65bdf0bf4
children b4cfd803b3f2
files hgext/keyword.py hgext/lfs/pointer.py mercurial/hgweb/webutil.py mercurial/match.py mercurial/sshpeer.py mercurial/sslutil.py tests/test-walk.t
diffstat 7 files changed, 44 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- 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):
--- 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):
--- 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
--- 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':
--- 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
--- 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
--- 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:
-  <includematcher includes='(?:mammals\\/[^/]*k(?:/|$))'>
+  <includematcher includes='(?:mammals/[^/]*k(?:/|$))'>
   f  mammals/skunk  skunk
   $ hg debugwalk -v -I 'glob:*k'
   * matcher:
-  <includematcher includes='(?:mammals\\/[^/]*k(?:/|$))'>
+  <includematcher includes='(?:mammals/[^/]*k(?:/|$))'>
   f  mammals/skunk  skunk
   $ hg debugwalk -v -I 'relglob:*k'
   * matcher:
@@ -260,7 +260,7 @@
   f  mammals/skunk                   skunk
   $ hg debugwalk -v Procyonidae
   * matcher:
-  <patternmatcher patterns='(?:mammals\\/Procyonidae(?:/|$))'>
+  <patternmatcher patterns='(?:mammals/Procyonidae(?:/|$))'>
   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:
-  <patternmatcher patterns='(?:mammals\\/Procyonidae(?:/|$))'>
+  <patternmatcher patterns='(?:mammals/Procyonidae(?:/|$))'>
   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:
-  <includematcher includes='(?:(?:[^/]*|(?:b|m)[^/]*\\/[^/]*)k(?:/|$))'>
+  <includematcher includes='(?:(?:[^/]*|(?:b|m)[^/]*/[^/]*)k(?:/|$))'>
   f  beans/black    beans/black
   f  fenugreek      fenugreek
   f  mammals/skunk  mammals/skunk
@@ -330,25 +330,25 @@
   <includematcher includes='(?:non\\-existent(?:/|$))'>
   $ hg debugwalk -v -Inon-existent -Ibeans/black
   * matcher:
-  <includematcher includes='(?:non\\-existent(?:/|$)|beans\\/black(?:/|$))'>
+  <includematcher includes='(?:non\\-existent(?:/|$)|beans/black(?:/|$))'>
   f  beans/black  beans/black
   $ hg debugwalk -v -Ibeans beans/black
   * matcher:
   <intersectionmatcher
-    m1=<patternmatcher patterns='(?:beans\\/black(?:/|$))'>,
+    m1=<patternmatcher patterns='(?:beans/black(?:/|$))'>,
     m2=<includematcher includes='(?:beans(?:/|$))'>>
   f  beans/black  beans/black  exact
   $ hg debugwalk -v -Ibeans/black beans
   * matcher:
   <intersectionmatcher
     m1=<patternmatcher patterns='(?:beans(?:/|$))'>,
-    m2=<includematcher includes='(?:beans\\/black(?:/|$))'>>
+    m2=<includematcher includes='(?:beans/black(?:/|$))'>>
   f  beans/black  beans/black
   $ hg debugwalk -v -Xbeans/black beans
   * matcher:
   <differencematcher
     m1=<patternmatcher patterns='(?:beans(?:/|$))'>,
-    m2=<includematcher includes='(?:beans\\/black(?:/|$))'>>
+    m2=<includematcher includes='(?:beans/black(?:/|$))'>>
   f  beans/borlotti  beans/borlotti
   f  beans/kidney    beans/kidney
   f  beans/navy      beans/navy
@@ -358,7 +358,7 @@
   * matcher:
   <differencematcher
     m1=<includematcher includes='(?:beans(?:/|$))'>,
-    m2=<includematcher includes='(?:beans\\/black(?:/|$))'>>
+    m2=<includematcher includes='(?:beans/black(?:/|$))'>>
   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:
   <differencematcher
-    m1=<patternmatcher patterns='(?:beans\\/black(?:/|$))'>,
-    m2=<includematcher includes='(?:beans\\/black(?:/|$))'>>
+    m1=<patternmatcher patterns='(?:beans/black(?:/|$))'>,
+    m2=<includematcher includes='(?:beans/black(?:/|$))'>>
   $ hg debugwalk -v -Xbeans/black -Ibeans/black
   * matcher:
   <differencematcher
-    m1=<includematcher includes='(?:beans\\/black(?:/|$))'>,
-    m2=<includematcher includes='(?:beans\\/black(?:/|$))'>>
+    m1=<includematcher includes='(?:beans/black(?:/|$))'>,
+    m2=<includematcher includes='(?:beans/black(?:/|$))'>>
   $ hg debugwalk -v -Xbeans beans/black
   * matcher:
   <differencematcher
-    m1=<patternmatcher patterns='(?:beans\\/black(?:/|$))'>,
+    m1=<patternmatcher patterns='(?:beans/black(?:/|$))'>,
     m2=<includematcher includes='(?:beans(?:/|$))'>>
   $ hg debugwalk -v -Xbeans -Ibeans/black
   * matcher:
   <differencematcher
-    m1=<includematcher includes='(?:beans\\/black(?:/|$))'>,
+    m1=<includematcher includes='(?:beans/black(?:/|$))'>,
     m2=<includematcher includes='(?:beans(?:/|$))'>>
   $ hg debugwalk -v 'glob:mammals/../beans/b*'
   * matcher:
-  <patternmatcher patterns='(?:beans\\/b[^/]*$)'>
+  <patternmatcher patterns='(?:beans/b[^/]*$)'>
   f  beans/black     beans/black
   f  beans/borlotti  beans/borlotti
   $ hg debugwalk -v '-X*/Procyonidae' mammals
   * matcher:
   <differencematcher
     m1=<patternmatcher patterns='(?:mammals(?:/|$))'>,
-    m2=<includematcher includes='(?:[^/]*\\/Procyonidae(?:/|$))'>>
+    m2=<includematcher includes='(?:[^/]*/Procyonidae(?:/|$))'>>
   f  mammals/skunk  mammals/skunk
   $ hg debugwalk -v path:mammals
   * matcher:
@@ -436,12 +436,12 @@
   $ hg debugwalk -v beans/black -X 'path:beans'
   * matcher:
   <differencematcher
-    m1=<patternmatcher patterns='(?:beans\\/black(?:/|$))'>,
+    m1=<patternmatcher patterns='(?:beans/black(?:/|$))'>,
     m2=<includematcher includes='(?:beans(?:/|$))'>>
   $ hg debugwalk -v -I 'path:beans/black' -X 'path:beans'
   * matcher:
   <differencematcher
-    m1=<includematcher includes='(?:beans\\/black(?:/|$))'>,
+    m1=<includematcher includes='(?:beans/black(?:/|$))'>,
     m2=<includematcher includes='(?:beans(?:/|$))'>>
 
 Test absolute paths:
@@ -485,11 +485,11 @@
   glob: $ENOENT$
   $ hg debugwalk -v glob:glob:glob
   * matcher:
-  <patternmatcher patterns='(?:glob\\:glob$)'>
+  <patternmatcher patterns='(?:glob:glob$)'>
   f  glob:glob  glob:glob  exact
   $ hg debugwalk -v path:glob:glob
   * matcher:
-  <patternmatcher patterns='(?:glob\\:glob(?:/|$))'>
+  <patternmatcher patterns='(?:glob:glob(?:/|$))'>
   f  glob:glob  glob:glob  exact
   $ rm glob:glob
   $ hg addremove
@@ -511,11 +511,11 @@
 
   $ hg debugwalk -v path:beans/black
   * matcher:
-  <patternmatcher patterns='(?:beans\\/black(?:/|$))'>
+  <patternmatcher patterns='(?:beans/black(?:/|$))'>
   f  beans/black  beans/black  exact
   $ hg debugwalk -v path:beans//black
   * matcher:
-  <patternmatcher patterns='(?:beans\\/black(?:/|$))'>
+  <patternmatcher patterns='(?:beans/black(?:/|$))'>
   f  beans/black  beans/black  exact
 
   $ hg debugwalk -v relglob:Procyonidae
@@ -523,20 +523,20 @@
   <patternmatcher patterns='(?:(?:|.*/)Procyonidae$)'>
   $ hg debugwalk -v 'relglob:Procyonidae/**'
   * matcher:
-  <patternmatcher patterns='(?:(?:|.*/)Procyonidae\\/.*$)'>
+  <patternmatcher patterns='(?:(?:|.*/)Procyonidae/.*$)'>
   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:
-  <patternmatcher patterns='(?:(?:|.*/)Procyonidae\\/.*$|fennel(?:/|$))'>
+  <patternmatcher patterns='(?:(?:|.*/)Procyonidae/.*$|fennel(?:/|$))'>
   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:
-  <patternmatcher patterns='(?:beans(?:/|$)|beans\\/[^/]*$)'>
+  <patternmatcher patterns='(?:beans(?:/|$)|beans/[^/]*$)'>
   f  beans/black     beans/black
   f  beans/borlotti  beans/borlotti
   f  beans/kidney    beans/kidney
@@ -598,7 +598,7 @@
   <patternmatcher patterns='(?:ignored(?:/|$))'>
   $ hg debugwalk -v ignored/file
   * matcher:
-  <patternmatcher patterns='(?:ignored\\/file(?:/|$))'>
+  <patternmatcher patterns='(?:ignored/file(?:/|$))'>
   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:
-  <includematcher includes='(?:fenugreek(?:/|$)|new(?:/|$)|mammals\\/skunk(?:/|$))'>
+  <includematcher includes='(?:fenugreek(?:/|$)|new(?:/|$)|mammals/skunk(?:/|$))'>
   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:
-  <patternmatcher patterns='(?:mammals\\/skunk(?:/|$))'>
+  <patternmatcher patterns='(?:mammals/skunk(?:/|$))'>
   f  mammals/skunk  t/mammals/skunk  exact
   $ mkdir t2
   $ cd t2
   $ hg debugwalk -v -R ../t ../t/mammals/skunk
   * matcher:
-  <patternmatcher patterns='(?:mammals\\/skunk(?:/|$))'>
+  <patternmatcher patterns='(?:mammals/skunk(?:/|$))'>
   f  mammals/skunk  ../t/mammals/skunk  exact
   $ hg debugwalk -v --cwd ../t mammals/skunk
   * matcher:
-  <patternmatcher patterns='(?:mammals\\/skunk(?:/|$))'>
+  <patternmatcher patterns='(?:mammals/skunk(?:/|$))'>
   f  mammals/skunk  mammals/skunk  exact
 
   $ cd ..