From 148fd55d8b7b061720792f3b786fd080a0a43e33 Mon Sep 17 00:00:00 2001 From: Huang Hao Date: Fri, 7 Sep 2012 10:32:49 +0800 Subject: [PATCH] Fix KeyError in ConfigMgr._get In function ConfigMrg._get() if NoOptionError occurs and section name is not in self.DEFAULTS, KeyError will be raised. Change-Id: Iaa08c032eae45b1cc95294a4c7975d0aedb10405 --- gitbuildsys/conf.py | 15 ++++++--------- tests/test_config.py | 6 ++++++ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/gitbuildsys/conf.py b/gitbuildsys/conf.py index ef77447..f186459 100644 --- a/gitbuildsys/conf.py +++ b/gitbuildsys/conf.py @@ -334,17 +334,14 @@ distconf = $build__distconf except NoOptionError: sect_found = True + if section in self.DEFAULTS and opt in self.DEFAULTS[section]: + return self.DEFAULTS[section][opt] + if not sect_found: - if section in self.DEFAULTS and opt in self.DEFAULTS[section]: - return self.DEFAULTS[section][opt] - else: - raise errors.ConfigError('no section %s' % section) + raise errors.ConfigError('no section %s' % section) else: - if opt in self.DEFAULTS[section]: - return self.DEFAULTS[section][opt] - else: - raise errors.ConfigError('no opt: %s in section %s' \ - % (opt, section)) + raise errors.ConfigError('no opt: %s in section %s' \ + % (opt, section)) def check_opt(self, opt, section='general'): if section in self.DEFAULTS and \ diff --git a/tests/test_config.py b/tests/test_config.py index 41cf3cf..79c3e82 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -114,6 +114,12 @@ class ConfigGettingTest(unittest.TestCase): self.get, 'not_exists_section', 'key') @Fixture(project='project1.ini') + def test_no_such_option(self): + '''test no such option''' + self.assertRaises(errors.ConfigError, + self.get, 'section', 'not_exists_option') + + @Fixture(project='project1.ini') def test_simple_get(self): '''get value when one config file provides''' self.assertEqual('projv2', self.get('section', 'proj_only_key')) -- 2.7.4