Merge pull request #47151 from zer0def/configparser-defaultsect

Allow interaction with default section in ConfigParser serializer
This commit is contained in:
Nicole Thomas 2018-05-16 09:53:43 -04:00 committed by GitHub
commit a2ed8cbb7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -85,15 +85,28 @@ def serialize(obj, **options):
raise SerializationError(error)
def _read_dict(configparser, dictionary):
def _is_defaultsect(section_name):
if six.PY3:
return section_name == configparser.DEFAULTSECT
else: # in py2 the check is done against lowercased section name
return section_name.upper() == configparser.DEFAULTSECT
def _read_dict(cp, dictionary):
'''
Cribbed from python3's ConfigParser.read_dict function.
'''
for section, keys in dictionary.items():
section = str(section)
configparser.add_section(section)
if _is_defaultsect(section):
if six.PY2:
section = configparser.DEFAULTSECT
else:
cp.add_section(section)
for key, value in keys.items():
key = configparser.optionxform(str(key))
key = cp.optionxform(str(key))
if value is not None:
value = str(value)
configparser.set(section, key, value)
cp.set(section, key, value)