r348184 - Portable Python script across Python version

Serge Guelton via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 3 12:26:51 PST 2018


Author: serge_sans_paille
Date: Mon Dec  3 12:26:51 2018
New Revision: 348184

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

Workaround naming and hierarchy changes in BaseHTTPServer and SimpleHTTPServer module.

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

Modified:
    cfe/trunk/tools/scan-view/share/ScanView.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=348184&r1=348183&r2=348184&view=diff
==============================================================================
--- cfe/trunk/tools/scan-view/share/ScanView.py (original)
+++ cfe/trunk/tools/scan-view/share/ScanView.py Mon Dec  3 12:26:51 2018
@@ -1,5 +1,8 @@
-import BaseHTTPServer
-import SimpleHTTPServer
+try:
+    from http.server import HTTPServer, SimpleHTTPRequestHandler
+except ImportError:
+    from BaseHTTPServer import HTTPServer
+    from SimpleHTTPServer import SimpleHTTPRequestHandler
 import os
 import sys
 import urllib, urlparse
@@ -112,9 +115,9 @@ class ReporterThread(threading.Thread):
             print >>s,'</pre>'
             self.status = s.getvalue()
 
-class ScanViewServer(BaseHTTPServer.HTTPServer):
+class ScanViewServer(HTTPServer):
     def __init__(self, address, handler, root, reporters, options):
-        BaseHTTPServer.HTTPServer.__init__(self, address, handler)
+        HTTPServer.__init__(self, address, handler)
         self.root = root
         self.reporters = reporters
         self.options = options        
@@ -170,7 +173,7 @@ class ScanViewServer(BaseHTTPServer.HTTP
         if self.options.autoReload:
             import ScanView
             self.RequestHandlerClass = reload(ScanView).ScanViewRequestHandler
-        BaseHTTPServer.HTTPServer.finish_request(self, request, client_address)
+        HTTPServer.finish_request(self, request, client_address)
 
     def handle_error(self, request, client_address):
         # Ignore socket errors
@@ -179,7 +182,7 @@ class ScanViewServer(BaseHTTPServer.HTTP
             if self.options.debug > 1:
                 print >>sys.stderr, "%s: SERVER: ignored socket error." % (sys.argv[0],)
             return
-        BaseHTTPServer.HTTPServer.handle_error(self, request, client_address)
+        HTTPServer.handle_error(self, request, client_address)
 
 # Borrowed from Quixote, with simplifications.
 def parse_query(qs, fields=None):
@@ -200,19 +203,19 @@ def parse_query(qs, fields=None):
             item.append(value)
     return fields
 
-class ScanViewRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
+class ScanViewRequestHandler(SimpleHTTPRequestHandler):
     server_version = "ScanViewServer/" + __version__
     dynamic_mtime = time.time()
 
     def do_HEAD(self):
         try:
-            SimpleHTTPServer.SimpleHTTPRequestHandler.do_HEAD(self)
+            SimpleHTTPRequestHandler.do_HEAD(self)
         except Exception as e:
             self.handle_exception(e)
             
     def do_GET(self):
         try:
-            SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
+            SimpleHTTPRequestHandler.do_GET(self)
         except Exception as e:
             self.handle_exception(e)
             




More information about the cfe-commits mailing list