Commit 0a1fa0e8 authored by Fred Drake's avatar Fred Drake

Issue #9551: Do not raise TypeError when setting the value to None for

SafeConfigParser instances constructed with allow_no_value == True.
parent 2d930804
...@@ -699,13 +699,13 @@ class SafeConfigParser(ConfigParser): ...@@ -699,13 +699,13 @@ class SafeConfigParser(ConfigParser):
if self._optcre is self.OPTCRE or value: if self._optcre is self.OPTCRE or value:
if not isinstance(value, basestring): if not isinstance(value, basestring):
raise TypeError("option values must be strings") raise TypeError("option values must be strings")
# check for bad percent signs: if value is not None:
# first, replace all "good" interpolations # check for bad percent signs:
tmp_value = value.replace('%%', '') # first, replace all "good" interpolations
tmp_value = self._interpvar_re.sub('', tmp_value) tmp_value = value.replace('%%', '')
# then, check if there's a lone percent sign left tmp_value = self._interpvar_re.sub('', tmp_value)
percent_index = tmp_value.find('%') # then, check if there's a lone percent sign left
if percent_index != -1: if '%' in tmp_value:
raise ValueError("invalid interpolation syntax in %r at " raise ValueError("invalid interpolation syntax in %r at "
"position %d" % (value, percent_index)) "position %d" % (value, tmp_value.find('%')))
ConfigParser.set(self, section, option, value) ConfigParser.set(self, section, option, value)
...@@ -357,6 +357,7 @@ class TestCaseBase(unittest.TestCase): ...@@ -357,6 +357,7 @@ class TestCaseBase(unittest.TestCase):
class ConfigParserTestCase(TestCaseBase): class ConfigParserTestCase(TestCaseBase):
config_class = ConfigParser.ConfigParser config_class = ConfigParser.ConfigParser
allow_no_value = True
def test_interpolation(self): def test_interpolation(self):
rawval = { rawval = {
...@@ -397,6 +398,7 @@ class ConfigParserTestCase(TestCaseBase): ...@@ -397,6 +398,7 @@ class ConfigParserTestCase(TestCaseBase):
cf.set('non-string', 'dict', {'pi': 3.14159, '%(': 1, cf.set('non-string', 'dict', {'pi': 3.14159, '%(': 1,
'%(list)': '%(list)'}) '%(list)': '%(list)'})
cf.set('non-string', 'string_with_interpolation', '%(list)s') cf.set('non-string', 'string_with_interpolation', '%(list)s')
cf.set('non-string', 'no-value')
self.assertEqual(cf.get('non-string', 'int', raw=True), 1) self.assertEqual(cf.get('non-string', 'int', raw=True), 1)
self.assertRaises(TypeError, cf.get, 'non-string', 'int') self.assertRaises(TypeError, cf.get, 'non-string', 'int')
self.assertEqual(cf.get('non-string', 'list', raw=True), self.assertEqual(cf.get('non-string', 'list', raw=True),
...@@ -409,6 +411,7 @@ class ConfigParserTestCase(TestCaseBase): ...@@ -409,6 +411,7 @@ class ConfigParserTestCase(TestCaseBase):
raw=True), '%(list)s') raw=True), '%(list)s')
self.assertRaises(ValueError, cf.get, 'non-string', self.assertRaises(ValueError, cf.get, 'non-string',
'string_with_interpolation', raw=False) 'string_with_interpolation', raw=False)
self.assertEqual(cf.get('non-string', 'no-value'), None)
class MultilineValuesTestCase(TestCaseBase): class MultilineValuesTestCase(TestCaseBase):
config_class = ConfigParser.ConfigParser config_class = ConfigParser.ConfigParser
......
...@@ -29,6 +29,9 @@ Core and Builtins ...@@ -29,6 +29,9 @@ Core and Builtins
Library Library
------- -------
- Issue #9551: Don't raise TypeError when setting the value to None for
SafeConfigParser instances constructed with allow_no_value == True.
- Issue #6915: Under Windows, os.listdir() didn't release the Global - Issue #6915: Under Windows, os.listdir() didn't release the Global
Interpreter Lock around all system calls. Original patch by Ryan Kelly. Interpreter Lock around all system calls. Original patch by Ryan Kelly.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment