--- Lib/configparser.py.orig 2014-04-16 14:28:16.406648591 -0400 +++ Lib/configparser.py 2014-04-16 14:32:50.078662782 -0400 @@ -865,9 +865,9 @@ return (option in self._sections[section] or option in self._defaults) - def set(self, section, option, value=None): + def set(self, section, option, value=None, raw=False): """Set an option.""" - if value: + if value and not raw: value = self._interpolation.before_set(self, section, option, value) if not section or section == self.default_section: @@ -1160,11 +1160,12 @@ _DEFAULT_INTERPOLATION = BasicInterpolation() - def set(self, section, option, value=None): + def set(self, section, option, value=None, raw=False): """Set an option. Extends RawConfigParser.set by validating type and interpolation syntax on the value.""" - self._validate_value_types(option=option, value=value) - super().set(section, option, value) + if not raw: + self._validate_value_types(option=option, value=value) + super().set(section, option, value, raw) def add_section(self, section): """Create a new section in the configuration. Extends --- Lib/test/test_configparser.py.orig 2014-04-16 14:29:44.246653146 -0400 +++ Lib/test/test_configparser.py 2014-04-16 15:11:09.458782015 -0400 @@ -1764,5 +1764,28 @@ self.assertEqual(s['k3'], 'v3;#//still v3# and still v3') +class RawParameterAsymmetryTestCase(unittest.TestCase): + ''' + Tests for issue #21265: ConfigParser can read values with + interpolation syntax violations (via `raw` parameter), but cannot + set them. + ''' + + def test_raw_value_get(self): + cfg = configparser.ConfigParser() + cfg.read_string('[time]\nfmt = %H:%M:%S\n') + self.assertEqual(cfg.get('time', 'fmt', raw=True), '%H:%M:%S') + with self.assertRaises(configparser.InterpolationSyntaxError) as cm: + cfg.get('time', 'fmt') + + def test_raw_value_set(self): + cfg = configparser.ConfigParser() + cfg.add_section('time') + cfg.set('time', 'fmt', '%I:%M %p', raw=True) + self.assertEqual(cfg.get('time', 'fmt', raw=True), '%I:%M %p') + with self.assertRaises(configparser.InterpolationSyntaxError) as cm: + cfg.get('time', 'fmt') + + if __name__ == '__main__': unittest.main()