1
0
mirror of https://github.com/openSUSE/osc.git synced 2024-11-12 15:46:15 +01:00

Fix config parser to throw an exception on duplicate sections or options

This commit is contained in:
Daniel Mach 2024-06-03 08:21:17 +02:00
parent d92f2677f4
commit 360a94c4a3
2 changed files with 29 additions and 2 deletions

View File

@ -263,7 +263,9 @@ class OscConfigParser(configparser.ConfigParser):
mo = self.SECTCRE.match(line)
if mo:
sectname = mo.group('header')
if sectname in self._sections:
if self._strict and sectname in self._sections:
raise configparser.DuplicateSectionError(sectname, fpname, lineno)
elif sectname in self._sections:
cursect = self._sections[sectname]
elif sectname == configparser.DEFAULTSECT:
cursect = self._defaults
@ -294,7 +296,9 @@ class OscConfigParser(configparser.ConfigParser):
if optval == '""':
optval = ''
optname = self.optionxform(optname.rstrip())
if cursect == configparser.DEFAULTSECT:
if self._strict and optname in self._sections[cursect]:
raise configparser.DuplicateOptionError(sectname, optname, fpname, lineno)
elif cursect == configparser.DEFAULTSECT:
self._defaults[optname] = optval
else:
self._sections[cursect]._add_option(optname, line=line)

View File

@ -1,3 +1,4 @@
import configparser
import unittest
from osc.OscConfigParser import OscConfigParser
@ -21,6 +22,28 @@ pass=
# ValueError: invalid interpolation syntax in '%' at position 0
self.parser.set("http://localhost", "pass", "%")
def test_duplicate_section(self):
conf = """
[general]
[http://localhost]
[http://localhost]
"""
parser = OscConfigParser()
self.assertRaises(configparser.DuplicateSectionError, parser.read_string, conf)
def test_duplicate_option(self):
conf = """
[general]
[http://localhost]
user=
user=
"""
parser = OscConfigParser()
self.assertRaises(configparser.DuplicateOptionError, parser.read_string, conf)
if __name__ == "__main__":
unittest.main()