From 1d865359629c1e352f8d29f71851cb1508c31773 Mon Sep 17 00:00:00 2001 From: Harshith Thota Date: Sat, 29 Jul 2023 20:47:53 +0530 Subject: [PATCH 1/2] Added overridable methods for handling duplicate sections and options in ConfigParser. --- Lib/configparser.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Lib/configparser.py b/Lib/configparser.py index 71362d23ec37576..20104f047bc77f1 100644 --- a/Lib/configparser.py +++ b/Lib/configparser.py @@ -997,8 +997,7 @@ def _read(self, fp, fpname): sectname = mo.group('header') if sectname in self._sections: if self._strict and sectname in elements_added: - raise DuplicateSectionError(sectname, fpname, - lineno) + self._handle_duplicate_section(sectname, fpname, lineno) cursect = self._sections[sectname] elements_added.add(sectname) elif sectname == self.default_section: @@ -1023,8 +1022,7 @@ def _read(self, fp, fpname): optname = self.optionxform(optname.rstrip()) if (self._strict and (sectname, optname) in elements_added): - raise DuplicateOptionError(sectname, optname, - fpname, lineno) + self._handle_duplicate_option(sectname, optname, fpname, lineno) elements_added.add((sectname, optname)) # This check is fine because the OPTCRE cannot # match if it would set optval to None @@ -1045,6 +1043,14 @@ def _read(self, fp, fpname): if e: raise e + def _handle_duplicate_option(self, sectname, optname, fpname, lineno): + """Handle duplicate option definition. Override for custom behavior.""" + raise DuplicateOptionError(sectname, optname, fpname, lineno) + + def _handle_duplicate_section(self, sectname, fpname, lineno): + """Handle duplicate section definition. Override for custom behavior.""" + raise DuplicateSectionError(sectname, fpname, lineno) + def _join_multiline_values(self): defaults = self.default_section, self._defaults all_sections = itertools.chain((defaults,), From 4b06849ba47dc91c3f687f8c9556cf8678b43b18 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 29 Jul 2023 15:26:25 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-07-29-15-26-24.gh-issue-107428.TPmUOE.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-07-29-15-26-24.gh-issue-107428.TPmUOE.rst diff --git a/Misc/NEWS.d/next/Library/2023-07-29-15-26-24.gh-issue-107428.TPmUOE.rst b/Misc/NEWS.d/next/Library/2023-07-29-15-26-24.gh-issue-107428.TPmUOE.rst new file mode 100644 index 000000000000000..cd9740f96168c29 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-07-29-15-26-24.gh-issue-107428.TPmUOE.rst @@ -0,0 +1 @@ +Added overridable methods for handling duplicate sections and options in ConfigParser.