[test-suite] r350382 - Make Python scripts portable across Python 2/3

Serge Guelton via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 4 00:14:43 PST 2019


Author: serge_sans_paille
Date: Fri Jan  4 00:14:43 2019
New Revision: 350382

URL: http://llvm.org/viewvc/llvm-project?rev=350382&view=rev
Log:
Make Python scripts portable across Python 2/3

Mostly:

- harmonize print function
- harmonize item iteration
- explicitly force list creation when needed

Differential Revision: https://reviews.llvm.org/D55829

Modified:
    test-suite/trunk/CompareDebugInfo.py
    test-suite/trunk/litsupport/modules/stats.py
    test-suite/trunk/utils/compare.py
    test-suite/trunk/utils/tdiff.py

Modified: test-suite/trunk/CompareDebugInfo.py
URL: http://llvm.org/viewvc/llvm-project/test-suite/trunk/CompareDebugInfo.py?rev=350382&r1=350381&r2=350382&view=diff
==============================================================================
--- test-suite/trunk/CompareDebugInfo.py (original)
+++ test-suite/trunk/CompareDebugInfo.py Fri Jan  4 00:14:43 2019
@@ -1,4 +1,5 @@
 #!/usr/bin/python
+from __future__ import print_function
 
 import os
 import sys
@@ -45,14 +46,14 @@ class BreakPoint:
         self.values[arg_name] = value
         
     def __repr__(self):
-        print self.name
+        print(self.name)
         for k, v in self.values.items():
-            print k, "=", v
+            print(k, "=", v)
         return ''
 
     def compare_args(self, other, file):
         myitems = self.values.items()
-        otheritems = other.values.items()
+        otheritems = list(other.values.items())
         match = False
         for i, my_item in enumerate(my_items):
             if i >= len(otheritems):

