From 2f0fd0fa4fd0424cee5718bccc826c2ed7220bb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Langa?= Date: Sat, 4 Dec 2010 17:48:18 +0000 Subject: [PATCH] configparser: mapping protocol access get() handles configparser-specific arguments as well --- Lib/configparser.py | 22 +++++++++++++------ Lib/test/test_cfgparser.py | 43 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/Lib/configparser.py b/Lib/configparser.py index e92d7fa3f5e..b5623cdbad9 100644 --- a/Lib/configparser.py +++ b/Lib/configparser.py @@ -1195,12 +1195,6 @@ class SectionProxy(MutableMapping): """Creates a view on a section of the specified `name` in `parser`.""" self._parser = parser self._name = name - self.getint = functools.partial(self._parser.getint, - self._name) - self.getfloat = functools.partial(self._parser.getfloat, - self._name) - self.getboolean = functools.partial(self._parser.getboolean, - self._name) def __repr__(self): return ''.format(self._name) @@ -1231,6 +1225,22 @@ class SectionProxy(MutableMapping): # XXX does not break when underlying container state changed return self._parser.options(self._name).__iter__() + def get(self, option, fallback=None, *, raw=False, vars=None): + return self._parser.get(self._name, option, raw=raw, vars=vars, + fallback=fallback) + + def getint(self, option, fallback=None, *, raw=False, vars=None): + return self._parser.getint(self._name, option, raw=raw, vars=vars, + fallback=fallback) + + def getfloat(self, option, fallback=None, *, raw=False, vars=None): + return self._parser.getfloat(self._name, option, raw=raw, vars=vars, + fallback=fallback) + + def getboolean(self, option, fallback=None, *, raw=False, vars=None): + return self._parser.getboolean(self._name, option, raw=raw, vars=vars, + fallback=fallback) + @property def parser(self): # The parser object of the proxy is read-only. diff --git a/Lib/test/test_cfgparser.py b/Lib/test/test_cfgparser.py index 2ea80dcd6c3..7b91518dacc 100644 --- a/Lib/test/test_cfgparser.py +++ b/Lib/test/test_cfgparser.py @@ -160,6 +160,49 @@ class BasicTestCase(CfgParserTestCaseClass): 'this line is much, much longer than my editor\nlikes it.') if self.allow_no_value: eq(cf['NoValue']['option-without-value'], None) + # test vars= and fallback= + eq(cf['Foo Bar'].get('foo', 'baz'), 'bar1') + eq(cf['Foo Bar'].get('foo', fallback='baz'), 'bar1') + eq(cf['Foo Bar'].get('foo', vars={'foo': 'baz'}), 'baz') + with self.assertRaises(KeyError): + cf['No Such Foo Bar']['foo'] + with self.assertRaises(KeyError): + cf['Foo Bar']['no-such-foo'] + with self.assertRaises(KeyError): + cf['No Such Foo Bar'].get('foo', fallback='baz') + eq(cf['Foo Bar'].get('no-such-foo', 'baz'), 'baz') + eq(cf['Foo Bar'].get('no-such-foo', fallback='baz'), 'baz') + eq(cf['Spacey Bar'].get('foo', None), 'bar2') + eq(cf['Spacey Bar'].get('foo', fallback=None), 'bar2') + with self.assertRaises(KeyError): + cf['No Such Spacey Bar'].get('foo', None) + eq(cf['Types'].getint('int', 18), 42) + eq(cf['Types'].getint('int', fallback=18), 42) + eq(cf['Types'].getint('no-such-int', 18), 18) + eq(cf['Types'].getint('no-such-int', fallback=18), 18) + eq(cf['Types'].getint('no-such-int', "18"), "18") # sic! + eq(cf['Types'].getint('no-such-int', fallback="18"), "18") # sic! + self.assertAlmostEqual(cf['Types'].getfloat('float', 0.0), 0.44) + self.assertAlmostEqual(cf['Types'].getfloat('float', + fallback=0.0), 0.44) + self.assertAlmostEqual(cf['Types'].getfloat('no-such-float', 0.0), 0.0) + self.assertAlmostEqual(cf['Types'].getfloat('no-such-float', + fallback=0.0), 0.0) + eq(cf['Types'].getfloat('no-such-float', "0.0"), "0.0") # sic! + eq(cf['Types'].getfloat('no-such-float', fallback="0.0"), "0.0") # sic! + eq(cf['Types'].getboolean('boolean', True), False) + eq(cf['Types'].getboolean('boolean', fallback=True), False) + eq(cf['Types'].getboolean('no-such-boolean', "yes"), "yes") # sic! + eq(cf['Types'].getboolean('no-such-boolean', fallback="yes"), + "yes") # sic! + eq(cf['Types'].getboolean('no-such-boolean', True), True) + eq(cf['Types'].getboolean('no-such-boolean', fallback=True), True) + if self.allow_no_value: + eq(cf['NoValue'].get('option-without-value', False), None) + eq(cf['NoValue'].get('option-without-value', fallback=False), None) + eq(cf['NoValue'].get('no-such-option-without-value', False), False) + eq(cf['NoValue'].get('no-such-option-without-value', + fallback=False), False) # Make sure the right things happen for remove_option(); # added to include check for SourceForge bug #123324: