[Python-Dev] [patch] Duplicate sections detection in ConfigParser

Yannick Gingras ygingras at ygingras.net
Tue Feb 3 07:34:13 CET 2009


Greetings Pythonistas, 
  ConfigParser as you most certainly know is a .ini parser included in
the Python standard library.  It's documentation mentions a
DuplicateSectionError but I was puzzled after hunting a bug in our
application that this error was not raised when parsing a file with
duplicate sections.  After looking at the code, it turns out that
DuplicateSectionError is only raised when using the builder interface;
the parser interface will never throw it.

The attached patch is compatible with both the 2.x and the 3.x
branches; it adds a `unique_sects` parameter to the constructor of
RawConfigParser and a test in the parser loop that raises
DuplicateSectionError if a section is seen more then once and that
unique_sects is True.

This is just a proof of concept and I'd like your opinion on it before
working on the final version.  I see two main issues regarding
backward compatibility and uniformity.  For uniformity `unique_sects`
should also apply to the builder interface.  However, if it does, it
should default to True since it was the default behavior for the
builder interface to raise on duplicate sections.  On the other hand,
the default behavior for the parser interface was to ignore duplicate
sections so making `unique_sects` default to True might break existing
code.

To summarize, I ask you all:

* Is the lack of duplicates detection in the parser a problem worth
  addressing?
* Should `unique_sects` control the builder interface?
* Should it default to True?

Best regards, 

-- 
Yannick Gingras
http://ygingras.net
-------------- next part --------------
A non-text attachment was scrubbed...
Name: no-dups-configparser.diff
Type: text/x-patch
Size: 1248 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-dev/attachments/20090203/cf50f14f/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.python.org/pipermail/python-dev/attachments/20090203/cf50f14f/attachment.pgp>


More information about the Python-Dev mailing list