r349501 - Portable Python script across Python version

Serge Guelton via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 18 08:04:21 PST 2018


Author: serge_sans_paille
Date: Tue Dec 18 08:04:21 2018
New Revision: 349501

URL: http://llvm.org/viewvc/llvm-project?rev=349501&view=rev
Log:
Portable Python script across Python version

In Python3, dict.items, dict.keys, dict.values, zip, map and filter no longer return lists, they create generator instead.

The portability patch consists in forcing an extra `list` call if the result is actually used as a list.
`map` are replaced by list comprehension and `filter` by filtered list comprehension.

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

Modified:
    cfe/trunk/bindings/python/examples/cindex/cindex-dump.py
    cfe/trunk/tools/scan-view/share/Reporter.py
    cfe/trunk/tools/scan-view/share/ScanView.py
    cfe/trunk/utils/ABITest/ABITestGen.py
    cfe/trunk/utils/ABITest/TypeGen.py
    cfe/trunk/utils/analyzer/CmpRuns.py
    cfe/trunk/utils/analyzer/SATestBuild.py
    cfe/trunk/utils/check_cfc/check_cfc.py
    cfe/trunk/utils/check_cfc/obj_diff.py
    cfe/trunk/utils/perf-training/perf-helper.py
    cfe/trunk/www/builtins.py

Modified: cfe/trunk/bindings/python/examples/cindex/cindex-dump.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/examples/cindex/cindex-dump.py?rev=349501&r1=349500&r2=349501&view=diff
==============================================================================
--- cfe/trunk/bindings/python/examples/cindex/cindex-dump.py (original)
+++ cfe/trunk/bindings/python/examples/cindex/cindex-dump.py Tue Dec 18 08:04:21 2018
@@ -79,7 +79,7 @@ def main():
     if not tu:
         parser.error("unable to load input")
 
-    pprint(('diags', map(get_diag_info, tu.diagnostics)))
+    pprint(('diags', [get_diag_info(d) for d in  tu.diagnostics]))
     pprint(('nodes', get_info(tu.cursor)))
 
 if __name__ == '__main__':

Modified: cfe/trunk/tools/scan-view/share/Reporter.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/scan-view/share/Reporter.py?rev=349501&r1=349500&r2=349501&view=diff
==============================================================================
--- cfe/trunk/tools/scan-view/share/Reporter.py (original)
+++ cfe/trunk/tools/scan-view/share/Reporter.py Tue Dec 18 08:04:21 2018
@@ -80,7 +80,7 @@ class EmailReporter(object):
         return 'Email'
 
     def getParameters(self):
-        return map(lambda x:TextParameter(x),['To', 'From', 'SMTP Server', 'SMTP Port'])
+        return [TextParameter(x) for x in ['To', 'From', 'SMTP Server', 'SMTP Port']]
 
     # Lifted from python email module examples.
     def attachFile(self, outer, path):
@@ -148,7 +148,7 @@ class BugzillaReporter(object):
         return 'Bugzilla'
     
     def getParameters(self):
-        return map(lambda x:TextParameter(x),['URL','Product'])
+        return [TextParameter(x) for x in ['URL','Product']]
 
     def fileReport(self, report, parameters):
         raise NotImplementedError
@@ -211,7 +211,7 @@ class RadarReporter(object):
 
         script = os.path.join(os.path.dirname(__file__),'../share/scan-view/FileRadar.scpt')
         args = ['osascript', script, component, componentVersion, classification, personID, report.title,
-                report.description, diagnosis, config] + map(os.path.abspath, report.files)
+                report.description, diagnosis, config] + [os.path.abspath(f) for f in report.files]
 #        print >>sys.stderr, args
         try:
           p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Modified: cfe/trunk/tools/scan-view/share/ScanView.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/scan-view/share/ScanView.py?rev=349501&r1=349500&r2=349501&view=diff
==============================================================================
--- cfe/trunk/tools/scan-view/share/ScanView.py (original)
+++ cfe/trunk/tools/scan-view/share/ScanView.py Tue Dec 18 08:04:21 2018
@@ -192,7 +192,7 @@ class ScanViewServer(HTTPServer):
 def parse_query(qs, fields=None):
     if fields is None:
         fields = {}
-    for chunk in filter(None, qs.split('&')):
+    for chunk in (_f for _f in qs.split('&') if _f):
         if '=' not in chunk:
             name = chunk
             value = ''

