[Piglit] [PATCH 1/5] python: Convert from print statement to function

Dylan Baker baker.dylan.c at gmail.com
Mon Feb 24 11:42:38 PST 2014


One of the biggest changes between python2 and python3 is that print
changed from a statement to a function. This change was backported to
python2 in the __future__ module, and this patch makes that conversation
to ease the transition from python2 to python3.

This patch does add a dictionary comprehension to
Summary.generate_text(), which are not available in python2 < 2.7, but
are available from at latest python 3.2 (the oldest supported version of
python3). Since a later patch in this series converts to python3, I
didn't feel it was a big deal to add this 2.7 dependency.

Signed-off-by: Dylan Baker <baker.dylan.c at gmail.com>
---
 framework/core.py        |  5 +++--
 framework/opencv.py      |  4 +++-
 framework/shader_test.py |  1 +
 framework/summary.py     | 36 +++++++++++++++++++-----------------
 generate-glean-tests.py  |  5 +++--
 piglit-print-commands.py |  3 ++-
 piglit-resume.py         |  1 +
 piglit-run.py            |  7 ++++---
 8 files changed, 36 insertions(+), 26 deletions(-)

diff --git a/framework/core.py b/framework/core.py
index 2cb2216..2863860 100644
--- a/framework/core.py
+++ b/framework/core.py
@@ -22,6 +22,7 @@
 
 # Piglit core
 
+from __future__ import print_function
 import errno
 import os
 import platform
@@ -204,8 +205,8 @@ def checkDir(dirname, failifexists):
             exists = False
 
     if exists and failifexists:
-        print >>sys.stderr, "%(dirname)s exists already.\nUse --overwrite if" \
-                            "you want to overwrite it.\n" % locals()
+        print("%(dirname)s exists already.\nUse --overwrite if "
+              "you want to overwrite it.\n" % locals(), file=sys.stderr)
         exit(1)
 
     try:
diff --git a/framework/opencv.py b/framework/opencv.py
index 2dc98bb..3e333c2 100644
--- a/framework/opencv.py
+++ b/framework/opencv.py
@@ -24,6 +24,7 @@
 # Authors: Tom Stellard <thomas.stellard at amd.com>
 #
 
+from __future__ import print_function
 import re
 import subprocess
 from os import path
@@ -46,7 +47,8 @@ def add_opencv_tests(profile, individual = False):
     opencv_test_ocl = path.join(PIGLIT_CONFIG.get('opencv',
         'opencv_test_ocl_bindir'), 'opencv_test_ocl')
     if not path.isfile(opencv_test_ocl):
-        print 'Warning: ', opencv_test_ocl, 'does not exist.\nSkipping OpenCV tests...'
+        print('Warning: {} does not exist.\nSkipping OpenCV '
+              'tests...'.format(opencv_test_ocl))
         return
     tests = subprocess.check_output([opencv_test_ocl, '--gtest_list_tests'])
     test_list = tests.splitlines()
diff --git a/framework/shader_test.py b/framework/shader_test.py
index 48697a7..e7b33d3 100755
--- a/framework/shader_test.py
+++ b/framework/shader_test.py
@@ -23,6 +23,7 @@
 # OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 # DEALINGS IN THE SOFTWARE.
 
+from __future__ import print_function
 import os
 import os.path as path
 import re
diff --git a/framework/summary.py b/framework/summary.py
index 2fc16ce..924fb2f 100644
--- a/framework/summary.py
+++ b/framework/summary.py
@@ -18,7 +18,8 @@
 # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
 # OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 # DEALINGS IN THE SOFTWARE.
-
+ 
+from __future__ import print_function
 import os
 import os.path as path
 import itertools
@@ -462,27 +463,28 @@ class Summary:
         if not summary:
             if diff:
                 for test in self.tests['changes']:
-                    print "%(test)s: %(statuses)s" % {'test': test, 'statuses':
+                    print("%(test)s: %(statuses)s" % {'test': test, 'statuses':
                           ' '.join([str(i.tests.get(test, {'result': so.Skip()})
-                                    ['result']) for i in self.results])}
+                                    ['result']) for i in self.results])})
             else:
                 for test in self.tests['all']:
-                    print "%(test)s: %(statuses)s" % {'test': test, 'statuses':
+                    print("%(test)s: %(statuses)s" % {'test': test, 'statuses':
                           ' '.join([str(i.tests.get(test, {'result': so.Skip()})
-                                    ['result']) for i in self.results])}
+                                    ['result']) for i in self.results])})
 
         # Print the summary