Modified: test-suite/trunk/litsupport/modules/stats.py
URL: http://llvm.org/viewvc/llvm-project/test-suite/trunk/litsupport/modules/stats.py?rev=350382&r1=350381&r2=350382&view=diff
==============================================================================
--- test-suite/trunk/litsupport/modules/stats.py (original)
+++ test-suite/trunk/litsupport/modules/stats.py Fri Jan  4 00:14:43 2019
@@ -14,7 +14,7 @@ def _mergeStats(global_stats, statsfilen
     except Exception as e:
         logging.warning("Could not read '%s'", statsfilename, exc_info=e)
         return
-    for name, value in stats.iteritems():
+    for name, value in stats.items():
         global_stats[name] += value
 
 
@@ -37,7 +37,7 @@ def _getStats(context):
         logging.warning("No stats for '%s'", context.test.getFullName())
 
     result = dict()
-    for key, value in stats.iteritems():
+    for key, value in stats.items():
         result[key] = value
     return result
 

Modified: test-suite/trunk/utils/compare.py
URL: http://llvm.org/viewvc/llvm-project/test-suite/trunk/utils/compare.py?rev=350382&r1=350381&r2=350382&view=diff
==============================================================================
--- test-suite/trunk/utils/compare.py (original)
+++ test-suite/trunk/utils/compare.py Fri Jan  4 00:14:43 2019
@@ -1,8 +1,10 @@
-#!/usr/bin/env python2.7
+#!/usr/bin/env python
 """Tool to filter, organize, compare and display benchmarking results. Usefull
 for smaller datasets. It works great with a few dozen runs it is not designed to
 deal with hundreds.
 Requires the pandas library to be installed."""
+from __future__ import print_function
+
 import pandas as pd
 import sys
 import os.path
@@ -19,7 +21,7 @@ def read_lit_json(filename):
     info_columns = ['hash']
     # Pass1: Figure out metrics (= the column index)
     if 'tests' not in jsondata:
-        print "%s: Could not find toplevel 'tests' key"
+        print("%s: Could not find toplevel 'tests' key")
         sys.exit(1)
     for test in jsondata['tests']:
         name = test.get("name")
@@ -31,7 +33,7 @@ def read_lit_json(filename):
             sys.exit(1)
         names.add(name)
         if "metrics" not in test:
-            print "Warning: '%s' has No metrics!" % test['name']
+            print("Warning: '%s' has No metrics!" % test['name'])
             continue
         for name in test["metrics"].keys():
             if name not in columnindexes:
@@ -54,9 +56,9 @@ def read_lit_json(filename):
 
         datarow = [nan] * len(columns)
         if "metrics" in test:
-            for (metricname, value) in test['metrics'].iteritems():
+            for (metricname, value) in test['metrics'].items():
                 datarow[columnindexes[metricname]] = value
-        for (name, value) in test.iteritems():
+        for (name, value) in test.items():
             index = columnindexes.get(name)
             if index is not None:
                 datarow[index] = test[name]
@@ -148,7 +150,7 @@ def print_filter_stats(reason, before, a
     n_after = len(after.groupby(level=1))
     n_filtered = n_before - n_after
     if n_filtered != 0:
-        print "%s: %s (filtered out)" % (reason, n_filtered)
+        print("%s: %s (filtered out)" % (reason, n_filtered))
 
 # Truncate a string to a maximum length by keeping a prefix, a suffix and ...
 # in the middle
@@ -222,8 +224,8 @@ def print_result(d, limit_output=True, s
     pd.set_option("display.max_colwidth", 0)
     out = dataout.to_string(index=False, justify='left',
                             float_format=float_format, formatters=formatters)
-    print out
-    print d.describe()
+    print(out)
+    print(d.describe())
 
 if __name__ == "__main__":
     parser = argparse.ArgumentParser(prog='compare.py')
@@ -303,7 +305,7 @@ if __name__ == "__main__":
     # Filter data
     proggroup = data.groupby(level=1)
     initial_size = len(proggroup.indices)
-    print "Tests: %s" % (initial_size,)
+    print("Tests: %s" % (initial_size,))
     if config.filter_failed and hasattr(data, 'Exec'):
         newdata = filter_failed(data)
         print_filter_stats("Failed", data, newdata)
@@ -326,10 +328,10 @@ if __name__ == "__main__":
         data = newdata
     final_size = len(data.groupby(level=1))
     if final_size != initial_size:
-        print "Remaining: %d" % (final_size,)
+        print("Remaining: %d" % (final_size,))
 
     # Reduce / add columns
-    print "Metric: %s" % (",".join(metrics),)
+    print("Metric: %s" % (",".join(metrics),))
     if len(metrics) > 0:
         data = data[metrics]
     data = add_diff_column(data)
@@ -339,7 +341,7 @@ if __name__ == "__main__":
         sortkey = data.columns[0]
 
     # Print data
-    print ""
+    print("")
     shorten_names = not config.full
     limit_output = (not config.all) and (not config.full)
     print_result(data, limit_output, shorten_names, config.show_diff, sortkey)

Modified: test-suite/trunk/utils/tdiff.py
URL: http://llvm.org/viewvc/llvm-project/test-suite/trunk/utils/tdiff.py?rev=350382&r1=350381&r2=350382&view=diff
==============================================================================
--- test-suite/trunk/utils/tdiff.py (original)
+++ test-suite/trunk/utils/tdiff.py Fri Jan  4 00:14:43 2019
@@ -95,7 +95,7 @@ def determine_max_commandline_len():
     if sc_arg_max <= 0:
         return 10000 # wild guess
     env_len = 0
-    for key,val in os.environ.iteritems():
+    for key,val in os.environ.items():
         env_len += len(key) + len(val) + 10
     return sc_arg_max - env_len
 
@@ -140,12 +140,12 @@ def filelist(mode, target, cwd, config):
 
     if config.mode == 'sources':
         # Take leafs in the dependency tree
-        for target, depnode in tree.iteritems():
+        for target, depnode in tree.items():
             if len(depnode.inputs) == 0:
                 yield target
     else:
         # Take files ending in '.o'
-        for target, depnode in tree.iteritems():
+        for target, depnode in tree.items():
             if target.endswith(".o"):
                 # Determine .s/.stats ending used by -save-temps=obj or
                 # -save-stats=obj




More information about the llvm-commits mailing list