This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Implement sectionxform in configparser
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: duplicate
Dependencies: Superseder: ConfigParser has optionxform, but not sectionxform
View: 26537
Assigned To: lukasz.langa Nosy List: Kunjesh.Kaushik, berker.peksag, eric.araujo, fdrake, lukasz.langa, r.david.murray, rhettinger
Priority: normal Keywords: easy

Created on 2011-01-27 14:37 by Kunjesh.Kaushik, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
allow_spaces_around_section_header.diff Kunjesh.Kaushik, 2011-01-27 14:37 Allow spaces around section header in ConfigParser review
allow_spaces_around_section_header.diff Kunjesh.Kaushik, 2011-01-28 07:59 Fixed patch file. review
Messages (13)
msg127193 - (view) Author: Kunjesh Kaushik (Kunjesh.Kaushik) Date: 2011-01-27 14:37
It is often desirable to be able to write a section with spaces around the header, as in "[ default ]" instead of "[default]" for the sake of readability of configuration file. I am not sure if this is the "standard" format of configuration files.

The following (attached) patch will achieve the desired result. It is also backward-compatible. Kindly arrange for the patch to be applied to the repository or to a future release.

Original Code Location: http://svn.python.org/view/*checkout*/python/branches/release27-maint/Lib/ConfigParser.py?revision=84443

