Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Lib/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,10 +489,11 @@ def _interpolate_some(self, parser, option, accum, rest, section, map,
path = m.group(1).split(':')
rest = rest[m.end():]
sect = section
opt = option
try:
if len(path) == 1:
opt = parser.optionxform(path[0])
if opt not in map:
map.update(parser.items(parser.default_section))
v = map[opt]
elif len(path) == 2:
sect = path[0]
Expand Down Expand Up @@ -843,9 +844,11 @@ def items(self, section=_UNSET, raw=False, vars=None):
d = self._defaults.copy()
try:
d.update(self._sections[section])
section_keys = list(self._sections[section])
except KeyError:
if section != self.default_section:
raise NoSectionError(section)
section_keys = list(d)
# Update with the entry specific variables
if vars:
for key, value in vars.items():
Expand All @@ -854,7 +857,8 @@ def items(self, section=_UNSET, raw=False, vars=None):
section, option, d[option], d)
if raw:
value_getter = lambda option: d[option]
return [(option, value_getter(option)) for option in d.keys()]
return [(option, value_getter(option)) for option in d.keys()
if option in section_keys]

def popitem(self):
"""Remove a section from the parser and return it as
Expand Down
17 changes: 5 additions & 12 deletions Lib/test/test_configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -912,11 +912,9 @@ def test_interpolation_missing_value(self):
'%(reference)s', 'reference'))

def test_items(self):
self.check_items_config([('default', '<default>'),
('getdefault', '|<default>|'),
self.check_items_config([('getdefault', '|<default>|'),
('key', '|value|'),
('name', 'value'),
('value', 'value')])
('name', 'value')])

def test_safe_interpolation(self):
# See http://www.python.org/sf/511737
Expand Down Expand Up @@ -1090,11 +1088,9 @@ def test_interpolation(self):
"something %(with11)s lots of interpolation (11 steps)")

def test_items(self):
self.check_items_config([('default', '<default>'),
('getdefault', '|%(default)s|'),
self.check_items_config([('getdefault', '|%(default)s|'),
('key', '|%(name)s|'),
('name', '%(value)s'),
('value', 'value')])
('name', '%(value)s')])

def test_set_nonstring_types(self):
cf = self.newconfig()
Expand Down Expand Up @@ -1356,10 +1352,7 @@ def test_cfgparser_dot_3(self):
longname = 'yeah, sections can be indented as well'
self.assertFalse(cf.getboolean(longname, 'are they subsections'))
self.assertEqual(cf.get(longname, 'lets use some Unicode'), '片仮名')
self.assertEqual(len(cf.items('another one!')), 5) # 4 in section and
# `go` from DEFAULT
with self.assertRaises(configparser.InterpolationMissingOptionError):
cf.items('no values here')
self.assertEqual(len(cf.items('another one!')), 4)
self.assertEqual(cf.get('tricky interpolation', 'lets'), 'do this')
self.assertEqual(cf.get('tricky interpolation', 'lets'),
cf.get('tricky interpolation', 'go'))
Expand Down