Skip to contentSkip to conversation

build/mkheader: Fixes

PATCH In review Andrew Cooper 3 participants 6 messages Jul 3, 2024 – Jul 4, 2024

lore
Share & tools
lore
#0 lore raw

cover build/mkheader: Fixes

For 4.19? Noticed while staring at build logs. Low risk, as the build system contains a cross-check that mkheader generates the same layout.

Not the end of the world if these fixes delay to 4.20.

Andrew Cooper (2):
build/mkheader: Remove C-isms from the code
build/mkheader: Fix Syntax/DeprecationWarnings

 tools/include/xen-foreign/mkheader.py | 108 +++++++++++++-------------
 1 file changed, 53 insertions(+), 55 deletions(-)
Signature · 1 line
2.39.2
#1 lore raw

PATCH 1/2 build/mkheader: Remove C-isms from the code

This was clearly written by a C programmer, rather than a python programmer. Drop all the useless semi-colons.

The very final line of the script simply references f.close, rather than calling the function. Switch to using a with: statement, as python does care about unclosed files if you enable enough warnings.

No functional change.

---
CC: Anthony PERARD <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Oleksii Kurochko <[email protected]>

---
 tools/include/xen-foreign/mkheader.py | 104 +++++++++++++-------------
 1 file changed, 51 insertions(+), 53 deletions(-)
tools/include/xen-foreign/mkheader.py +51 -53
index 081b57f842a2..3a33df4984a2 100644
@@ -2,21 +2,21 @@
 
 from __future__ import print_function
 
-import sys, re;
-from structs import unions, structs, defines;
+import sys, re
+from structs import unions, structs, defines
 
 # command line arguments
-arch    = sys.argv[1];
-outfile = sys.argv[2];
-infiles = sys.argv[3:];
+arch    = sys.argv[1]
+outfile = sys.argv[2]
+infiles = sys.argv[3:]
 
 
 ###########################################################################
 # configuration #2: architecture information
 
