diff mbox series

[2/2] build/mkheader: Fix Syntax/DeprecationWarnings

Message ID 20240703225525.1759907-3-andrew.cooper3@citrix.com (mailing list archive)
State New
Headers show
Series build/mkheader: Fixes | expand

Commit Message

Andrew Cooper July 3, 2024, 10:55 p.m. UTC
With Python 3.11, the following is emitted during a build:

  tools/include/xen-foreign/mkheader.py:162: DeprecationWarning: invalid escape sequence '\s'
    regex = "#define\s+%s\\b" % define;
  tools/include/xen-foreign/mkheader.py:177: DeprecationWarning: invalid escape sequence '\*'
    input = re.compile("/\*(.*?)\*/", re.S).sub("", input)
  tools/include/xen-foreign/mkheader.py:178: DeprecationWarning: invalid escape sequence '\s'
    input = re.compile("\n\s*\n", re.S).sub("\n", input);
  tools/include/xen-foreign/mkheader.py:182: DeprecationWarning: invalid escape sequence '\s'
    regex = "union\s+%s\s*\{(.*?)\n\};" % union;
  tools/include/xen-foreign/mkheader.py:192: DeprecationWarning: invalid escape sequence '\s'
    regex = "(?:#ifdef ([A-Z_]+))?\nstruct\s+%s\s*\{(.*?)\n\};" % struct;
  tools/include/xen-foreign/mkheader.py:218: DeprecationWarning: invalid escape sequence '\s'
    output = re.sub("\\b(union\s+%s)\\b" % union, "\\1_%s" % arch, output);
  tools/include/xen-foreign/mkheader.py:222: DeprecationWarning: invalid escape sequence '\s'
    output = re.sub("\\b(struct\s+%s)\\b" % struct, "\\1_%s" % arch, output);

Python regexes should use raw strings.  Convert all regexes, and drop escaped
backslashes.  Note that regular escape sequences are interpreted normally when
parsing a regex, so \n even in a raw-string regex is a newline.

No functional change.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
---
CC: Anthony PERARD <anthony.perard@vates.tech>
CC: Jan Beulich <JBeulich@suse.com>
CC: Stefano Stabellini <sstabellini@kernel.org>
CC: Julien Grall <julien@xen.org>
CC: Oleksii Kurochko <oleksii.kurochko@gmail.com>
---
 tools/include/xen-foreign/mkheader.py | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

Comments

Anthony PERARD July 4, 2024, 8:53 a.m. UTC | #1
On Wed, Jul 03, 2024 at 11:55:25PM +0100, Andrew Cooper wrote:
> With Python 3.11, the following is emitted during a build:
> 
>   tools/include/xen-foreign/mkheader.py:162: DeprecationWarning: invalid escape sequence '\s'
>     regex = "#define\s+%s\\b" % define;
>   tools/include/xen-foreign/mkheader.py:177: DeprecationWarning: invalid escape sequence '\*'
>     input = re.compile("/\*(.*?)\*/", re.S).sub("", input)
>   tools/include/xen-foreign/mkheader.py:178: DeprecationWarning: invalid escape sequence '\s'
>     input = re.compile("\n\s*\n", re.S).sub("\n", input);
>   tools/include/xen-foreign/mkheader.py:182: DeprecationWarning: invalid escape sequence '\s'
>     regex = "union\s+%s\s*\{(.*?)\n\};" % union;
>   tools/include/xen-foreign/mkheader.py:192: DeprecationWarning: invalid escape sequence '\s'
>     regex = "(?:#ifdef ([A-Z_]+))?\nstruct\s+%s\s*\{(.*?)\n\};" % struct;
>   tools/include/xen-foreign/mkheader.py:218: DeprecationWarning: invalid escape sequence '\s'
>     output = re.sub("\\b(union\s+%s)\\b" % union, "\\1_%s" % arch, output);
>   tools/include/xen-foreign/mkheader.py:222: DeprecationWarning: invalid escape sequence '\s'
>     output = re.sub("\\b(struct\s+%s)\\b" % struct, "\\1_%s" % arch, output);
> 
> Python regexes should use raw strings.  Convert all regexes, and drop escaped
> backslashes.  Note that regular escape sequences are interpreted normally when
> parsing a regex, so \n even in a raw-string regex is a newline.
> 
> No functional change.
> 
> Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>

