]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Allow internal whitespace in keys.
authorFred Drake <fdrake@acm.org>
Fri, 27 Sep 2002 16:34:30 +0000 (16:34 +0000)
committerFred Drake <fdrake@acm.org>
Fri, 27 Sep 2002 16:34:30 +0000 (16:34 +0000)
Closes SF bug #583248.

Lib/ConfigParser.py
Lib/test/test_cfgparser.py

index 327f9ea86ffc1876df317b691910fe2e23f85103..85eb7b32bdc40f9ae7ee51361163631cf626e2aa 100644 (file)
@@ -385,7 +385,7 @@ class ConfigParser:
         r'\]'                                 # ]
         )
     OPTCRE = re.compile(
-        r'(?P<option>[^:=\s]+)'               # very permissive!
+        r'(?P<option>[^:=\s][^:=]*)'          # very permissive!
         r'\s*(?P<vi>[:=])\s*'                 # any number of space/tab,
                                               # followed by separator
                                               # (either : or =), followed
@@ -415,7 +415,8 @@ class ConfigParser:
             # comment or blank line?
             if line.strip() == '' or line[0] in '#;':
                 continue
-            if line.split(None, 1)[0].lower() == 'rem' and line[0] in "rR":      # no leading whitespace
+            if line.split(None, 1)[0].lower() == 'rem' and line[0] in "rR":
+                # no leading whitespace
                 continue
             # continuation line?
             if line[0].isspace() and cursect is not None and optname:
@@ -455,7 +456,7 @@ class ConfigParser:
                         # allow empty values
                         if optval == '""':
                             optval = ''
-                        optname = self.optionxform(optname)
+                        optname = self.optionxform(optname.rstrip())
                         cursect[optname] = optval
                     else:
                         # a non-fatal parsing error occurred.  set up the
index fbc915b7e47c2c37c31a66e2d99723693bcc1d10..2c9587e069014e29b43a555d006d58360dc1d72a 100644 (file)
@@ -16,6 +16,7 @@ def basic(src):
                  r'Internationalized Stuff',
                  r'Long Line',
                  r'Section\with$weird%characters[' '\t',
+                 r'Spaces',
                  r'Spacey Bar',
                  ],
            "unexpected list of section names")
@@ -26,6 +27,8 @@ def basic(src):
     verify(cf.get('Foo Bar', 'foo', raw=1) == 'bar')
     verify(cf.get('Spacey Bar', 'foo', raw=1) == 'bar')
     verify(cf.get('Commented Bar', 'foo', raw=1) == 'bar')
+    verify(cf.get('Spaces', 'key with spaces', raw=1) == 'value')
+    verify(cf.get('Spaces', 'another with spaces', raw=1) == 'splat!')
 
     verify('__name__' not in cf.options("Foo Bar"),
            '__name__ "option" should not be exposed by the API!')
@@ -225,6 +228,9 @@ foo[bg]: Bulgarian
 foo=Default
 foo[en]=English
 foo[de]=Deutsch
+[Spaces]
+key with spaces : value
+another with spaces = splat!
 """)
 write("""[Long Line]
 foo: this line is much, much longer than my editor