mirror of
https://github.com/python/cpython.git
synced 2025-08-01 07:33:08 +00:00
Fix #7113. Patch by Łukasz Langa.
Changes include using a list of lines instead of patching together using string interpolation, and a multi-line value test cases.
This commit is contained in:
parent
53f94d0753
commit
e4334b4949
2 changed files with 60 additions and 16 deletions
|
@ -398,12 +398,11 @@ class RawConfigParser:
|
||||||
for section in self._sections:
|
for section in self._sections:
|
||||||
fp.write("[%s]\n" % section)
|
fp.write("[%s]\n" % section)
|
||||||
for (key, value) in self._sections[section].items():
|
for (key, value) in self._sections[section].items():
|
||||||
if key != "__name__":
|
if key == "__name__":
|
||||||
if value is None:
|
continue
|
||||||
fp.write("%s\n" % (key))
|
if value is not None:
|
||||||
else:
|
key = " = ".join((key, str(value).replace('\n', '\n\t')))
|
||||||
fp.write("%s = %s\n" %
|
fp.write("%s\n" % (key))
|
||||||
(key, str(value).replace('\n', '\n\t')))
|
|
||||||
fp.write("\n")
|
fp.write("\n")
|
||||||
|
|
||||||
def remove_option(self, section, option):
|
def remove_option(self, section, option):
|
||||||
|
@ -464,10 +463,10 @@ class RawConfigParser:
|
||||||
leading whitespace. Blank lines, lines beginning with a '#',
|
leading whitespace. Blank lines, lines beginning with a '#',
|
||||||
and just about everything else are ignored.
|
and just about everything else are ignored.
|
||||||
"""
|
"""
|
||||||
cursect = None # None, or a dictionary
|
cursect = None # None, or a dictionary
|
||||||
optname = None
|
optname = None
|
||||||
lineno = 0
|
lineno = 0
|
||||||
e = None # None, or an exception
|
e = None # None, or an exception
|
||||||
while True:
|
while True:
|
||||||
line = fp.readline()
|
line = fp.readline()
|
||||||
if not line:
|
if not line:
|
||||||
|
@ -483,7 +482,7 @@ class RawConfigParser:
|
||||||
if line[0].isspace() and cursect is not None and optname:
|
if line[0].isspace() and cursect is not None and optname:
|
||||||
value = line.strip()
|
value = line.strip()
|
||||||
if value:
|
if value:
|
||||||
cursect[optname] = "%s\n%s" % (cursect[optname], value)
|
cursect[optname].append(value)
|
||||||
# a section header or option header?
|
# a section header or option header?
|
||||||
else:
|
else:
|
||||||
# is it a section header?
|
# is it a section header?
|
||||||
|
@ -508,6 +507,7 @@ class RawConfigParser:
|
||||||
mo = self._optcre.match(line)
|
mo = self._optcre.match(line)
|
||||||
if mo:
|
if mo:
|
||||||
optname, vi, optval = mo.group('option', 'vi', 'value')
|
optname, vi, optval = mo.group('option', 'vi', 'value')
|
||||||
|
optname = self.optionxform(optname.rstrip())
|
||||||
# This check is fine because the OPTCRE cannot
|
# This check is fine because the OPTCRE cannot
|
||||||
# match if it would set optval to None
|
# match if it would set optval to None
|
||||||
if optval is not None:
|
if optval is not None:
|
||||||
|
@ -518,11 +518,13 @@ class RawConfigParser:
|
||||||
if pos != -1 and optval[pos-1].isspace():
|
if pos != -1 and optval[pos-1].isspace():
|
||||||
optval = optval[:pos]
|
optval = optval[:pos]
|
||||||
optval = optval.strip()
|
optval = optval.strip()
|
||||||
# allow empty values
|
# allow empty values
|
||||||
if optval == '""':
|
if optval == '""':
|
||||||
optval = ''
|
optval = ''
|
||||||
optname = self.optionxform(optname.rstrip())
|
cursect[optname] = [optval]
|
||||||
cursect[optname] = optval
|
else:
|
||||||
|
# valueless option handling
|
||||||
|
cursect[optname] = optval
|
||||||
else:
|
else:
|
||||||
# a non-fatal parsing error occurred. set up the
|
# a non-fatal parsing error occurred. set up the
|
||||||
# exception but keep going. the exception will be
|
# exception but keep going. the exception will be
|
||||||
|
@ -535,6 +537,13 @@ class RawConfigParser:
|
||||||
if e:
|
if e:
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
|
# join the multi-line values collected while reading
|
||||||
|
all_sections = [self._defaults]
|
||||||
|
all_sections.extend(self._sections.values())
|
||||||
|
for options in all_sections:
|
||||||
|
for name, val in options.items():
|
||||||
|
if isinstance(val, list):
|
||||||
|
options[name] = '\n'.join(val)
|
||||||
|
|
||||||
class ConfigParser(RawConfigParser):
|
class ConfigParser(RawConfigParser):
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import ConfigParser
|
import ConfigParser
|
||||||
import StringIO
|
import StringIO
|
||||||
|
import os
|
||||||
import unittest
|
import unittest
|
||||||
import UserDict
|
import UserDict
|
||||||
|
|
||||||
|
@ -186,7 +187,8 @@ class TestCaseBase(unittest.TestCase):
|
||||||
self.assertEqual(cf.sections(), [],
|
self.assertEqual(cf.sections(), [],
|
||||||
"new ConfigParser should have no defined sections")
|
"new ConfigParser should have no defined sections")
|
||||||
self.assertFalse(cf.has_section("Foo"),
|
self.assertFalse(cf.has_section("Foo"),
|
||||||
"new ConfigParser should have no acknowledged sections")
|
"new ConfigParser should have no acknowledged "
|
||||||
|
"sections")
|
||||||
self.assertRaises(ConfigParser.NoSectionError,
|
self.assertRaises(ConfigParser.NoSectionError,
|
||||||
cf.options, "Foo")
|
cf.options, "Foo")
|
||||||
self.assertRaises(ConfigParser.NoSectionError,
|
self.assertRaises(ConfigParser.NoSectionError,
|
||||||
|
@ -357,6 +359,11 @@ class ConfigParserTestCase(TestCaseBase):
|
||||||
config_class = ConfigParser.ConfigParser
|
config_class = ConfigParser.ConfigParser
|
||||||
|
|
||||||
def test_interpolation(self):
|
def test_interpolation(self):
|
||||||
|
rawval = {
|
||||||
|
ConfigParser.ConfigParser: ("something %(with11)s "
|
||||||
|
"lots of interpolation (11 steps)"),
|
||||||
|
ConfigParser.SafeConfigParser: "%(with1)s",
|
||||||
|
}
|
||||||
cf = self.get_interpolation_config()
|
cf = self.get_interpolation_config()
|
||||||
eq = self.assertEqual
|
eq = self.assertEqual
|
||||||
eq(cf.get("Foo", "getname"), "Foo")
|
eq(cf.get("Foo", "getname"), "Foo")
|
||||||
|
@ -403,6 +410,33 @@ class ConfigParserTestCase(TestCaseBase):
|
||||||
self.assertRaises(ValueError, cf.get, 'non-string',
|
self.assertRaises(ValueError, cf.get, 'non-string',
|
||||||
'string_with_interpolation', raw=False)
|
'string_with_interpolation', raw=False)
|
||||||
|
|
||||||
|
class MultilineValuesTestCase(TestCaseBase):
|
||||||
|
config_class = ConfigParser.ConfigParser
|
||||||
|
wonderful_spam = ("I'm having spam spam spam spam "
|
||||||
|
"spam spam spam beaked beans spam "
|
||||||
|
"spam spam and spam!").replace(' ', '\t\n')
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
cf = self.newconfig()
|
||||||
|
for i in range(100):
|
||||||
|
s = 'section{}'.format(i)
|
||||||
|
cf.add_section(s)
|
||||||
|
for j in range(10):
|
||||||
|
cf.set(s, 'lovely_spam{}'.format(j), self.wonderful_spam)
|
||||||
|
with open(test_support.TESTFN, 'w') as f:
|
||||||
|
cf.write(f)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
os.unlink(test_support.TESTFN)
|
||||||
|
|
||||||
|
def test_dominating_multiline_values(self):
|
||||||
|
# we're reading from file because this is where the code changed
|
||||||
|
# during performance updates in Python 3.2
|
||||||
|
cf_from_file = self.newconfig()
|
||||||
|
with open(test_support.TESTFN) as f:
|
||||||
|
cf_from_file.readfp(f)
|
||||||
|
self.assertEqual(cf_from_file.get('section8', 'lovely_spam4'),
|
||||||
|
self.wonderful_spam.replace('\t\n', '\n'))
|
||||||
|
|
||||||
class RawConfigParserTestCase(TestCaseBase):
|
class RawConfigParserTestCase(TestCaseBase):
|
||||||
config_class = ConfigParser.RawConfigParser
|
config_class = ConfigParser.RawConfigParser
|
||||||
|
@ -521,10 +555,11 @@ class SortedTestCase(RawConfigParserTestCase):
|
||||||
def test_main():
|
def test_main():
|
||||||
test_support.run_unittest(
|
test_support.run_unittest(
|
||||||
ConfigParserTestCase,
|
ConfigParserTestCase,
|
||||||
|
MultilineValuesTestCase,
|
||||||
RawConfigParserTestCase,
|
RawConfigParserTestCase,
|
||||||
SafeConfigParserTestCase,
|
SafeConfigParserTestCase,
|
||||||
SortedTestCase,
|
|
||||||
SafeConfigParserTestCaseNoValue,
|
SafeConfigParserTestCaseNoValue,
|
||||||
|
SortedTestCase,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue