Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2f0fd0f

Browse files
committed
configparser: mapping protocol access get() handles configparser-specific arguments as well
1 parent 1215915 commit 2f0fd0f

2 files changed

Lines changed: 59 additions & 6 deletions

File tree

Lib/configparser.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,12 +1195,6 @@ def __init__(self, parser, name):
11951195
"""Creates a view on a section of the specified `name` in `parser`."""
11961196
self._parser = parser
11971197
self._name = name
1198-
self.getint = functools.partial(self._parser.getint,
1199-
self._name)
1200-
self.getfloat = functools.partial(self._parser.getfloat,
1201-
self._name)
1202-
self.getboolean = functools.partial(self._parser.getboolean,
1203-
self._name)
12041198

12051199
def __repr__(self):
12061200
return '<Section: {}>'.format(self._name)
@@ -1231,6 +1225,22 @@ def __iter__(self):
12311225
# XXX does not break when underlying container state changed
12321226
return self._parser.options(self._name).__iter__()
12331227

1228+
def get(self, option, fallback=None, *, raw=False, vars=None):
1229+
return self._parser.get(self._name, option, raw=raw, vars=vars,
1230+
fallback=fallback)
1231+
1232+
def getint(self, option, fallback=None, *, raw=False, vars=None):
1233+
return self._parser.getint(self._name, option, raw=raw, vars=vars,
1234+
fallback=fallback)
1235+
1236+
def getfloat(self, option, fallback=None, *, raw=False, vars=None):
1237+
return self._parser.getfloat(self._name, option, raw=raw, vars=vars,
1238+
fallback=fallback)
1239+
1240+
def getboolean(self, option, fallback=None, *, raw=False, vars=None):
1241+
return self._parser.getboolean(self._name, option, raw=raw, vars=vars,
1242+
fallback=fallback)
1243+
12341244
@property
12351245
def parser(self):
12361246
# The parser object of the proxy is read-only.

Lib/test/test_cfgparser.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,49 @@ def basic_test(self, cf):
160160
'this line is much, much longer than my editor\nlikes it.')
161161
if self.allow_no_value:
162162
eq(cf['NoValue']['option-without-value'], None)
163+
# test vars= and fallback=
164+
eq(cf['Foo Bar'].get('foo', 'baz'), 'bar1')
165+
eq(cf['Foo Bar'].get('foo', fallback='baz'), 'bar1')
166+
eq(cf['Foo Bar'].get('foo', vars={'foo': 'baz'}), 'baz')
167+
with self.assertRaises(KeyError):
168+
cf['No Such Foo Bar']['foo']
169+
with self.assertRaises(KeyError):
170+
cf['Foo Bar']['no-such-foo']
171+
with self.assertRaises(KeyError):
172+
cf['No Such Foo Bar'].get('foo', fallback='baz')
173+
eq(cf['Foo Bar'].get('no-such-foo', 'baz'), 'baz')
174+
eq(cf['Foo Bar'].get('no-such-foo', fallback='baz'), 'baz')
175+
eq(cf['Spacey Bar'].get('foo', None), 'bar2')
176+
eq(cf['Spacey Bar'].get('foo', fallback=None), 'bar2')
177+
with self.assertRaises(KeyError):
178+
cf['No Such Spacey Bar'].get('foo', None)
179+
eq(cf['Types'].getint('int', 18), 42)
180+
eq(cf['Types'].getint('int', fallback=18), 42)
181+
eq(cf['Types'].getint('no-such-int', 18), 18)
182+
eq(cf['Types'].getint('no-such-int', fallback=18), 18)
183+
eq(cf['Types'].getint('no-such-int', "18"), "18") # sic!
184+
eq(cf['Types'].getint('no-such-int', fallback="18"), "18") # sic!
185+
self.assertAlmostEqual(cf['Types'].getfloat('float', 0.0), 0.44)
186+
self.assertAlmostEqual(cf['Types'].getfloat('float',
187+
fallback=0.0), 0.44)
188+
self.assertAlmostEqual(cf['Types'].getfloat('no-such-float', 0.0), 0.0)
189+
self.assertAlmostEqual(cf['Types'].getfloat('no-such-float',
190+
fallback=0.0), 0.0)
191+
eq(cf['Types'].getfloat('no-such-float', "0.0"), "0.0") # sic!
192+
eq(cf['Types'].getfloat('no-such-float', fallback="0.0"), "0.0") # sic!
193+
eq(cf['Types'].getboolean('boolean', True), False)
194+
eq(cf['Types'].getboolean('boolean', fallback=True), False)
195+
eq(cf['Types'].getboolean('no-such-boolean', "yes"), "yes") # sic!
196+
eq(cf['Types'].getboolean('no-such-boolean', fallback="yes"),
197+
"yes") # sic!
198+
eq(cf['Types'].getboolean('no-such-boolean', True), True)
199+
eq(cf['Types'].getboolean('no-such-boolean', fallback=True), True)
200+
if self.allow_no_value:
201+
eq(cf['NoValue'].get('option-without-value', False), None)
202+
eq(cf['NoValue'].get('option-without-value', fallback=False), None)
203+
eq(cf['NoValue'].get('no-such-option-without-value', False), False)
204+
eq(cf['NoValue'].get('no-such-option-without-value',
205+
fallback=False), False)
163206

164207
# Make sure the right things happen for remove_option();
165208
# added to include check for SourceForge bug #123324:

0 commit comments

Comments
 (0)