-        print "summary:"
-        print "       pass: %d" % self.totals['pass']
-        print "       fail: %d" % self.totals['fail']
-        print "      crash: %d" % self.totals['crash']
-        print "       skip: %d" % self.totals['skip']
-        print "       warn: %d" % self.totals['warn']
-        print " dmesg-warn: %d" % self.totals['dmesg-warn']
-        print " dmesg-fail: %d" % self.totals['dmesg-fail']
+        print("summary:\n"
+              "       pass: {pass}\n"
+              "       fail: {fail}\n"
+              "      crash: {crash}\n"
+              "       skip: {skip}\n"
+              "       warn: {warn}\n"
+              " dmesg-warn: {dmesg-warn}\n"
+              " dmesg-fail: {dmesg-fail}".format(**self.totals))
         if self.tests['changes']:
-            print "    changes: %d" % len(self.tests['changes'])
-            print "      fixes: %d" % len(self.tests['fixes'])
-            print "regressions: %d" % len(self.tests['regressions'])
+            print("    changes: {changes}\n"
+                  "      fixes: {fixes}\n"
+                  "regressions: {regressions}".format(
+                      **{k: len(v) for k, v in self.tests.iteritems()}))
 
-        print "      total: %d" % sum(self.totals.itervalues())
+        print("      total: {}".format(sum(self.totals.itervalues())))
diff --git a/generate-glean-tests.py b/generate-glean-tests.py
index e2c7167..df08ede 100755
--- a/generate-glean-tests.py
+++ b/generate-glean-tests.py
@@ -25,6 +25,7 @@
 # Authors:
 #    Eric Anholt <eric at anholt.net>
 
+from __future__ import print_function
 from getopt import getopt, GetoptError
 
 import re
@@ -38,7 +39,7 @@ Usage %(progName) [cppfile] [add_prefix]
 cppfile: path to glean cppfile to parse
 add_suffix: prefix to have in test name i.e. glsl1 -> add_glsl1
 """
-    print USAGE % {'progName': sys.argv[0]}
+    print(USAGE % {'progName': sys.argv[0]})
     sys.exit(1)
 
 
@@ -67,7 +68,7 @@ def main():
                 break
             if not re.match(r'//', name):
                 name = re.sub(r'".*', r'', name)
-                print "add_" + suffix + "('" + name + "')"
+                print("add_" + suffix + "('" + name + "')")
                 next_is_name = False
         if line == "    {\n":
             next_is_name = True
diff --git a/piglit-print-commands.py b/piglit-print-commands.py
index d49e5d9..4529608 100755
--- a/piglit-print-commands.py
+++ b/piglit-print-commands.py
@@ -22,6 +22,7 @@
 # DEALINGS IN THE SOFTWARE.
 
 
+from __future__ import print_function
 import argparse
 import sys
 import os
@@ -77,7 +78,7 @@ def main():
     profile.prepare_test_list(env)
     for name, test in profile.test_list.items():
         assert(isinstance(test, ExecTest))
-        print name, ':::', getCommand(test)
+        print(name, ':::', getCommand(test))
 
 
 if __name__ == "__main__":
diff --git a/piglit-resume.py b/piglit-resume.py
index 06bba4d..c9cb72b 100755
--- a/piglit-resume.py
+++ b/piglit-resume.py
@@ -21,6 +21,7 @@
 # OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 # DEALINGS IN THE SOFTWARE.
 
+from __future__ import print_function
 import sys
 import os
 import os.path as path
diff --git a/piglit-run.py b/piglit-run.py
index 5fc9baf..d57abd7 100755
--- a/piglit-run.py
+++ b/piglit-run.py
@@ -22,6 +22,7 @@
 # DEALINGS IN THE SOFTWARE.
 
 
+from __future__ import print_function
 import argparse
 import sys
 import os
@@ -176,9 +177,9 @@ def main():
     json_writer.close_dict()
     json_writer.file.close()
 
-    print
-    print 'Thank you for running Piglit!'
-    print 'Results have been written to ' + result_filepath
+    print('\n'
+          'Thank you for running Piglit!\n'
+          'Results have been written to ' + result_filepath)
 
 
 if __name__ == "__main__":
-- 
1.9.0



More information about the Piglit mailing list