[PATCH 2 of 4 stable] py3: fix for Python 3.12 emitting SyntaxWarning on invalid escape sequences

Mads Kiilerich mads at kiilerich.com
Tue Mar 7 18:23:17 UTC 2023


# HG changeset patch
# User Mads Kiilerich <mads at kiilerich.com>
# Date 1678203954 -3600
#      Tue Mar 07 16:45:54 2023 +0100
# Branch stable
# Node ID 72ba5a6dbb52570fbdfa07ce15ac6ad88e35f63c
# Parent  1efa4e96f6461bf071b28d66b13bdb67fdc91fc6
py3: fix for Python 3.12 emitting SyntaxWarning on invalid escape sequences

Mercurial became very noisy after https://github.com/python/cpython/commit/a60ddd31be7ff96a8189e7483bf1eb2071d2bddf ,
for example:

  $ python3.12 mercurial/store.py
  mercurial/store.py:406: SyntaxWarning: invalid escape sequence '\.'
    EXCLUDED = re.compile(b'.*undo\.[^/]+\.(nd?|i)$')

This verbosity made some tests fail.

The problems were mostly insufficiently escaped regexps, relying on the Python
parser/scanner preserving invalid escape sequences.

diff --git a/mercurial/match.py b/mercurial/match.py
--- a/mercurial/match.py
+++ b/mercurial/match.py
@@ -196,14 +196,14 @@ def match(
     ...     return match(util.localpath(root), *args, **kwargs)
 
     Usually a patternmatcher is returned:
-    >>> _match(b'/foo', b'.', [b're:.*\.c$', b'path:foo/a', b'*.py'])
+    >>> _match(b'/foo', b'.', [br're:.*\.c$', b'path:foo/a', b'*.py'])
     <patternmatcher patterns='.*\\.c$|foo/a(?:/|$)|[^/]*\\.py$'>
 
     Combining 'patterns' with 'include' (resp. 'exclude') gives an
     intersectionmatcher (resp. a differencematcher):
-    >>> type(_match(b'/foo', b'.', [b're:.*\.c$'], include=[b'path:lib']))
+    >>> type(_match(b'/foo', b'.', [br're:.*\.c$'], include=[b'path:lib']))
     <class 'mercurial.match.intersectionmatcher'>
-    >>> type(_match(b'/foo', b'.', [b're:.*\.c$'], exclude=[b'path:build']))
+    >>> type(_match(b'/foo', b'.', [br're:.*\.c$'], exclude=[b'path:build']))
     <class 'mercurial.match.differencematcher'>
 
     Notice that, if 'patterns' is empty, an alwaysmatcher is returned:
@@ -212,7 +212,7 @@ def match(
 
     The 'default' argument determines which kind of pattern is assumed if a
     pattern has no prefix:
-    >>> _match(b'/foo', b'.', [b'.*\.c$'], default=b're')
+    >>> _match(b'/foo', b'.', [br'.*\.c$'], default=b're')
     <patternmatcher patterns='.*\\.c$'>
     >>> _match(b'/foo', b'.', [b'main.py'], default=b'relpath')
     <patternmatcher patterns='main\\.py(?:/|$)'>
@@ -223,7 +223,7 @@ def match(
     name) matches againset one of the patterns given at initialization. There
     are two ways of doing this check.
 
-    >>> m = _match(b'/foo', b'', [b're:.*\.c$', b'relpath:a'])
+    >>> m = _match(b'/foo', b'', [br're:.*\.c$', b'relpath:a'])
 
     1. Calling the matcher with a file name returns True if any pattern
     matches that file name:
diff --git a/tests/test-help.t b/tests/test-help.t
--- a/tests/test-help.t
+++ b/tests/test-help.t
@@ -1988,7 +1988,7 @@ such str.lower().
 
   $ "$PYTHON" <<EOF
   > def escape(s):
-  >     return b''.join(b'\\u%x' % ord(uc) for uc in s.decode('cp932'))
+  >     return b''.join(br'\\u%x' % ord(uc) for uc in s.decode('cp932'))
   > # translation of "record" in ja_JP.cp932
   > upper = b"\x8bL\x98^"
   > # str.lower()-ed section name should be treated as different one
diff --git a/tests/test-minirst.py b/tests/test-minirst.py
--- a/tests/test-minirst.py
+++ b/tests/test-minirst.py
@@ -154,7 +154,7 @@ marker after the option. It is treated a
 
 debugformats('options', options)
 
-fields = b"""
+fields = br"""
 :a: First item.
 :ab: Second item. Indentation and wrapping
      is handled automatically.
diff --git a/tests/testlib/persistent-nodemap-race-ext.py b/tests/testlib/persistent-nodemap-race-ext.py
--- a/tests/testlib/persistent-nodemap-race-ext.py
+++ b/tests/testlib/persistent-nodemap-race-ext.py
@@ -1,4 +1,4 @@
-"""Create the race condition for issue6554
+r"""Create the race condition for issue6554
 
 The persistent nodemap issues had an issue where a second writer could
 overwrite the data that a previous write just wrote. The would break the append



More information about the Mercurial-devel mailing list