[gn] Paper over Py3 urllib2 incompatibility in gn/get.py
authorReid Kleckner <rnk@google.com>
Wed, 22 Jan 2020 22:31:12 +0000 (14:31 -0800)
committerReid Kleckner <rnk@google.com>
Tue, 11 Feb 2020 19:39:16 +0000 (11:39 -0800)
Tested with both Python 2.7 and Python 3.7.

Reviewed By: thakis

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

llvm/utils/gn/get.py

index c39649d..adb8eb7 100755 (executable)
@@ -5,7 +5,12 @@ from __future__ import print_function
 
 import io
 import os
-import urllib2
+try:
+    # In Python 3, we need the module urllib.reqest. In Python 2, this
+    # functionality was in the urllib2 module.
+    from urllib import request as urllib_request
+except ImportError:
+    import urllib2 as urllib_request
 import sys
 import zipfile
 
@@ -14,7 +19,7 @@ def download_and_unpack(url, output_dir, gn):
     """Download an archive from url and extract gn from it into output_dir."""
     print('downloading %s ...' % url, end='')
     sys.stdout.flush()
-    data = urllib2.urlopen(url).read()
+    data = urllib_request.urlopen(url).read()
     print(' done')
     zipfile.ZipFile(io.BytesIO(data)).extract(gn, path=output_dir)