python: Add fallbacks for packages that have been renamed
authorAndreas Sandberg <andreas.sandberg@arm.com>
Sat, 26 Jan 2019 09:54:00 +0000 (09:54 +0000)
committerAndreas Sandberg <andreas.sandberg@arm.com>
Fri, 22 Feb 2019 15:38:27 +0000 (15:38 +0000)
Python 3 has restructured some packages. Specifically, __builtin__ has
been renamed to builtins and urlparse has been included in urllib.

Change-Id: I81f8f3942471db1043006a36abbad6e5a49e0a43
Signed-off-by: Andreas Sandberg <andreas.sandberg@arm.com>
Reviewed-on: https://gem5-review.googlesource.com/c/15994
Reviewed-by: Juha Jäykkä <juha.jaykka@arm.com>
src/python/m5/stats/__init__.py
src/python/m5/util/code_formatter.py

index acb62f13e4e0941b98822fa60314a491ad5cf9dd..019c7ebb8246301e4f335843c0cc08174a4c2b02 100644 (file)
@@ -71,7 +71,11 @@ def _url_factory(func):
 
     @wraps(func)
     def wrapper(url):
-        from urlparse import parse_qs
+        try:
+            from urllib.parse import parse_qs
+        except ImportError:
+            # Python 2 fallback
+            from urlparse import parse_qs
         from ast import literal_eval
 
         qs = parse_qs(url.query, keep_blank_values=True)
@@ -135,7 +139,11 @@ def addStatVisitor(url):
 
     """
 
-    from urlparse import urlsplit
+    try:
+        from urllib.parse import urlsplit
+    except ImportError:
+        # Python 2 fallback
+        from urlparse import urlsplit
 
     parsed = urlsplit(url)
 
index c845c90212de806c231d5669204ca7b4d02c5d5b..21bbcd7a88e664e65b7c8b6ea9cb366e156e00ee 100644 (file)
 
 from __future__ import print_function
 
-import __builtin__
+try:
+    import builtins
+except ImportError:
+    # Python 2 fallback
+    import __builtin__ as builtins
 import inspect
 import os
 import re
@@ -66,8 +70,8 @@ class lookup(object):
         if self.formatter.globals and item in self.frame.f_globals:
             return self.frame.f_globals[item]
 
-        if item in __builtin__.__dict__:
-            return __builtin__.__dict__[item]
+        if item in builtins.__dict__:
+            return builtins.__dict__[item]
 
         try:
             item = int(item)