Reviewed-by: Anthony PERARD <anthony.perard@vates.tech>

Thanks,
diff mbox series

Patch

diff --git a/tools/include/xen-foreign/mkheader.py b/tools/include/xen-foreign/mkheader.py
index 3a33df4984a2..128b0f3014c2 100644
--- a/tools/include/xen-foreign/mkheader.py
+++ b/tools/include/xen-foreign/mkheader.py
@@ -159,7 +159,7 @@  defined = {}
 # add defines to output
 for line in re.findall("#define[^\n]+", input):
     for define in defines:
-        regex = "#define\s+%s\\b" % define
+        regex = r"#define\s+%s\b" % define
         match = re.search(regex, line)
         if None == match:
             continue
@@ -168,18 +168,18 @@  for line in re.findall("#define[^\n]+", input):
             replace = define + "_" + arch.upper()
         else:
             replace = define + "_" + arch
-        regex = "\\b%s\\b" % define
+        regex = r"\b%s\b" % define
         output += re.sub(regex, replace, line) + "\n"
 output += "\n"
 
 # delete defines, comments, empty lines
 input = re.sub("#define[^\n]+\n", "", input)
-input = re.compile("/\*(.*?)\*/", re.S).sub("", input)
-input = re.compile("\n\s*\n", re.S).sub("\n", input)
+input = re.compile(r"/\*(.*?)\*/", re.S).sub("", input)
+input = re.compile(r"\n\s*\n", re.S).sub("\n", input)
 
 # add unions to output
 for union in unions:
-    regex = "union\s+%s\s*\{(.*?)\n\};" % union
+    regex = r"union\s+%s\s*\{(.*?)\n\};" % union
     match = re.search(regex, input, re.S)
     if None == match:
         output += "#define %s_has_no_%s 1\n" % (arch, union)
@@ -189,7 +189,7 @@  for union in unions:
 
 # add structs to output
 for struct in structs:
-    regex = "(?:#ifdef ([A-Z_]+))?\nstruct\s+%s\s*\{(.*?)\n\};" % struct
+    regex = r"(?:#ifdef ([A-Z_]+))?\nstruct\s+%s\s*\{(.*?)\n\};" % struct
     match = re.search(regex, input, re.S)
     if None == match or \
            (match.group(1) is not None and match.group(1) not in defined):
@@ -211,20 +211,20 @@  for define in defines:
         replace = define + "_" + arch.upper()
     else:
         replace = define + "_" + arch
-    output = re.sub("\\b%s\\b" % define, replace, output)
+    output = re.sub(r"\b%s\b" % define, replace, output)
 
 # replace: unions
 for union in unions:
-    output = re.sub("\\b(union\s+%s)\\b" % union, "\\1_%s" % arch, output)
+    output = re.sub(r"\b(union\s+%s)\b" % union, r"\1_%s" % arch, output)
 
 # replace: structs + struct typedefs
 for struct in structs:
-    output = re.sub("\\b(struct\s+%s)\\b" % struct, "\\1_%s" % arch, output)
-    output = re.sub("\\b(%s)_t\\b" % struct, "\\1_%s_t" % arch, output)
+    output = re.sub(r"\b(struct\s+%s)\b" % struct, r"\1_%s" % arch, output)
+    output = re.sub(r"\b(%s)_t\b" % struct, r"\1_%s_t" % arch, output)
 
 # replace: integer types
 for old, new in inttypes[arch]:
-    output = re.sub("\\b%s\\b" % old, new, output)
+    output = re.sub(r"\b%s\b" % old, new, output)
 
 # print results
 with open(outfile, "w") as f: