[PATCH v2] tools/misc/xencov_split: Add python 3 compatibility

Javi Merino posted 1 patch 7 months, 3 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://gitlab.com/xen-project/patchew/xen tags/patchew/20230905201653.98425-1-javi.merino@cloud.com
tools/misc/xencov_split | 15 +++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)
[PATCH v2] tools/misc/xencov_split: Add python 3 compatibility
Posted by Javi Merino 7 months, 3 weeks ago
Closes #154

Signed-off-by: Javi Merino <javi.merino@cloud.com>
---

Changes since v1:
  - Don't touch the shebang.

 tools/misc/xencov_split | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/tools/misc/xencov_split b/tools/misc/xencov_split
index e4f68ebb6e..a921e8ef44 100755
--- a/tools/misc/xencov_split
+++ b/tools/misc/xencov_split
@@ -1,5 +1,7 @@
 #!/usr/bin/env python
 
+from __future__ import print_function
+from builtins import str
 import sys, os, os.path as path, struct, errno
 from optparse import OptionParser
 
@@ -16,7 +18,7 @@ def xencov_split(opts):
 
     input_file = opts.args[0]
 
-    f = open(input_file)
+    f = open(input_file, "rb")
 
     # Magic number
     s = f.read(4)
@@ -31,9 +33,10 @@ def xencov_split(opts):
     f.close()
 
     while content:
-        off = content.find('\x00')
+        off = content.find(b'\x00')
         fmt = bo_prefix + str(off) + 's'
         fn, = struct.unpack_from(fmt, content)
+        fn = fn.decode('utf-8')
         content = content[off+1:]
 
         fmt = bo_prefix + 'I'
@@ -51,14 +54,14 @@ def xencov_split(opts):
         dir = opts.output_dir + path.dirname(fn)
         try:
             os.makedirs(dir)
-        except OSError, e:
+        except OSError as e:
             if e.errno == errno.EEXIST and os.path.isdir(dir):
                 pass
             else:
                 raise
 
         full_path = dir + '/' + path.basename(fn)
-        f = open(full_path, "w")
+        f = open(full_path, "wb")
         f.write(payload)
         f.close()
 
@@ -89,8 +92,8 @@ def main():
 if __name__ == "__main__":
     try:
         sys.exit(main())
-    except Exception, e:
-        print >>sys.stderr, "Error:", e
+    except Exception as e:
+        print("Error:", e, file=sys.stderr)
         sys.exit(1)
     except KeyboardInterrupt:
         sys.exit(1)
-- 
2.41.0
Re: [PATCH v2] tools/misc/xencov_split: Add python 3 compatibility
Posted by Andrew Cooper 7 months, 2 weeks ago
On 05/09/2023 9:15 pm, Javi Merino wrote:
> Closes #154

This wants to be:

Resolves: xen-project/xen#154

I'll fix on commit.

> Signed-off-by: Javi Merino <javi.merino@cloud.com>

Acked-by: Andrew Cooper <andrew.cooper3@citrix.com>