Index: Lib/ConfigParser.py
===================================================================
--- Lib/ConfigParser.py	(revision 84443)
+++ Lib/ConfigParser.py	(working copy)
@@ -432,7 +432,7 @@
     #
     SECTCRE = re.compile(
         r'\['                                 # [
-        r'(?P<header>[^]]+)'                  # very permissive!
+        r'\s*(?P<header>[^]]+)\s*'            # very permissive!
         r'\]'                                 # ]
         )
     OPTCRE = re.compile(
msg127206 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2011-01-27 18:16
A feature request can only go in to 3.3 at this point.  ConfigParser has had a serious overhaul in 3.2, by the way.
msg127211 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-01-27 18:55
There's a case to be made that the current regex is buggy.  It already accepts whitespace around the header name but doesn't strip it.  ISTM, this is undesirable:  [ section header ]  --> ' section header ' instead of 'section header'.

>>> import configparser
>>> r = configparser.ConfigParser.SECTCRE
>>> r.match('[ section header ]').group('header')
' section header '

That result is not want people would usually want or expect.  I don't see any advantage to deferring this to 3.3.
msg127215 - (view) Author: Kunjesh Kaushik (Kunjesh.Kaushik) Date: 2011-01-27 19:33
Mr. Raymond has raised a valid point. On second thought, I think the submitted patch won't resolve the issue.

>>> import re
>>> r = re.compile(r'\[\s*(?P<header>[^]]+)\s*\]') # as in the patch
>>> r.match('[ section header ]').group('header')  # still has issues
'section header '

ISTM, the only solution to this problem is to strip the section headers while parsing the file. Subsequent access mechanism should also follow suit. IMHO, section headers should be made case-insensitive as well -- similar to option keys.
msg127218 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2011-01-27 20:01
Well, there's still a backward compatibility issue: if this is changed, currently working code may break.  Maybe Lukasz or Fred will have a guess as to how likely that is, because I don't.
msg127223 - (view) Author: Fred Drake (fdrake) (Python committer) Date: 2011-01-27 20:35
I doubt anyone is looking for section names with leading or trailing whitespace.

One approach to dealing with this is to provide and sectionxform similar to optionxform.  If we're wrong and someone really is expecting leading or trailing whitespace, they can set that (to str) to cancel out the change in behavior.

While I'd be surprised if anyone relies on this, it is a feature change, no matter how it's implemented, so must wait for 3.3.
msg127230 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-01-27 22:13
Could just expand the docs to show examples of customizing behavior through monkey-patching or subclassing:

  class MyConfigParser(ConfigParser):
     SECTRE = re.compile(r'[\*(?P<header>[^]]+)\s*]'

or:

  RawConfigParser.SECTRE = re.compile(r'[\*(?P<header>[^]]+)\s*]'
msg127251 - (view) Author: Kunjesh Kaushik (Kunjesh.Kaushik) Date: 2011-01-28 07:59
I think we are dealing with two separate issues: a feature request for sectionxform kind of functionality desirable in a future release (3.3 maybe) and a behaviour issue in current releases (2.x and 3.x both). I suggest we split the two issues and solve them as such. Deferring the latter may be undesirable.

Also, I found that a non-greedy pattern will work with the original patch:

>>> import re
>>> r = re.compile(r'\[\s*(?P<header>[^]]+?)\s*\]') # note +? instead of +
>>> r.match('[ section header ]').group('header')   # works as "expected"
'section header'

Attaching a new patch file as well.
msg127271 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2011-01-28 10:30
Kunjesh, first of all many thanks for your feedback! A bit of advice, though. I have myself made the mistake of calling features I personally want as "often needed". The reality is that they are not often needed if they weren't reported before. While the current behaviour is not intuitive and probably unwelcome, it's hardly a bug if it has been that way for 15 years now.

At one point I had `sectionxform` implemented for configparser 3.2 but it made the source less readable. Then I thought about what Raymond taught me: is this a feature that follows a real use case? I concluded it didn't so I left it out. That being said, your report proves that such functionality would be welcome. I checked and ConfigObj does strip whitespace from section names, too. Nevertheless, in my opinion that's a feature request that only applies to Python 3.3. Your patches are for 2.7.

Let me elaborate a bit on a broader issue: configparser as of 3.2 still has only limited ability to write configuration files back (it strips whitespace and comments between options, option names get xformed). I want to fix that for 3.3 so the file can be altered and written back with only minimal changes. `optionxform` already gets in the way, `sectionxform` would too, if improperly implemented.

Raymond, I would want to keep the regexes as raw as possible as to enable writing the file back with the whitespace preserved. That includes whitespace around the section name, if we are to support that.

To conclude: I won't touch the regexes, I will implement `sectionxform` while implementing on-save whitespace and comment preservation for 3.3.
msg127278 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-01-28 10:41
My recommendation wasn't to change the regexes.

Instead, I recommended documenting how to override them in a subclass.
msg127282 - (view) Author: Kunjesh Kaushik (Kunjesh.Kaushik) Date: 2011-01-28 11:03
Very well, then. I would rely on sub-classing for now. The patch would work for me as I am only reading configuration. :)

And yes, I wouldn't deny the personal bias anyway. Thanks a lot for all your help, folks. Keep up the good work!
msg127290 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2011-01-28 11:58
Documentation updated in r88220.
msg269977 - (view) Author: Berker Peksag (berker.peksag) * (Python committer) Date: 2016-07-08 07:12
Issue 26537 is a duplicate of this one but it has an up-to-date and more complete patch so I'm going to close this one.
History
Date User Action Args
2022-04-11 14:57:11adminsetgithub: 55236
2016-07-08 07:12:57berker.peksagsetstatus: open -> closed

superseder: ConfigParser has optionxform, but not sectionxform

nosy: + berker.peksag
messages: + msg269977
resolution: duplicate
stage: needs patch -> resolved
2012-07-07 08:34:48lukasz.langasetversions: + Python 3.4, - Python 3.3
2011-07-13 15:10:38eric.araujosetnosy: + eric.araujo
2011-01-28 11:58:06lukasz.langasetnosy: fdrake, rhettinger, r.david.murray, lukasz.langa, Kunjesh.Kaushik
messages: + msg127290
2011-01-28 11:57:47lukasz.langasetnosy: fdrake, rhettinger, r.david.murray, lukasz.langa, Kunjesh.Kaushik
messages: - msg127279
2011-01-28 11:03:08Kunjesh.Kaushiksetnosy: fdrake, rhettinger, r.david.murray, lukasz.langa, Kunjesh.Kaushik
messages: + msg127282
2011-01-28 10:43:02lukasz.langasetnosy: fdrake, rhettinger, r.david.murray, lukasz.langa, Kunjesh.Kaushik
messages: + msg127279
2011-01-28 10:41:30rhettingersetnosy: fdrake, rhettinger, r.david.murray, lukasz.langa, Kunjesh.Kaushik
messages: + msg127278
2011-01-28 10:30:28lukasz.langasettitle: Allow spaces around section header in ConfigParser -> Implement sectionxform in configparser
nosy: fdrake, rhettinger, r.david.murray, lukasz.langa, Kunjesh.Kaushik
messages: + msg127271

keywords: - patch
stage: needs patch
2011-01-28 07:59:46Kunjesh.Kaushiksetfiles: + allow_spaces_around_section_header.diff

messages: + msg127251
keywords: + patch
nosy: fdrake, rhettinger, r.david.murray, lukasz.langa, Kunjesh.Kaushik
2011-01-27 22:13:02rhettingersetnosy: fdrake, rhettinger, r.david.murray, lukasz.langa, Kunjesh.Kaushik
messages: + msg127230
2011-01-27 20:35:07fdrakesetnosy: fdrake, rhettinger, r.david.murray, lukasz.langa, Kunjesh.Kaushik
messages: + msg127223
2011-01-27 20:01:52r.david.murraysetnosy: + fdrake
messages: + msg127218
2011-01-27 19:33:07Kunjesh.Kaushiksetnosy: rhettinger, r.david.murray, lukasz.langa, Kunjesh.Kaushik
messages: + msg127215
2011-01-27 18:55:39rhettingersetkeywords: + easy, - patch
nosy: + rhettinger
messages: + msg127211

2011-01-27 18:16:29r.david.murraysetversions: + Python 3.3, - Python 2.7
nosy: + r.david.murray, lukasz.langa

messages: + msg127206

assignee: lukasz.langa
2011-01-27 14:37:04Kunjesh.Kaushikcreate