cpython: 5257bb466d18 (original) (raw)
Mercurial > cpython
changeset 77980:5257bb466d18
Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs earlier without preceding space. [#14590]
Ćukasz Langa lukasz@langa.pl | |
---|---|
date | Sat, 07 Jul 2012 18:54:08 +0200 |
parents | b51a85ed3e63 |
children | 8f9e391c1e6c |
files | Lib/configparser.py Lib/test/test_configparser.py |
diffstat | 2 files changed, 51 insertions(+), 6 deletions(-)[+] [-] Lib/configparser.py 20 Lib/test/test_configparser.py 37 |
line wrap: on
line diff
--- a/Lib/configparser.py +++ b/Lib/configparser.py @@ -993,18 +993,26 @@ class RawConfigParser(MutableMapping): indent_level = 0 e = None # None, or an exception for lineno, line in enumerate(fp, start=1):
comment_start = None[](#l1.7)
comment_start = sys.maxsize[](#l1.8) # strip inline comments[](#l1.9)
for prefix in self._inline_comment_prefixes:[](#l1.10)
index = line.find(prefix)[](#l1.11)
if index == 0 or (index > 0 and line[index-1].isspace()):[](#l1.12)
comment_start = index[](#l1.13)
break[](#l1.14)
inline_prefixes = {p: -1 for p in self._inline_comment_prefixes}[](#l1.15)
while comment_start == sys.maxsize and inline_prefixes:[](#l1.16)
next_prefixes = {}[](#l1.17)
for prefix, index in inline_prefixes.items():[](#l1.18)
index = line.find(prefix, index+1)[](#l1.19)
if index == -1:[](#l1.20)
continue[](#l1.21)
next_prefixes[prefix] = index[](#l1.22)
if index == 0 or (index > 0 and line[index-1].isspace()):[](#l1.23)
comment_start = min(comment_start, index)[](#l1.24)
inline_prefixes = next_prefixes[](#l1.25) # strip full line comments[](#l1.26) for prefix in self._comment_prefixes:[](#l1.27) if line.strip().startswith(prefix):[](#l1.28) comment_start = 0[](#l1.29) break[](#l1.30)
if comment_start == sys.maxsize:[](#l1.31)
comment_start = None[](#l1.32) value = line[:comment_start].strip()[](#l1.33) if not value:[](#l1.34) if self._empty_lines_in_values:[](#l1.35)
--- a/Lib/test/test_configparser.py +++ b/Lib/test/test_configparser.py @@ -1618,6 +1618,42 @@ class ExceptionPicklingTestCase(unittest self.assertEqual(repr(e1), repr(e2)) +class InlineCommentStrippingTestCase(unittest.TestCase):
- """Tests for issue #14590: ConfigParser doesn't strip inline comment when
- delimiter occurs earlier without preceding space.."""
- def test_stripping(self):
cfg = configparser.ConfigParser(inline_comment_prefixes=(';', '#',[](#l2.12)
'//'))[](#l2.13)
cfg.read_string("""[](#l2.14)
[section][](#l2.15)
k1 = v1;still v1[](#l2.16)
k2 = v2 ;a comment[](#l2.17)
k3 = v3 ; also a comment[](#l2.18)
k4 = v4;still v4 ;a comment[](#l2.19)
k5 = v5;still v5 ; also a comment[](#l2.20)
k6 = v6;still v6; and still v6 ;a comment[](#l2.21)
k7 = v7;still v7; and still v7 ; also a comment[](#l2.22)
[multiprefix][](#l2.24)
k1 = v1;still v1 #a comment ; yeah, pretty much[](#l2.25)
k2 = v2 // this already is a comment ; continued[](#l2.26)
k3 = v3;#//still v3# and still v3 ; a comment[](#l2.27)
""")[](#l2.28)
s = cfg['section'][](#l2.29)
self.assertEqual(s['k1'], 'v1;still v1')[](#l2.30)
self.assertEqual(s['k2'], 'v2')[](#l2.31)
self.assertEqual(s['k3'], 'v3')[](#l2.32)
self.assertEqual(s['k4'], 'v4;still v4')[](#l2.33)
self.assertEqual(s['k5'], 'v5;still v5')[](#l2.34)
self.assertEqual(s['k6'], 'v6;still v6; and still v6')[](#l2.35)
self.assertEqual(s['k7'], 'v7;still v7; and still v7')[](#l2.36)
s = cfg['multiprefix'][](#l2.37)
self.assertEqual(s['k1'], 'v1;still v1')[](#l2.38)
self.assertEqual(s['k2'], 'v2')[](#l2.39)
self.assertEqual(s['k3'], 'v3;#//still v3# and still v3')[](#l2.40)
+ + def test_main(): support.run_unittest( ConfigParserTestCase, @@ -1640,4 +1676,5 @@ def test_main(): ReadFileTestCase, CoverageOneHundredTestCase, ExceptionPicklingTestCase,
InlineCommentStrippingTestCase,[](#l2.50) )[](#l2.51)