python: Update use of exec to work with Python 3

Python 3 uses 'exec(code, globals)' instead of 'exec code in
globals'. Switch to the newer syntax since it is supported by Python
2.7. Also, move check_tracing out of main to work around a bug in
Python 2.7.

Change-Id: I6d390160f58783e1b038a572b64cdf3ff09535fa
Signed-off-by: Andreas Sandberg <andreas.sandberg@arm.com>
Reviewed-on: https://gem5-review.googlesource.com/c/15986
Reviewed-by: Jason Lowe-Power <jason@lowepower.com>
diff --git a/src/python/importer.py b/src/python/importer.py
index c54fb49..224ab3b 100644
--- a/src/python/importer.py
+++ b/src/python/importer.py
@@ -77,7 +77,7 @@
                 mod.__package__ = fullname.rpartition('.')[0]
             mod.__file__ = srcfile
 
-            exec code in mod.__dict__
+            exec(code, mod.__dict__)
         except Exception:
             del sys.modules[fullname]
             raise
diff --git a/src/python/m5/internal/params.py b/src/python/m5/internal/params.py
index ef94ad4..400e780 100644
--- a/src/python/m5/internal/params.py
+++ b/src/python/m5/internal/params.py
@@ -43,4 +43,4 @@
 
 for name, module in inspect.getmembers(_m5):
     if name.startswith('param_') or name.startswith('enum_'):
-        exec "from _m5.%s import *" % name
+        exec("from _m5.%s import *" % name)
diff --git a/src/python/m5/main.py b/src/python/m5/main.py
index e4619c0..8a259f3 100644
--- a/src/python/m5/main.py
+++ b/src/python/m5/main.py
@@ -148,7 +148,7 @@
     options_file = config.get('options.py')
     if options_file:
         scope = { 'options' : options }
-        execfile(options_file, scope)
+        exec(compile(open(options_file).read(), options_file, 'exec'), scope)
 
     arguments = options.parse_args()
     return options,arguments
@@ -191,6 +191,13 @@
         # isn't available.
         code.InteractiveConsole(scope).interact(banner)
 
+
+def _check_tracing():
+    if defines.TRACING_ON:
+        return
+
+    fatal("Tracing is not enabled.  Compile with TRACING_ON")
+
 def main(*args):
     import m5
 
@@ -213,12 +220,6 @@
 
     m5.options = options
 
-    def check_tracing():
-        if defines.TRACING_ON:
-            return
-
-        fatal("Tracing is not enabled.  Compile with TRACING_ON")
-
     # Set the main event queue for the main thread.
     event.mainq = event.getEventQueue(0)
     event.setEventQueue(event.mainq)
@@ -279,7 +280,7 @@
 
     if options.debug_help:
         done = True
-        check_tracing()
+        _check_tracing()
         debug.help()
 
     if options.list_sim_objects:
@@ -366,7 +367,7 @@
         debug.schedBreak(int(when))
 
     if options.debug_flags:
-        check_tracing()
+        _check_tracing()
 
         on_flags = []
         off_flags = []
@@ -386,21 +387,21 @@
                 debug.flags[flag].enable()
 
     if options.debug_start:
-        check_tracing()
+        _check_tracing()
         e = event.create(trace.enable, event.Event.Debug_Enable_Pri)
         event.mainq.schedule(e, options.debug_start)
     else:
         trace.enable()
 
     if options.debug_end:
-        check_tracing()
+        _check_tracing()
         e = event.create(trace.disable, event.Event.Debug_Enable_Pri)
         event.mainq.schedule(e, options.debug_end)
 
     trace.output(options.debug_file)
 
     for ignore in options.debug_ignore:
-        check_tracing()
+        _check_tracing()
         trace.ignore(ignore)
 
     sys.argv = arguments
@@ -432,7 +433,7 @@
                 t = t.tb_next
                 pdb.interaction(t.tb_frame,t)
     else:
-        exec filecode in scope
+        exec(filecode, scope)
 
     # once the script is done
     if options.interactive:
diff --git a/src/python/m5/objects/__init__.py b/src/python/m5/objects/__init__.py
index 29402c5..8186c52 100644
--- a/src/python/m5/objects/__init__.py
+++ b/src/python/m5/objects/__init__.py
@@ -36,4 +36,4 @@
 
 for module in modules.iterkeys():
     if module.startswith('m5.objects.'):
-        exec "from %s import *" % module
+        exec("from %s import *" % module)
diff --git a/src/python/m5/util/jobfile.py b/src/python/m5/util/jobfile.py
index ad5b5ff..613289a 100644
--- a/src/python/m5/util/jobfile.py
+++ b/src/python/m5/util/jobfile.py
@@ -417,7 +417,7 @@
             raise AttributeError("Could not find file '%s'" % jobfile)
 
     data = {}
-    execfile(filename, data)
+    exec(compile(open(filename).read(), filename, 'exec'), data)
     if 'conf' not in data:
         raise ImportError('cannot import name conf from %s' % jobfile)
     return data['conf']