r348126 - Portable Python script across Python version

Serge Guelton via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 3 04:11:21 PST 2018


Author: serge_sans_paille
Date: Mon Dec  3 04:11:21 2018
New Revision: 348126

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

Python2 supports the two following equivalent construct

	raise ExceptionType, exception_value
and
	raise ExceptionType(exception_value)

Only the later is supported by Python3.

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

Modified:
    cfe/trunk/tools/scan-view/share/ScanView.py
    cfe/trunk/utils/ABITest/ABITestGen.py
    cfe/trunk/utils/ABITest/Enumeration.py
    cfe/trunk/utils/ABITest/TypeGen.py
    cfe/trunk/utils/analyzer/SATestBuild.py
    cfe/trunk/utils/token-delta.py

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=348126&r1=348125&r2=348126&view=diff
==============================================================================
--- cfe/trunk/tools/scan-view/share/ScanView.py (original)
+++ cfe/trunk/tools/scan-view/share/ScanView.py Mon Dec  3 04:11:21 2018
@@ -102,9 +102,9 @@ class ReporterThread(threading.Thread):
             time.sleep(3)
             if self.server.options.debug:
                 print >>sys.stderr, "%s: SERVER: submission complete."%(sys.argv[0],)
-        except Reporter.ReportFailure,e:
+        except Reporter.ReportFailure as e:
             self.status = e.value
-        except Exception,e:
+        except Exception as e:
             s = StringIO.StringIO()
             import traceback
             print >>s,'<b>Unhandled Exception</b><br><pre>'
