[Piglit] [PATCH 12/12] core.py: rewrie loadTestProfile to remove execfile()

Dylan Baker baker.dylan.c at gmail.com
Mon Dec 23 16:51:49 PST 2013


This patch removes execfile, replaceing it with import instead.

This isn't a prefect solution, but it provides feature parity with the
execfile() aproach, while being python3 safe.
---
 framework/core.py | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/framework/core.py b/framework/core.py
index cd5c11d..6dacc7c 100644
--- a/framework/core.py
+++ b/framework/core.py
@@ -38,6 +38,7 @@ from textwrap import dedent
 from threads import synchronized_self
 import threading
 import multiprocessing
+import importlib
 try:
     import simplejson as json
 except ImportError:
@@ -632,13 +633,22 @@ class TestProfile:
 
 
 def loadTestProfile(filename):
-    ns = {'__file__': filename}
+    """ Load a python module and return it's profile attribute
+
+    All of the python test files provide a profile attribute which is a
+    TestProfile instance. This loads that module and returns it or raises an
+    error.
+
+    """
+    mod = importlib.import_module('tests.{0}'.format(
+        os.path.splitext(os.path.basename(filename))[0]))
+
     try:
-        execfile(filename, ns)
-    except:
-        traceback.print_exc()
-        raise Exception('Could not read tests profile')
-    return ns['profile']
+        return mod.profile
+    except AttributeError:
+        print("Error: There is not profile attribute in module {0}."
+              "Did you specify the right file?".format(filename))
+        sys.exit(1)
 
 
 def merge_test_profiles(profiles):
-- 
1.8.5.2



More information about the Piglit mailing list