Modified: cfe/trunk/utils/ABITest/ABITestGen.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/utils/ABITest/ABITestGen.py?rev=349501&r1=349500&r2=349501&view=diff
==============================================================================
--- cfe/trunk/utils/ABITest/ABITestGen.py (original)
+++ cfe/trunk/utils/ABITest/ABITestGen.py Tue Dec 18 08:04:21 2018
@@ -148,7 +148,7 @@ class TypePrinter(object):
                 retvalTests = None
             else:
                 retvalTests = self.getTestValuesArray(FT.returnType)
-            tests = map(self.getTestValuesArray, FT.argTypes)
+            tests = [self.getTestValuesArray(ty) for ty in FT.argTypes]
             print('void test_%s(void) {'%(fnName,), file=self.outputTests)
 
             if retvalTests is not None:
@@ -231,10 +231,10 @@ class TypePrinter(object):
                     yield '{ %s }' % v
                 return
 
-            fieldValues = map(list, map(self.getTestValues, nonPadding))
+            fieldValues = [list(v) for v in map(self.getTestValues, nonPadding)]
             for i,values in enumerate(fieldValues):
                 for v in values:
-                    elements = map(random.choice,fieldValues)
+                    elements = [random.choice(fv) for fv in fieldValues]
                     elements[i] = v
                     yield '{ %s }'%(', '.join(elements))
 

Modified: cfe/trunk/utils/ABITest/TypeGen.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/utils/ABITest/TypeGen.py?rev=349501&r1=349500&r2=349501&view=diff
==============================================================================
--- cfe/trunk/utils/ABITest/TypeGen.py (original)
+++ cfe/trunk/utils/ABITest/TypeGen.py Tue Dec 18 08:04:21 2018
@@ -110,7 +110,7 @@ class RecordType(Type):
                                                t.getBitFieldSize())
             else:
                 return '%s field%d;'%(printer.getTypeName(t),i)
-        fields = map(getField, enumerate(self.fields))
+        fields = [getField(f) for f in enumerate(self.fields)]
         # Name the struct for more readable LLVM IR.
         return 'typedef %s %s { %s } %s;'%(('struct','union')[self.isUnion],
                                            name, ' '.join(fields), name)
@@ -372,7 +372,7 @@ class RecordTypeGenerator(TypeGenerator)
         isUnion,I = False,N
         if self.useUnion:
             isUnion,I = (I&1),I>>1
-        fields = map(self.typeGen.get,getNthTuple(I,self.maxSize,self.typeGen.cardinality))
+        fields = [self.typeGen.get(f) for f in getNthTuple(I,self.maxSize,self.typeGen.cardinality)]
         return RecordType(N, isUnion, fields)
 
 class FunctionTypeGenerator(TypeGenerator):