-inttypes = {};
-header = {};
-footer = {};
+inttypes = {}
+header = {}
+footer = {}
 
 #arm
 inttypes["arm32"] = [
@@ -35,7 +35,7 @@ header["arm32"] = """
 # define __DECL_REG(n64, n32) uint64_t n64
 # define __align8__ FIXME
 #endif
-""";
+"""
 footer["arm32"] = """
 #undef __DECL_REG
 """
@@ -56,7 +56,7 @@ header["arm64"] = """
 # define __DECL_REG(n64, n32) uint64_t n64
 # define __align8__ FIXME
 #endif
-""";
+"""
 footer["arm64"] = """
 #undef __DECL_REG
 """
@@ -73,12 +73,12 @@ header["x86_32"] = """
 #define __DECL_REG_LO16(name) uint32_t e ## name
 #define __i386___X86_32 1
 #pragma pack(4)
-""";
+"""
 footer["x86_32"] = """
 #undef __DECL_REG_LO8
 #undef __DECL_REG_LO16
 #pragma pack()
-""";
+"""
 
 # x86_64
 inttypes["x86_64"] = [
@@ -100,7 +100,7 @@ header["x86_64"] = """
 #define __DECL_REG_LO16       __DECL_REG
 #define __DECL_REG_HI         __DECL_REG
 #define __x86_64___X86_64 1
-""";
+"""
 footer["x86_64"] = """
 #undef __DECL_REG
 #undef __DECL_REG_LOHI
@@ -112,12 +112,12 @@ footer["x86_64"] = """
 ###########################################################################
 # main
 
-input  = "";
-output = "";
-fileid = re.sub("[-.]", "_", "__FOREIGN_%s__" % outfile.upper());
+input  = ""
+output = ""
+fileid = re.sub("[-.]", "_", "__FOREIGN_%s__" % outfile.upper())
 
 for name in infiles:
-    f = open(name, "r");
+    f = open(name, "r")
 
     # Sanity check the licence of the input file(s)
     line = f.readline()
@@ -126,8 +126,8 @@ for name in infiles:
               (sys.argv[0], name, line.strip()), file=sys.stderr)
         exit(1)
 
-    input += f.read();
-    f.close();
+    input += f.read()
+    f.close()
 
 # replace path in "infiles" by path in '/usr/include' to avoid exposing the
 # build directory path in the generated headers.
@@ -151,83 +151,81 @@ output += """/* SPDX-License-Identifier: MIT */
 """ % (arch, headers_name_list, sys.argv[0], fileid, fileid)
 
 if arch in header:
-    output += header[arch];
-    output += "\n";
+    output += header[arch]
+    output += "\n"
 
 defined = {}
 
 # add defines to output
 for line in re.findall("#define[^\n]+", input):
     for define in defines:
-        regex = "#define\s+%s\\b" % define;
-        match = re.search(regex, line);
+        regex = "#define\s+%s\\b" % define
+        match = re.search(regex, line)
         if None == match:
-            continue;
+            continue
         defined[define] = 1
         if define.upper()[0] == define[0]:
-            replace = define + "_" + arch.upper();
+            replace = define + "_" + arch.upper()
         else:
-            replace = define + "_" + arch;
-        regex = "\\b%s\\b" % define;
-        output += re.sub(regex, replace, line) + "\n";
-output += "\n";
+            replace = define + "_" + arch
+        regex = "\\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.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("\n\s*\n", re.S).sub("\n", input)
 
 # add unions to output
 for union in unions:
-    regex = "union\s+%s\s*\{(.*?)\n\};" % union;
+    regex = "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);
+        output += "#define %s_has_no_%s 1\n" % (arch, union)
     else:
-        output += "union %s_%s {%s\n};\n" % (union, arch, match.group(1));
-    output += "\n";
+        output += "union %s_%s {%s\n};\n" % (union, arch, match.group(1))
+    output += "\n"
 
 # add structs to output
 for struct in structs:
-    regex = "(?:#ifdef ([A-Z_]+))?\nstruct\s+%s\s*\{(.*?)\n\};" % struct;
+    regex = "(?:#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):
-        output += "#define %s_has_no_%s 1\n" % (arch, struct);
+        output += "#define %s_has_no_%s 1\n" % (arch, struct)
     else:
-        output += "struct %s_%s {%s\n};\n" % (struct, arch, match.group(2));
-        output += "typedef struct %s_%s %s_%s_t;\n" % (struct, arch, struct, arch);
-    output += "\n";
+        output += "struct %s_%s {%s\n};\n" % (struct, arch, match.group(2))
+        output += "typedef struct %s_%s %s_%s_t;\n" % (struct, arch, struct, arch)
+    output += "\n"
 
 # add footer
 if arch in footer:
-    output += footer[arch];
-    output += "\n";
-output += "#endif /* %s */\n" % fileid;
+    output += footer[arch]
+    output += "\n"
+output += "#endif /* %s */\n" % fileid
 
 # replace: defines
 for define in defines:
     if define.upper()[0] == define[0]:
-        replace = define + "_" + arch.upper();
+        replace = define + "_" + arch.upper()
     else:
-        replace = define + "_" + arch;
-    output = re.sub("\\b%s\\b" % define, replace, output);
+        replace = define + "_" + arch
+    output = re.sub("\\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("\\b(union\s+%s)\\b" % union, "\\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("\\b(struct\s+%s)\\b" % struct, "\\1_%s" % arch, output)
+    output = re.sub("\\b(%s)_t\\b" % struct, "\\1_%s_t" % arch, output)
 
 # replace: integer types
 for old, new in inttypes[arch]:
     output = re.sub("\\b%s\\b" % old, new, output)
 
 # print results
-f = open(outfile, "w");
-f.write(output);
-f.close;
-
+with open(outfile, "w") as f:
+    f.write(output)
Signature · 1 line
2.39.2
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] build/mkheader: Remove C-isms from the code

Quoted 10 lines · Andrew Cooper

This was clearly written by a C programmer, rather than a python programmer. Drop all the useless semi-colons.

The very final line of the script simply references f.close, rather than calling the function. Switch to using a with: statement, as python does care about unclosed files if you enable enough warnings.

No functional change.

Thanks,

Signature · 5 lines
Anthony Perard | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#3 lore raw

PATCH 2/2 build/mkheader: Fix Syntax/DeprecationWarnings

In reply to Andrew Cooper

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.

---
CC: Anthony PERARD <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Julien Grall <[email protected]>
CC: Oleksii Kurochko <[email protected]>

---
 tools/include/xen-foreign/mkheader.py | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
tools/include/xen-foreign/mkheader.py +11 -11
index 3a33df4984a2..128b0f3014c2 100644
@@ -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:
Signature · 1 line
2.39.2
Reviewed-by on v1 2/2
#4 lore raw

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

Quoted 17 lines · Andrew Cooper

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);
Quoted 7 lines

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.

Thanks,

Signature · 5 lines
Anthony Perard | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#5 lore raw
In reply to Andrew Cooper
Quoted 5 lines · Andrew Cooper

For 4.19?  Noticed while staring at build logs.  Low risk, as the build system
contains a cross-check that mkheader generates the same layout.

Not the end of the world if these fixes delay to 4.20.

~ Oleksii

Quoted 9 lines

Andrew Cooper (2):
  build/mkheader: Remove C-isms from the code
  build/mkheader: Fix Syntax/DeprecationWarnings

 tools/include/xen-foreign/mkheader.py | 108 +++++++++++++----------- --

 1 file changed, 53 insertions(+), 55 deletions(-)