[Piglit] [Patch v2 13/13] core.py: rewrite loadTestProfile to remove execfile()
Dylan Baker
baker.dylan.c at gmail.com
Wed Jan 8 16:25:11 PST 2014
This patch removes execfile, replacing it with import instead.
This isn't a prefect solution, but it provides feature parity with the
execfile() approach, while being python3 safe.
With this patch a user can require a test profile as tests/<your
profile>.* since, the code actually drops the '.' and everything after
it, so scripts that wrap piglit and use ie quick.tests will continue to
work. It also works without a '.', so the following comand examples will
work after this patch:
tests/quick.tests
tests/quick.py
tests/quick
V2: - add explanation of new acceptable names for tests.
Signed-off-by: Dylan Baker <baker.dylan.c at gmail.com>
---
framework/core.py | 22 ++++++++++++++++------
1 file changed, 16 insertions(+), 6 deletions(-)
diff --git a/framework/core.py b/framework/core.py
index b06c841..ee650e1 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:
@@ -636,13 +637,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