@@ -405,7 +405,7 @@ class FunctionTypeGenerator(TypeGenerato
         else:
             retTy = None
             argIndices = getNthTuple(N, self.maxSize, self.typeGen.cardinality)
-        args = map(self.typeGen.get, argIndices)
+        args = [self.typeGen.get(i) for i in argIndices]
         return FunctionType(N, retTy, args)
 
 class AnyTypeGenerator(TypeGenerator):

Modified: cfe/trunk/utils/analyzer/CmpRuns.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/utils/analyzer/CmpRuns.py?rev=349501&r1=349500&r2=349501&view=diff
==============================================================================
--- cfe/trunk/utils/analyzer/CmpRuns.py (original)
+++ cfe/trunk/utils/analyzer/CmpRuns.py Tue Dec 18 08:04:21 2018
@@ -298,10 +298,10 @@ def deriveStats(results):
             combined_data['PathsLength'].append(diagnostic.getPathLength())
 
     for stat in results.stats:
-        for key, value in stat.iteritems():
+        for key, value in stat.items():
             combined_data[key].append(value)
     combined_stats = {}
-    for key, values in combined_data.iteritems():
+    for key, values in combined_data.items():
         combined_stats[str(key)] = {
             "max": max(values),
             "min": min(values),

Modified: cfe/trunk/utils/analyzer/SATestBuild.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/utils/analyzer/SATestBuild.py?rev=349501&r1=349500&r2=349501&view=diff
==============================================================================
--- cfe/trunk/utils/analyzer/SATestBuild.py (original)
+++ cfe/trunk/utils/analyzer/SATestBuild.py Tue Dec 18 08:04:21 2018
@@ -583,8 +583,7 @@ def runCmpResults(Dir, Strictness=0):
 
     # Iterate and find the differences.
     NumDiffs = 0
-    PairList = zip(RefList, NewList)
-    for P in PairList:
+    for P in zip(RefList, NewList):
         RefDir = P[0]
         NewDir = P[1]
 

Modified: cfe/trunk/utils/check_cfc/check_cfc.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/utils/check_cfc/check_cfc.py?rev=349501&r1=349500&r2=349501&view=diff
==============================================================================
--- cfe/trunk/utils/check_cfc/check_cfc.py (original)
+++ cfe/trunk/utils/check_cfc/check_cfc.py Tue Dec 18 08:04:21 2018
@@ -98,8 +98,8 @@ def remove_dir_from_path(path_var, direc
     PATH"""
     pathlist = path_var.split(os.pathsep)
     norm_directory = os.path.normpath(os.path.normcase(directory))
-    pathlist = filter(lambda x: os.path.normpath(
-        os.path.normcase(x)) != norm_directory, pathlist)
+    pathlist = [x for x in pathlist if os.path.normpath(
+        os.path.normcase(x)) != norm_directory]
     return os.pathsep.join(pathlist)
 
 def path_without_wrapper():

Modified: cfe/trunk/utils/check_cfc/obj_diff.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/utils/check_cfc/obj_diff.py?rev=349501&r1=349500&r2=349501&view=diff
==============================================================================
--- cfe/trunk/utils/check_cfc/obj_diff.py (original)
+++ cfe/trunk/utils/check_cfc/obj_diff.py Tue Dec 18 08:04:21 2018
@@ -25,7 +25,7 @@ def disassemble(objfile):
     if p.returncode or err:
         print("Disassemble failed: {}".format(objfile))
         sys.exit(1)
-    return filter(keep_line, out.split(os.linesep))
+    return [line for line in out.split(os.linesep) if keep_line(line)]
 
 def dump_debug(objfile):
     """Dump all of the debug info from a file."""
@@ -34,7 +34,7 @@ def dump_debug(objfile):
     if p.returncode or err:
         print("Dump debug failed: {}".format(objfile))
         sys.exit(1)
-    return filter(keep_line, out.split(os.linesep))
+    return [line for line in out.split(os.linesep) if keep_line(line)]
 
 def first_diff(a, b, fromfile, tofile):
     """Returns the first few lines of a difference, if there is one.  Python

Modified: cfe/trunk/utils/perf-training/perf-helper.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/utils/perf-training/perf-helper.py?rev=349501&r1=349500&r2=349501&view=diff
==============================================================================
--- cfe/trunk/utils/perf-training/perf-helper.py (original)
+++ cfe/trunk/utils/perf-training/perf-helper.py Tue Dec 18 08:04:21 2018
@@ -295,8 +295,8 @@ def form_by_frequency(symbol_lists):
     for a in symbols:
       counts[a] = counts.get(a,0) + 1
 
-  by_count = counts.items()
-  by_count.sort(key = lambda (_,n): -n)
+  by_count = list(counts.items())
+  by_count.sort(key = lambda __n: -__n[1])
   return [s for s,n in by_count]
  
 def form_by_random(symbol_lists):
@@ -333,7 +333,7 @@ def genOrderFile(args):
     help="write a list of the unordered symbols to PATH (requires --binary)",
     default=None, metavar="PATH")
   parser.add_argument("--method", dest="method",
-    help="order file generation method to use", choices=methods.keys(),
+    help="order file generation method to use", choices=list(methods.keys()),
     default='call_order')
   opts = parser.parse_args(args)
 

Modified: cfe/trunk/www/builtins.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/www/builtins.py?rev=349501&r1=349500&r2=349501&view=diff
==============================================================================
--- cfe/trunk/www/builtins.py (original)
+++ cfe/trunk/www/builtins.py Tue Dec 18 08:04:21 2018
@@ -151,7 +151,7 @@ def report_cant(builtin):
   sys.stderr.write("%s:%d: x86 builtin %s used, too many replacements\n" % (fileinput.filename(), fileinput.filelineno(), builtin))
 
 for line in fileinput.input(inplace=1):
-  for builtin, repl in repl_map.iteritems():
+  for builtin, repl in repl_map.items():
     if builtin in line:
       line = line.replace(builtin, repl)
       report_repl(builtin, repl)




More information about the cfe-commits mailing list