@@ -163,7 +163,7 @@ class ScanViewServer(BaseHTTPServer.HTTP
                 print >>sys.stderr, "%s: SERVER: waiting..." % (sys.argv[0],)
             try:
                 self.handle_request()
-            except OSError,e:
+            except OSError as e:
                 print 'OSError',e.errno
 
     def finish_request(self, request, client_address):
@@ -207,13 +207,13 @@ class ScanViewRequestHandler(SimpleHTTPS
     def do_HEAD(self):
         try:
             SimpleHTTPServer.SimpleHTTPRequestHandler.do_HEAD(self)
-        except Exception,e:
+        except Exception as e:
             self.handle_exception(e)
             
     def do_GET(self):
         try:
             SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
-        except Exception,e:
+        except Exception as e:
             self.handle_exception(e)
             
     def do_POST(self):
@@ -230,7 +230,7 @@ class ScanViewRequestHandler(SimpleHTTPS
             if f:
                 self.copyfile(f, self.wfile)
                 f.close()
-        except Exception,e:
+        except Exception as e:
             self.handle_exception(e)            
 
     def log_message(self, format, *args):
@@ -428,7 +428,7 @@ Submit</h3>
             data = self.load_crashes()
             # Don't allow empty reports.
             if not data:
-                raise ValueError, 'No crashes detected!'
+                raise ValueError('No crashes detected!')
             c = Context()
             c.title = 'clang static analyzer failures'
 
@@ -472,7 +472,7 @@ STDERR Summary
             # Check that this is a valid report.            
             path = posixpath.join(self.server.root, 'report-%s.html' % report)
             if not posixpath.exists(path):
-                raise ValueError, 'Invalid report ID'
+                raise ValueError('Invalid report ID')
             keys = self.load_report(report)
             c = Context()
             c.title = keys.get('DESC','clang error (unrecognized')
@@ -501,7 +501,7 @@ Line: %s
         # report is None is used for crashes
         try:
             c = self.get_report_context(report)
-        except ValueError, e:
+        except ValueError as e:
             return self.send_error(400, e.message)
 
         title = c.title

Modified: cfe/trunk/utils/ABITest/ABITestGen.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/utils/ABITest/ABITestGen.py?rev=348126&r1=348125&r2=348126&view=diff
==============================================================================
--- cfe/trunk/utils/ABITest/ABITestGen.py (original)
+++ cfe/trunk/utils/ABITest/ABITestGen.py Mon Dec  3 04:11:21 2018
@@ -250,7 +250,7 @@ class TypePrinter:
                     elements[i] = v
                     yield '{ %s }'%(', '.join(elements))
         else:
-            raise NotImplementedError,'Cannot make tests values of type: "%s"'%(t,)
+            raise NotImplementedError('Cannot make tests values of type: "%s"'%(t,))
 
     def printSizeOfType(self, prefix, name, t, output=None, indent=2):
         print >>output, '%*sprintf("%s: sizeof(%s) = %%ld\\n", (long)sizeof(%s));'%(indent, '', prefix, name, name) 
@@ -310,7 +310,7 @@ class TypePrinter:
                 else:
                     self.printValueOfType(prefix, '%s[%d]'%(name,i), t.elementType, output=output,indent=indent)                    
         else:
-            raise NotImplementedError,'Cannot print value of type: "%s"'%(t,)
+            raise NotImplementedError('Cannot print value of type: "%s"'%(t,))
 
     def checkTypeValues(self, nameLHS, nameRHS, t, output=None, indent=2):
         prefix = 'foo'
@@ -343,7 +343,7 @@ class TypePrinter:
                     self.checkTypeValues('%s[%d]'%(nameLHS,i), '%s[%d]'%(nameRHS,i), 
                                          t.elementType, output=output,indent=indent)                    
         else:
-            raise NotImplementedError,'Cannot print value of type: "%s"'%(t,)
+            raise NotImplementedError('Cannot print value of type: "%s"'%(t,))
 
 import sys
 
@@ -642,7 +642,7 @@ def main():
     def write(N):
         try:
             FT = ftg.get(N)
-        except RuntimeError,e:
+        except RuntimeError as e:
             if e.args[0]=='maximum recursion depth exceeded':
                 print >>sys.stderr,'WARNING: Skipped %d, recursion limit exceeded (bad arguments?)'%(N,)
                 return

Modified: cfe/trunk/utils/ABITest/Enumeration.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/utils/ABITest/Enumeration.py?rev=348126&r1=348125&r2=348126&view=diff
==============================================================================
--- cfe/trunk/utils/ABITest/Enumeration.py (original)
+++ cfe/trunk/utils/ABITest/Enumeration.py Mon Dec  3 04:11:21 2018
@@ -17,7 +17,7 @@ class Aleph0(int):
         return 1
 
     def __sub__(self, b):
-        raise ValueError,"Cannot subtract aleph0"
+        raise ValueError("Cannot subtract aleph0")
     __rsub__ = __sub__
 
     def __add__(self, b): 
@@ -86,9 +86,9 @@ def getNthPairBounded(N,W=aleph0,H=aleph
     Return the N-th pair such that 0 <= x < W and 0 <= y < H."""
 
     if W <= 0 or H <= 0:
-        raise ValueError,"Invalid bounds"
+        raise ValueError("Invalid bounds")
     elif N >= W*H:
-        raise ValueError,"Invalid input (out of bounds)"
+        raise ValueError("Invalid input (out of bounds)")
 
     # Simple case...
     if W is aleph0 and H is aleph0:
@@ -170,7 +170,7 @@ def getNthTuple(N, maxSize=aleph0, maxEl
     N -= 1
     if maxElement is not aleph0:
         if maxSize is aleph0:
-            raise NotImplementedError,'Max element size without max size unhandled'
+            raise NotImplementedError('Max element size without max size unhandled')
         bounds = [maxElement**i for i in range(1, maxSize+1)]
         S,M = getNthPairVariableBounds(N, bounds)
     else:
@@ -193,9 +193,9 @@ def getNthPairVariableBounds(N, bounds):
     bounds[x]."""
 
     if not bounds:
-        raise ValueError,"Invalid bounds"
+        raise ValueError("Invalid bounds")
     if not (0 <= N < sum(bounds)):
-        raise ValueError,"Invalid input (out of bounds)"
+        raise ValueError("Invalid input (out of bounds)")
 
     level = 0
     active = range(len(bounds))
@@ -216,7 +216,7 @@ def getNthPairVariableBounds(N, bounds):
             N -= levelSize
             prevLevel = level
     else:
-        raise RuntimError,"Unexpected loop completion"
+        raise RuntimError("Unexpected loop completion")
 
 def getNthPairVariableBoundsChecked(N, bounds, GNVP=getNthPairVariableBounds):
     x,y = GNVP(N,bounds)

Modified: cfe/trunk/utils/ABITest/TypeGen.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/utils/ABITest/TypeGen.py?rev=348126&r1=348125&r2=348126&view=diff
==============================================================================
--- cfe/trunk/utils/ABITest/TypeGen.py (original)
+++ cfe/trunk/utils/ABITest/TypeGen.py Mon Dec  3 04:11:21 2018
@@ -435,7 +435,7 @@ class AnyTypeGenerator(TypeGenerator):
             if (self._cardinality is aleph0) or prev==self._cardinality:
                 break
         else:
-            raise RuntimeError,"Infinite loop in setting cardinality"
+            raise RuntimeError("Infinite loop in setting cardinality")
 
     def generateType(self, N):
         index,M = getNthPairVariableBounds(N, self.bounds)
@@ -466,7 +466,7 @@ def test():
         if i == atg.cardinality:
             try:
                 atg.get(i)
-                raise RuntimeError,"Cardinality was wrong"
+                raise RuntimeError("Cardinality was wrong")
             except AssertionError:
                 break
         print '%4d: %s'%(i, atg.get(i))

Modified: cfe/trunk/utils/analyzer/SATestBuild.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/utils/analyzer/SATestBuild.py?rev=348126&r1=348125&r2=348126&view=diff
==============================================================================
--- cfe/trunk/utils/analyzer/SATestBuild.py (original)
+++ cfe/trunk/utils/analyzer/SATestBuild.py Mon Dec  3 04:11:21 2018
@@ -382,7 +382,7 @@ def runAnalyzePreprocessed(Args, Dir, SB
             check_call(Command, cwd=Dir, stderr=LogFile,
                        stdout=LogFile,
                        shell=True)
-        except CalledProcessError, e:
+        except CalledProcessError as e:
             Local.stderr.write("Error: Analyzes of %s failed. "
                                "See %s for details."
                                "Error code %d.\n" % (

Modified: cfe/trunk/utils/token-delta.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/utils/token-delta.py?rev=348126&r1=348125&r2=348126&view=diff
==============================================================================
--- cfe/trunk/utils/token-delta.py (original)
+++ cfe/trunk/utils/token-delta.py Mon Dec  3 04:11:21 2018
@@ -37,7 +37,7 @@ class DeltaAlgorithm(object):
         # O(N^2) case unless user requests it.
         if not force:
             if not self.getTestResult(changes):
-                raise ValueError,'Initial test passed to delta fails.'
+                raise ValueError('Initial test passed to delta fails.')
 
         # Check empty set first to quickly find poor test functions.
         if self.getTestResult(set()):




More information about the cfe-commits mailing list