[Python-checkins] cpython: Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs

lukasz.langa python-checkins at python.org
Sat Jul 7 18:59:17 CEST 2012


http://hg.python.org/cpython/rev/5257bb466d18
changeset:   77980:5257bb466d18
parent:      77977:b51a85ed3e63
user:        Łukasz Langa <lukasz at langa.pl>
date:        Sat Jul 07 18:54:08 2012 +0200
summary:
  Fixes #14590: ConfigParser doesn't strip inline comment when delimiter occurs
earlier without preceding space.

files:
  Lib/configparser.py           |  20 ++++++++---
  Lib/test/test_configparser.py |  37 +++++++++++++++++++++++
  2 files changed, 51 insertions(+), 6 deletions(-)


diff --git a/Lib/configparser.py b/Lib/configparser.py
--- a/Lib/configparser.py
+++ b/Lib/configparser.py
@@ -993,18 +993,26 @@
         indent_level = 0
         e = None                              # None, or an exception
         for lineno, line in enumerate(fp, start=1):
-            comment_start = None
+            comment_start = sys.maxsize
             # strip inline comments
-            for prefix in self._inline_comment_prefixes:
-                index = line.find(prefix)
-                if index == 0 or (index > 0 and line[index-1].isspace()):
-                    comment_start = index
-                    break
+            inline_prefixes = {p: -1 for p in self._inline_comment_prefixes}
+            while comment_start == sys.maxsize and inline_prefixes:
+                next_prefixes = {}
+                for prefix, index in inline_prefixes.items():
+                    index = line.find(prefix, index+1)
+                    if index == -1:
+                        continue
+                    next_prefixes[prefix] = index
+                    if index == 0 or (index > 0 and line[index-1].isspace()):
+                        comment_start = min(comment_start, index)
+                inline_prefixes = next_prefixes
             # strip full line comments
             for prefix in self._comment_prefixes:
                 if line.strip().startswith(prefix):
                     comment_start = 0
                     break
+            if comment_start == sys.maxsize:
+                comment_start = None
             value = line[:comment_start].strip()
             if not value:
                 if self._empty_lines_in_values:
diff --git a/Lib/test/test_configparser.py b/Lib/test/test_configparser.py
--- a/Lib/test/test_configparser.py
+++ b/Lib/test/test_configparser.py
@@ -1618,6 +1618,42 @@
         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=(';', '#',
+                '//'))
+        cfg.read_string("""
+        [section]
+        k1 = v1;still v1
+        k2 = v2 ;a comment
+        k3 = v3 ; also a comment
+        k4 = v4;still v4 ;a comment
+        k5 = v5;still v5 ; also a comment
+        k6 = v6;still v6; and still v6 ;a comment
+        k7 = v7;still v7; and still v7 ; also a comment
+
+        [multiprefix]
+        k1 = v1;still v1 #a comment ; yeah, pretty much
+        k2 = v2 // this already is a comment ; continued
+        k3 = v3;#//still v3# and still v3 ; a comment
+        """)
+        s = cfg['section']
+        self.assertEqual(s['k1'], 'v1;still v1')
+        self.assertEqual(s['k2'], 'v2')
+        self.assertEqual(s['k3'], 'v3')
+        self.assertEqual(s['k4'], 'v4;still v4')
+        self.assertEqual(s['k5'], 'v5;still v5')
+        self.assertEqual(s['k6'], 'v6;still v6; and still v6')
+        self.assertEqual(s['k7'], 'v7;still v7; and still v7')
+        s = cfg['multiprefix']
+        self.assertEqual(s['k1'], 'v1;still v1')
+        self.assertEqual(s['k2'], 'v2')
+        self.assertEqual(s['k3'], 'v3;#//still v3# and still v3')
+
+
 def test_main():
     support.run_unittest(
         ConfigParserTestCase,
@@ -1640,4 +1676,5 @@
         ReadFileTestCase,
         CoverageOneHundredTestCase,
         ExceptionPicklingTestCase,
+        InlineCommentStrippingTestCase,
         )

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list