Author Phil Kang
Recipients Phil Kang
Date 2019-01-27.22:27:01
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1548628021.95.0.834258344082.issue35838@roundup.psfhosted.org>
In-reply-to
Content
ConfigParser calls ConfigParser.optionxform twice per each key when assigning a dictionary to a section.

The following code:

    ini = configparser.ConfigParser()
    ini.optionxform = lambda x: '(' + x + ')'
    
    # Bugged
    ini['section A'] = {'key 1': 'value 1', 'key 2': 'value 2'}
    # Not bugged
    ini.add_section('section B')
    ini['section B']['key 3'] = 'value 3'
    ini['section B']['key 4'] = 'value 4'

   inifile = io.StringIO()
   ini.write(inifile)
   print(inifile.getvalue())

...results in an INI file that looks like:

    [section A]
    ((key 1)) = value 1
    ((key 2)) = value 2
    
    [section B]
    (key 3) = value 3
    (key 4) = value 4

Here, optionxform has been called twice on key 1 and key 2, resulting in the double parentheses.

This also breaks conventional mapping access on the ConfigParser:

    print(ini['section A']['key 1'])    # Raises KeyError('key 1')
    print(ini['section A']['(key 1)'])  # OK
    
    # Raises ValueError: too many values to unpack (expected 2)
    for key, value in ini['section A']:
        print(key + ', ' + value)
History
Date User Action Args
2019-01-27 22:27:05Phil Kangsetrecipients: + Phil Kang
2019-01-27 22:27:01Phil Kangsetmessageid: <1548628021.95.0.834258344082.issue35838@roundup.psfhosted.org>
2019-01-27 22:27:01Phil Kanglinkissue35838 messages
2019-01-27 22:27:01Phil Kangcreate