global: use raw strings for regular expressions with escapes
authorGregory Szorc <gregory.szorc@gmail.com>
Mon, 04 Feb 2019 08:54:30 -0800
changeset 41532 bd3f03d8cc9f
parent 41531 14983ac4a764
child 41533 0f64091cc851
global: use raw strings for regular expressions with escapes Escape sequences like \w, \s, and \d are technically invalid in str/bytes. This became a deprecation warning in Python 3.6 (https://bugs.python.org/issue27364). Python 3.8 bumps it to a SyntaxWarning (https://bugs.python.org/issue32912), which is non-silent by default. This commit changes a number of regular expressions to use br'' so regular expression special sequences don't need \\ literals. This fixes roughly half of the SyntaxWarning we see in the code base with Python 3.8. Differential Revision: https://phab.mercurial-scm.org/D5815
hgext/blackbox.py
hgext/commitextras.py
hgext/convert/cvsps.py
hgext/mq.py
hgext/phabricator.py
hgext/releasenotes.py
mercurial/color.py
mercurial/patch.py
--- a/hgext/blackbox.py	Mon Feb 04 09:29:25 2019 -0800
+++ b/hgext/blackbox.py	Mon Feb 04 08:54:30 2019 -0800
@@ -190,7 +190,7 @@
             break
 
         # count the commands by matching lines like: 2013/01/23 19:13:36 root>
-        if re.match('^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} .*> .*', line):
+        if re.match(br'^\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2} .*> .*', line):
             count += 1
         output.append(line)
 
--- a/hgext/commitextras.py	Mon Feb 04 09:29:25 2019 -0800
+++ b/hgext/commitextras.py	Mon Feb 04 08:54:30 2019 -0800
@@ -58,7 +58,7 @@
                 if not k:
                     msg = _("unable to parse '%s', keys can't be empty")
                     raise error.Abort(msg % raw)
-                if re.search('[^\w-]', k):
+                if re.search(br'[^\w-]', k):
                     msg = _("keys can only contain ascii letters, digits,"
                             " '_' and '-'")
                     raise error.Abort(msg)
--- a/hgext/convert/cvsps.py	Mon Feb 04 09:29:25 2019 -0800
+++ b/hgext/convert/cvsps.py	Mon Feb 04 08:54:30 2019 -0800
@@ -122,7 +122,7 @@
     re_31 = re.compile(b'----------------------------$')
     re_32 = re.compile(b'======================================='
                        b'======================================$')
-    re_50 = re.compile(b'revision ([\\d.]+)(\s+locked by:\s+.+;)?$')
+    re_50 = re.compile(br'revision ([\d.]+)(\s+locked by:\s+.+;)?$')
     re_60 = re.compile(br'date:\s+(.+);\s+author:\s+(.+);\s+state:\s+(.+?);'
                        br'(\s+lines:\s+(\+\d+)?\s+(-\d+)?;)?'
                        br'(\s+commitid:\s+([^;]+);)?'
--- a/hgext/mq.py	Mon Feb 04 09:29:25 2019 -0800
+++ b/hgext/mq.py	Mon Feb 04 08:54:30 2019 -0800
@@ -1181,7 +1181,7 @@
     def makepatchname(self, title, fallbackname):
         """Return a suitable filename for title, adding a suffix to make
         it unique in the existing list"""
-        namebase = re.sub('[\s\W_]+', '_', title.lower()).strip('_')
+        namebase = re.sub(br'[\s\W_]+', b'_', title.lower()).strip(b'_')
         namebase = namebase[:75] # avoid too long name (issue5117)
         if namebase:
             try:
--- a/hgext/phabricator.py	Mon Feb 04 09:29:25 2019 -0800
+++ b/hgext/phabricator.py	Mon Feb 04 08:54:30 2019 -0800
@@ -255,9 +255,9 @@
     repo.ui.setconfig(b'phabricator', b'repophid', repophid)
     return repophid
 
-_differentialrevisiontagre = re.compile(b'\AD([1-9][0-9]*)\Z')
+_differentialrevisiontagre = re.compile(br'\AD([1-9][0-9]*)\Z')
 _differentialrevisiondescre = re.compile(
-    b'^Differential Revision:\s*(?P<url>(?:.*)D(?P<id>[1-9][0-9]*))$', re.M)
+    br'^Differential Revision:\s*(?P<url>(?:.*)D(?P<id>[1-9][0-9]*))$', re.M)
 
 def getoldnodedrevmap(repo, nodelist):
     """find previous nodes that has been sent to Phabricator
--- a/hgext/releasenotes.py	Mon Feb 04 09:29:25 2019 -0800
+++ b/hgext/releasenotes.py	Mon Feb 04 08:54:30 2019 -0800
@@ -55,7 +55,7 @@
     ('api', _('API Changes')),
 ]
 
-RE_DIRECTIVE = re.compile('^\.\. ([a-zA-Z0-9_]+)::\s*([^$]+)?$')
+RE_DIRECTIVE = re.compile(br'^\.\. ([a-zA-Z0-9_]+)::\s*([^$]+)?$')
 RE_ISSUE = br'\bissue ?[0-9]{4,6}(?![0-9])\b'
 
 BULLET_SECTION = _('Other Changes')
--- a/mercurial/color.py	Mon Feb 04 09:29:25 2019 -0800
+++ b/mercurial/color.py	Mon Feb 04 08:54:30 2019 -0800
@@ -484,7 +484,7 @@
             w32effects = None
         else:
             origattr = csbi.wAttributes
-            ansire = re.compile(b'\033\[([^m]*)m([^\033]*)(.*)',
+            ansire = re.compile(br'\033\[([^m]*)m([^\033]*)(.*)',
                                 re.MULTILINE | re.DOTALL)
 
     def win32print(ui, writefunc, text, **opts):
--- a/mercurial/patch.py	Mon Feb 04 09:29:25 2019 -0800
+++ b/mercurial/patch.py	Mon Feb 04 08:54:30 2019 -0800
@@ -638,8 +638,8 @@
         return self.changed | self.removed
 
 # @@ -start,len +start,len @@ or @@ -start +start @@ if len is 1
-unidesc = re.compile('@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@')
-contextdesc = re.compile('(?:---|\*\*\*) (\d+)(?:,(\d+))? (?:---|\*\*\*)')
+unidesc = re.compile(br'@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@')
+contextdesc = re.compile(br'(?:---|\*\*\*) (\d+)(?:,(\d+))? (?:---|\*\*\*)')
 eolmodes = ['strict', 'crlf', 'lf', 'auto']
 
 class patchfile(object):
@@ -2762,7 +2762,7 @@
     return maxfile, maxtotal, addtotal, removetotal, binary
 
 def diffstatdata(lines):
-    diffre = re.compile('^diff .*-r [a-z0-9]+\s(.*)$')
+    diffre = re.compile(br'^diff .*-r [a-z0-9]+\s(.*)$')
 
     results = []
     filename, adds, removes, isbinary = None, 0, 0, False