r349627 - Portable Python script across Python version

Serge Guelton via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 19 05:46:13 PST 2018


Author: serge_sans_paille
Date: Wed Dec 19 05:46:13 2018
New Revision: 349627

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

urllib2 as been renamed into urllib and the library layout has changed.
Workaround that in a consistent manner.

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

Modified:
    cfe/trunk/docs/tools/dump_ast_matchers.py
    cfe/trunk/docs/tools/dump_format_style.py
    cfe/trunk/tools/scan-view/bin/scan-view
    cfe/trunk/tools/scan-view/share/ScanView.py

Modified: cfe/trunk/docs/tools/dump_ast_matchers.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/docs/tools/dump_ast_matchers.py?rev=349627&r1=349626&r2=349627&view=diff
==============================================================================
--- cfe/trunk/docs/tools/dump_ast_matchers.py (original)
+++ cfe/trunk/docs/tools/dump_ast_matchers.py Wed Dec 19 05:46:13 2018
@@ -5,7 +5,10 @@
 
 import collections
 import re
-import urllib2
+try:
+    from urllib.request import urlopen
+except ImportError:
+    from urllib2 import urlopen
 
 MATCHERS_FILE = '../../include/clang/ASTMatchers/ASTMatchers.h'
 
@@ -42,7 +45,7 @@ def esc(text):
     if url not in doxygen_probes:
       try:
         print('Probing %s...' % url)
-        urllib2.urlopen(url)
+        urlopen(url)
         doxygen_probes[url] = True
       except:
         doxygen_probes[url] = False

Modified: cfe/trunk/docs/tools/dump_format_style.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/docs/tools/dump_format_style.py?rev=349627&r1=349626&r2=349627&view=diff
==============================================================================
--- cfe/trunk/docs/tools/dump_format_style.py (original)
+++ cfe/trunk/docs/tools/dump_format_style.py Wed Dec 19 05:46:13 2018
@@ -6,7 +6,6 @@
 import collections
 import os
 import re
-import urllib2
 
 CLANG_DIR = os.path.join(os.path.dirname(__file__), '../..')
 FORMAT_STYLE_FILE = os.path.join(CLANG_DIR, 'include/clang/Format/Format.h')

Modified: cfe/trunk/tools/scan-view/bin/scan-view
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/scan-view/bin/scan-view?rev=349627&r1=349626&r2=349627&view=diff
==============================================================================
--- cfe/trunk/tools/scan-view/bin/scan-view (original)
+++ cfe/trunk/tools/scan-view/bin/scan-view Wed Dec 19 05:46:13 2018
@@ -11,7 +11,10 @@ import os
 import posixpath
 import threading
 import time
-import urllib
+try:
+    from urllib.request import urlopen
+except ImportError:
+    from urllib2 import urlopen
 import webbrowser
 
 # How long to wait for server to start.
@@ -29,7 +32,7 @@ kMaxPortsToTry = 100
 
 def url_is_up(url):
     try:
-        o = urllib.urlopen(url)
+        o = urlopen(url)
     except IOError:
         return False
     o.close()
@@ -37,7 +40,6 @@ def url_is_up(url):
 
 
 def start_browser(port, options):
-    import urllib
     import webbrowser
 
     url = 'http://%s:%d' % (options.host, port)

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=349627&r1=349626&r2=349627&view=diff
==============================================================================
--- cfe/trunk/tools/scan-view/share/ScanView.py (original)
+++ cfe/trunk/tools/scan-view/share/ScanView.py Wed Dec 19 05:46:13 2018
@@ -6,7 +6,12 @@ except ImportError:
     from SimpleHTTPServer import SimpleHTTPRequestHandler
 import os
 import sys
-import urllib, urlparse
+try:
+    from urlparse import urlparse
+    from urllib import unquote
+except ImportError:
+    from urllib.parse import urlparse, unquote
+
 import posixpath
 import StringIO
 import re
@@ -198,8 +203,8 @@ def parse_query(qs, fields=None):
             value = ''
         else:
             name, value = chunk.split('=', 1)
-        name = urllib.unquote(name.replace('+', ' '))
-        value = urllib.unquote(value.replace('+', ' '))
+        name = unquote(name.replace('+', ' '))
+        value = unquote(value.replace('+', ' '))
         item = fields.get(name)
         if item is None:
             fields[name] = [value]
@@ -654,9 +659,9 @@ File Bug</h3>
             fields = {}
         self.fields = fields
 
-        o = urlparse.urlparse(self.path)
+        o = urlparse(self.path)
         self.fields = parse_query(o.query, fields)
-        path = posixpath.normpath(urllib.unquote(o.path))
+        path = posixpath.normpath(unquote(o.path))
 
         # Split the components and strip the root prefix.
         components = path.split('/')[1:]




More information about the cfe-commits mailing list