Patchwork D3842: cleanup: migrate from re.escape to stringutil.reescape

login
register
mail settings
Submitter phabricator
Date June 26, 2018, 5:51 p.m.
Message ID <c2eb3cb259d8abb3606fe9345c024367@localhost.localdomain>
Download mbox | patch
Permalink /patch/32445/
State Not Applicable
Headers show

Comments

phabricator - June 26, 2018, 5:51 p.m.
This revision was automatically updated to reflect the committed changes.
Closed by commit rHG67dc32d4e790: cleanup: migrate from re.escape to stringutil.reescape (authored by durin42, committed by ).

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D3842?vs=9309&id=9313

REVISION DETAIL
  https://phab.mercurial-scm.org/D3842

AFFECTED 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

CHANGE DETAILS




To: durin42, #hg-reviewers, pulkit
Cc: mercurial-devel

Patch

diff --git a/tests/test-walk.t b/tests/test-walk.t
--- a/tests/test-walk.t
+++ b/tests/test-walk.t
@@ -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,15 +260,15 @@ 
   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
 
   $ 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,42 +358,42 @@ 
   * 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
   f  beans/pinto     beans/pinto
   f  beans/turtle    beans/turtle
   $ 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,32 +511,32 @@ 
 
   $ 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
   * matcher:
   <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,25 +612,25 @@ 
   $ $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
 
   $ 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 ..
diff --git a/mercurial/sslutil.py b/mercurial/sslutil.py
--- a/mercurial/sslutil.py
+++ b/mercurial/sslutil.py
@@ -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 --git a/mercurial/sshpeer.py b/mercurial/sshpeer.py
--- a/mercurial/sshpeer.py
+++ b/mercurial/sshpeer.py
@@ -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 --git a/mercurial/match.py b/mercurial/match.py
--- a/mercurial/match.py
+++ b/mercurial/match.py
@@ -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 --git a/mercurial/hgweb/webutil.py b/mercurial/hgweb/webutil.py
--- a/mercurial/hgweb/webutil.py
+++ b/mercurial/hgweb/webutil.py
@@ -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 --git a/hgext/lfs/pointer.py b/hgext/lfs/pointer.py
--- a/hgext/lfs/pointer.py
+++ b/hgext/lfs/pointer.py
@@ -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 --git a/hgext/keyword.py b/hgext/keyword.py
--- a/hgext/keyword.py
+++ b/hgext/keyword.py
@@ -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):