[Piglit] [PATCH] framework/core: add fallback to PiglitConfig.safe_get

Dylan Baker baker.dylan.c at gmail.com
Tue Sep 22 13:04:46 PDT 2015


This is in keeping with the way that piglitconfig.get() works in python3
(although there are some corner case differences since in python3 it
uses keyword only arguments).

Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---

This would actually be pretty useful in a number of cases in piglit
currently, and a few patches on the mailing list.

 framework/core.py             | 6 +++---
 framework/tests/core_tests.py | 4 ++++
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/framework/core.py b/framework/core.py
index 63cb911..dbd6895 100644
--- a/framework/core.py
+++ b/framework/core.py
@@ -58,7 +58,7 @@ class PiglitConfig(ConfigParser.SafeConfigParser):
         ConfigParser.SafeConfigParser.readfp(self, fp, filename)
         self.filename = os.path.abspath(filename or fp.name)
 
-    def safe_get(self, *args, **kwargs):
+    def safe_get(self, section, option, fallback=None, **kwargs):
         """A version of self.get that doesn't raise NoSectionError or
         NoOptionError.
 
@@ -67,9 +67,9 @@ class PiglitConfig(ConfigParser.SafeConfigParser):
 
         """
         try:
-            return self.get(*args, **kwargs)
+            return self.get(section, option, **kwargs)
         except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
-            return None
+            return fallback
 
     def required_get(self, section, option, **kwargs):
         """A version fo self.get that raises PiglitFatalError.
diff --git a/framework/tests/core_tests.py b/framework/tests/core_tests.py
index af09339..9be9c57 100644
--- a/framework/tests/core_tests.py
+++ b/framework/tests/core_tests.py
@@ -267,3 +267,7 @@ class TestPiglitConfig(object):
         """core.PiglitConfig: required_get raises PiglitFatalError if the section is missing
         """
         self.conf.required_get('invalid', 'invalid')
+
+    def test_safe_get_fallback(self):
+        """core.PiglitConfig: safe_get returns the value of fallback when the section or option is missing"""
+        nt.eq_(self.conf.safe_get('invalid', 'invalid', fallback='foo'), 'foo')
-- 
2.5.3



More information about the Piglit mailing list