Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Lib/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,7 @@ def _handle_header(self, st, mo, fpname):
st.sectname = mo.group('header')
if st.sectname in self._sections:
if self._strict and st.sectname in st.elements_added:
raise DuplicateSectionError(st.sectname, fpname,
self._handle_duplicate_section(st.sectname, fpname,
st.lineno)
st.cursect = self._sections[st.sectname]
st.elements_added.add(st.sectname)
Expand All @@ -1126,6 +1126,10 @@ def _handle_header(self, st, mo, fpname):
# So sections can't start with a continuation line
st.optname = None

def _handle_duplicate_section(self, sectname, fpname, lineno):
"""Handle duplicate section definition. Override for custom behavior."""
raise DuplicateSectionError(sectname, fpname, lineno)

def _handle_option(self, st, line, fpname):
# an option line?
st.indent_level = st.cur_indent_level
Expand All @@ -1145,7 +1149,7 @@ def _handle_option(self, st, line, fpname):
st.optname = self.optionxform(st.optname.rstrip())
if (self._strict and
(st.sectname, st.optname) in st.elements_added):
raise DuplicateOptionError(st.sectname, st.optname,
self._handle_duplicate_option(st.sectname, st.optname,
fpname, st.lineno)
st.elements_added.add((st.sectname, st.optname))
# This check is fine because the OPTCRE cannot
Expand All @@ -1157,6 +1161,10 @@ def _handle_option(self, st, line, fpname):
# valueless option handling
st.cursect[st.optname] = None

def _handle_duplicate_option(self, sectname, optname, fpname, lineno):
"""Handle duplicate option definition. Override for custom behavior."""
raise DuplicateOptionError(sectname, optname, fpname, lineno)

def _join_multiline_values(self):
defaults = self.default_section, self._defaults
all_sections = itertools.chain((defaults,),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added overridable methods for handling duplicate sections and options in ConfigParser.