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
Index: Lib/ConfigParser.py
===================================================================
--- Lib/ConfigParser.py	(revision 68546)
+++ Lib/ConfigParser.py	(working copy)
@@ -215,13 +215,14 @@
 
 
 class RawConfigParser:
-    def __init__(self, defaults=None, dict_type=dict):
+    def __init__(self, defaults=None, dict_type=dict, unique_sects=False):
         self._dict = dict_type
         self._sections = self._dict()
         self._defaults = self._dict()
         if defaults:
             for key, value in defaults.items():
                 self._defaults[self.optionxform(key)] = value
+        self._unique_sects = unique_sects
 
     def defaults(self):
         return self._defaults
@@ -468,7 +469,10 @@
                 if mo:
                     sectname = mo.group('header')
                     if sectname in self._sections:
-                        cursect = self._sections[sectname]
+                        if self._unique_sects:
+                            raise DuplicateSectionError(sectname)
+                        else:
+                            cursect = self._sections[sectname]
                     elif sectname == DEFAULTSECT:
                         cursect = self._defaults
                     else:

Attachment: signature.asc
Description: This is a digitally signed message part.

_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to