Skip to contentSkip to conversation

xen/tools: support Python 2 and Python 3

PATCH In review Doug Goldstein 3 participants 5 messages Feb 22, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/tools: support Python 2 and Python 3

These changes should make it possible to support modern Pythons as well as the oldest Python 2 still supported.

---
CC: Andrew Cooper <[email protected]>
CC: George Dunlap <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Konrad Rzeszutek Wilk <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Tim Deegan <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/tools/compat-build-header.py |  2 +-
 xen/tools/compat-build-source.py |  2 +-
 xen/tools/gen-cpuid.py           | 37 +++++++++++++++++++++++++++----------
 xen/tools/get-fields.sh          |  2 +-
 4 files changed, 30 insertions(+), 13 deletions(-)
xen/tools/compat-build-header.py +1 -1
index 32421b645b..546371225d 100755
@@ -23,4 +23,4 @@ pats = [
 for line in sys.stdin.readlines():
     for pat in pats:
         line = re.subn(pat[0], pat[1], line)[0]
-    print line.rstrip()
+    sys.stdout.write(line.rstrip() + '\n')
xen/tools/compat-build-source.py +1 -1
index 595bc3ff58..8101290ebe 100755
@@ -26,4 +26,4 @@ for pat in pats:
 for line in sys.stdin.readlines():
     for pat in pats:
         line = re.sub(pat[0], pat[1], line)
-    print line.rstrip()
+    sys.stdout.write(line.rstrip() + '\n')
xen/tools/gen-cpuid.py +27 -10
index 613b909c3d..d64f257816 100755
@@ -3,6 +3,13 @@
 
 import sys, os, re
 
+if (sys.version_info > (3, 0)):
+    def long(x):
+        return x
+
+    def xrange(x):
+        return range(x)
+
 class Fail(Exception):
     pass
 
@@ -98,13 +105,13 @@ def parse_definitions(state):
 def featureset_to_uint32s(fs, nr):
     """ Represent a featureset as a list of C-compatible uint32_t's """
 
-    bitmap = 0L
+    bitmap = long(0)
     for f in fs:
-        bitmap |= 1L << f
+        bitmap |= long(1) << f
 
     words = []
     while bitmap:
-        words.append(bitmap & ((1L << 32) - 1))
+        words.append(bitmap & ((long(1) << 32) - 1))
         bitmap >>= 32
 
     assert len(words) <= nr
@@ -276,8 +283,8 @@ def crunch_numbers(state):
             # To debug, uncomment the following lines:
             # def repl(l):
             #     return "[" + ", ".join((state.names[x] for x in l)) + "]"
-            # print >>sys.stderr, "Feature %s, seen %s, to_process %s " % \
-            #     (state.names[feat], repl(seen), repl(to_process))
+            # sys.stderr.write("Feature %s, seen %s, to_process %s \n" % \
+            #     (state.names[feat], repl(seen), repl(to_process)))
 
             f = to_process.pop(0)
 
@@ -293,7 +300,12 @@ def crunch_numbers(state):
     state.deep_features = featureset_to_uint32s(deps.keys(), nr_entries)
     state.nr_deep_deps = len(state.deep_deps.keys())
 
-    for k, v in state.deep_deps.iteritems():
+    try:
+        _tmp = state.deep_deps.iteritems()
+    except AttributeError:
+        _tmp = state.deep_deps.items()
+
+    for k, v in _tmp:
         state.deep_deps[k] = featureset_to_uint32s(v, nr_entries)
 
     # Calculate the bitfield name declarations
@@ -410,7 +422,8 @@ def open_file_or_fd(val, mode, buffering):
         else:
             return open(val, mode, buffering)
 
-    except StandardError, e:
+    except StandardError:
+        e = sys.exc_info()[1]
         if fd != -1:
             raise Fail("Unable to open fd %d: %s: %s" %
                        (fd, e.__class__.__name__, e))
@@ -453,10 +466,14 @@ def main():
 if __name__ == "__main__":
     try:
         sys.exit(main())
-    except Fail, e:
-        print >>sys.stderr, "%s:" % (sys.argv[0],), e
+    except Fail:
+        e = sys.exc_info()[1]
+        sys.stderr.write("%s:" % (sys.argv[0],))
+        sys.stderr.write(e)
+        sys.stderr.write("\n")
         sys.exit(1)
-    except SystemExit, e:
+    except SystemExit:
+        e = sys.exc_info()[1]
         sys.exit(e.code)
     except KeyboardInterrupt:
         sys.exit(2)
xen/tools/get-fields.sh +1 -1
index 887feec370..45a0e2ed50 100644
@@ -139,7 +139,7 @@ handle_field ()
 		local tag=$(echo "$5" | ${PYTHON} -c '
 import re,sys
 for line in sys.stdin.readlines():
-    print re.subn(r"\s*(struct|union)\s+(compat_)?(\w+)\s.*", r"\3", line)[0].rstrip()
+    sys.stdout.write(re.subn(r"\s*(struct|union)\s+(compat_)?(\w+)\s.*", r"\3", line)[0].rstrip() + "\n")
 ')
 		echo " \\"
 		printf %s "${1}XLAT_$tag(&(_d_)->$3, &(_s_)->$3);"
Signature · 7 lines
2.13.6


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw
Quoted 4 lines · Doug Goldstein

These changes should make it possible to support modern Pythons as well as the oldest Python 2 still supported.

To the overall effect, this is definitely a good thing.  Some queries however...

Quoted 21 lines

---
CC: Andrew Cooper <[email protected]>
CC: George Dunlap <[email protected]>
CC: Ian Jackson <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Konrad Rzeszutek Wilk <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Tim Deegan <[email protected]>
CC: Wei Liu <[email protected]>

---
 xen/tools/compat-build-header.py |  2 +-
 xen/tools/compat-build-source.py |  2 +-
 xen/tools/gen-cpuid.py           | 37 +++++++++++++++++++++++++++----------
 xen/tools/get-fields.sh          |  2 +-
 4 files changed, 30 insertions(+), 13 deletions(-)
xen/tools/compat-build-header.py +0 -0
index 32421b645b..546371225d 100755

xen/tools/compat-build-header.py pats = [ · line 26

Quoted 5 lines
xen/tools/compat-build-header.py +1 -1
@@ -23,4 +23,4 @@ pats = [
 for line in sys.stdin.readlines():
     for pat in pats:
         line = re.subn(pat[0], pat[1], line)[0]
-    print line.rstrip()
+    sys.stdout.write(line.rstrip() + '\n')

Is there anything wrong with print(line.rstrip()) which is the more common way of doing this?

Quoted 15 lines
xen/tools/compat-build-source.py +1 -1
index 595bc3ff58..8101290ebe 100755
@@ -26,4 +26,4 @@ for pat in pats:
 for line in sys.stdin.readlines():
     for pat in pats:
         line = re.sub(pat[0], pat[1], line)
-    print line.rstrip()
+    sys.stdout.write(line.rstrip() + '\n')
xen/tools/gen-cpuid.py +0 -0
index 613b909c3d..d64f257816 100755

xen/tools/gen-cpuid.py line 8

Quoted 6 lines
xen/tools/gen-cpuid.py +3 -0
@@ -3,6 +3,13 @@
 
 import sys, os, re
 
+if (sys.version_info > (3, 0)):
+    def long(x):
+        return x

Strictly speaking, return int(x), however...

Quoted 8 lines
xen/tools/gen-cpuid.py +4 -0
+
+    def xrange(x):
+        return range(x)
+
 class Fail(Exception):
     pass
 

xen/tools/gen-cpuid.py def parse_definitions(state): · line 110

Quoted 8 lines
xen/tools/gen-cpuid.py +2 -2
@@ -98,13 +105,13 @@ def parse_definitions(state):
 def featureset_to_uint32s(fs, nr):
     """ Represent a featureset as a list of C-compatible uint32_t's """
 
-    bitmap = 0L
+    bitmap = long(0)
     for f in fs:
-        bitmap |= 1L << f
+        bitmap |= long(1) << f

Having tested this out, I think the L suffixes can just be dropped:

Python 2.4.3 (#1, Sep 21 2011, 20:06:00)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

bitmap = 0
type(bitmap)

<type 'int'>

Quoted 1 line
Quoted 1 line
Quoted 1 line

for x in xrange(80): bitmap |= 1 << x

...

Quoted 1 line
Quoted 1 line
Quoted 1 line

type(bitmap)

<type 'long'>

Quoted 1 line
Quoted 1 line
Quoted 1 line

I don't recall why I wrote it like this, but I don't think its necessary.  As a result, you shouldn't need to define long above.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 10 lines · Andrew Cooper
Quoted 4 lines · Doug Goldstein

These changes should make it possible to support modern Pythons as well as the oldest Python 2 still supported.

To the overall effect, this is definitely a good thing.  Some queries however...

Quoted 13 lines

xen/tools/compat-build-header.py pats = [ · line 26

Quoted 10 lines
xen/tools/compat-build-header.py +1 -1
index 32421b645b..546371225d 100755
@@ -23,4 +23,4 @@ pats = [
 for line in sys.stdin.readlines():
     for pat in pats:
         line = re.subn(pat[0], pat[1], line)[0]
-    print line.rstrip()
+    sys.stdout.write(line.rstrip() + '\n')

Is there anything wrong with print(line.rstrip()) which is the more common way of doing this?

The other scripts in the repo avoided using print as a function which was only added to Python 2.6 and newer if you use from __future__ import print_function. The README in the repo still says Python 2.3 and newer so I had assumed if I broke things prior to Python 2.6 it would be a show stopper. While Python 2.3 was released July 29th 2003 and Python 2.6 was released Oct 1st 2008 I figured changing this requirement was going to be met with the same difficultly I encountered last time suggesting that we only support versions of build tools released in the past decade.

Quoted 52 lines
Quoted 16 lines
xen/tools/compat-build-source.py +1 -1
index 595bc3ff58..8101290ebe 100755
@@ -26,4 +26,4 @@ for pat in pats:
 for line in sys.stdin.readlines():
     for pat in pats:
         line = re.sub(pat[0], pat[1], line)
-    print line.rstrip()
+    sys.stdout.write(line.rstrip() + '\n')
xen/tools/gen-cpuid.py +0 -0
index 613b909c3d..d64f257816 100755

xen/tools/gen-cpuid.py line 8

Quoted 6 lines
xen/tools/gen-cpuid.py +3 -0
@@ -3,6 +3,13 @@
 
 import sys, os, re
 
+if (sys.version_info > (3, 0)):
+    def long(x):
+        return x

Strictly speaking, return int(x), however...

Quoted 8 lines
xen/tools/gen-cpuid.py +4 -0
+
+    def xrange(x):
+        return range(x)
+
 class Fail(Exception):
     pass
 

xen/tools/gen-cpuid.py def parse_definitions(state): · line 110

Quoted 8 lines
xen/tools/gen-cpuid.py +2 -2
@@ -98,13 +105,13 @@ def parse_definitions(state):
 def featureset_to_uint32s(fs, nr):
     """ Represent a featureset as a list of C-compatible uint32_t's """
 
-    bitmap = 0L
+    bitmap = long(0)
     for f in fs:
-        bitmap |= 1L << f
+        bitmap |= long(1) << f

Having tested this out, I think the L suffixes can just be dropped:

Python 2.4.3 (#1, Sep 21 2011, 20:06:00)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

bitmap = 0
type(bitmap)

<type 'int'>

Quoted 1 line
Quoted 1 line
Quoted 1 line

for x in xrange(80): bitmap |= 1 << x

...

Quoted 8 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

type(bitmap)

<type 'long'>

Quoted 1 line
Quoted 1 line
Quoted 1 line

I don't recall why I wrote it like this, but I don't think its necessary.  As a result, you shouldn't need to define long above.

~Andrew

Python 2.4 and newer will properly extend for you but Python 2.3 will not and I was aiming to still support Python 2.3. Now this is all theoretical since I can't find any boxes with Python 2.3 anywhere and I'm not interested in building it.

Signature · 1 line
Doug Goldstein

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 25 lines · Doug Goldstein
Quoted 10 lines · Andrew Cooper
Quoted 4 lines · Doug Goldstein

These changes should make it possible to support modern Pythons as well as the oldest Python 2 still supported.

To the overall effect, this is definitely a good thing.  Some queries however...

Quoted 13 lines

xen/tools/compat-build-header.py pats = [ · line 26

Quoted 10 lines
xen/tools/compat-build-header.py +1 -1
index 32421b645b..546371225d 100755
@@ -23,4 +23,4 @@ pats = [
 for line in sys.stdin.readlines():
     for pat in pats:
         line = re.subn(pat[0], pat[1], line)[0]
-    print line.rstrip()
+    sys.stdout.write(line.rstrip() + '\n')

Is there anything wrong with print(line.rstrip()) which is the more common way of doing this?

Quoted 10 lines

The other scripts in the repo avoided using print as a function which was only added to Python 2.6 and newer if you use from __future__ import print_function. The README in the repo still says Python 2.3 and newer so I had assumed if I broke things prior to Python 2.6 it would be a show stopper. While Python 2.3 was released July 29th 2003 and Python 2.6 was released Oct 1st 2008 I figured changing this requirement was going to be met with the same difficultly I encountered last time suggesting that we only support versions of build tools released in the past decade.

2.3 is more than ~15 years ago, can we seek to bump the version a bit. :-)

2.6 is reaching its 10 birthday this year, maybe it is better to just use that?

What difficulty you had last time?

Wei.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
In reply to Doug Goldstein
Quoted 21 lines · Doug Goldstein
Quoted 8 lines · Andrew Cooper
Quoted 4 lines · Doug Goldstein

These changes should make it possible to support modern Pythons as well as the oldest Python 2 still supported.

To the overall effect, this is definitely a good thing.  Some queries however...

Quoted 12 lines

xen/tools/compat-build-header.py pats = [ · line 26

Quoted 10 lines
xen/tools/compat-build-header.py +1 -1
index 32421b645b..546371225d 100755
@@ -23,4 +23,4 @@ pats = [
 for line in sys.stdin.readlines():
     for pat in pats:
         line = re.subn(pat[0], pat[1], line)[0]
-    print line.rstrip()
+    sys.stdout.write(line.rstrip() + '\n')

Is there anything wrong with print(line.rstrip()) which is the more common way of doing this?

Quoted 9 lines

The other scripts in the repo avoided using print as a function which was only added to Python 2.6 and newer if you use from __future__ import print_function. The README in the repo still says Python 2.3 and newer so I had assumed if I broke things prior to Python 2.6 it would be a show stopper. While Python 2.3 was released July 29th 2003 and Python 2.6 was released Oct 1st 2008 I figured changing this requirement was going to be met with the same difficultly I encountered last time suggesting that we only support versions of build tools released in the past decade.

The print function, or lack thereof, doesn't matter.

The above works with all of python 2, because it parses as "print_stmt, expression of type str"

The only bit where it goes wonky is if you try to use any of the optional print function parameters, at which point Py2 will complain.

Quoted 54 lines
Quoted 54 lines
Quoted 15 lines
xen/tools/compat-build-source.py +1 -1
index 595bc3ff58..8101290ebe 100755
@@ -26,4 +26,4 @@ for pat in pats:
 for line in sys.stdin.readlines():
     for pat in pats:
         line = re.sub(pat[0], pat[1], line)
-    print line.rstrip()
+    sys.stdout.write(line.rstrip() + '\n')
xen/tools/gen-cpuid.py +0 -0
index 613b909c3d..d64f257816 100755

xen/tools/gen-cpuid.py line 8

Quoted 6 lines
xen/tools/gen-cpuid.py +3 -0
@@ -3,6 +3,13 @@
 
 import sys, os, re
 
+if (sys.version_info > (3, 0)):
+    def long(x):
+        return x

Strictly speaking, return int(x), however...

Quoted 8 lines
xen/tools/gen-cpuid.py +4 -0
+
+    def xrange(x):
+        return range(x)
+
 class Fail(Exception):
     pass
 

xen/tools/gen-cpuid.py def parse_definitions(state): · line 110

Quoted 8 lines
xen/tools/gen-cpuid.py +2 -2
@@ -98,13 +105,13 @@ def parse_definitions(state):
 def featureset_to_uint32s(fs, nr):
     """ Represent a featureset as a list of C-compatible uint32_t's """
 
-    bitmap = 0L
+    bitmap = long(0)
     for f in fs:
-        bitmap |= 1L << f
+        bitmap |= long(1) << f

Having tested this out, I think the L suffixes can just be dropped:

Python 2.4.3 (#1, Sep 21 2011, 20:06:00)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

bitmap = 0
type(bitmap)

<type 'int'>

Quoted 1 line
Quoted 1 line
Quoted 1 line

for x in xrange(80): bitmap |= 1 << x

...

Quoted 1 line
Quoted 1 line
Quoted 1 line

type(bitmap)

<type 'long'>
I don't recall why I wrote it like this, but I don't think its necessary.  As a result, you shouldn't need to define long above.

Quoted 5 lines
Quoted 1 line

~Andrew

Python 2.4 and newer will properly extend for you but Python 2.3 will not and I was aiming to still support Python 2.3. Now this is all theoretical since I can't find any boxes with Python 2.3 anywhere and I'm not interested in building it.

Oh - lovely :(  So we either keep that as it was, or decide that continuing to support a 15 year old thing is bordering on the excessive.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel