This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ConfigParser(dict_type=) not behaving as expected
Type: behavior Stage:
Components: Extension Modules Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Cyril Jouve, malonn
Priority: normal Keywords:

Created on 2022-02-16 17:17 by malonn, last changed 2022-04-11 14:59 by admin.

Messages (3)
msg413343 - (view) Author: Mark Lonnemann (malonn) Date: 2022-02-16 17:17
ConfigParser() is not using a custom dictionary class correctly, according to my understanding.  I have duplicate options in a config file that I want to rename uniquely.  The following code does not work.
x = 0
class MultiDict(dict):
    def __setitem__(self, key, value):
        if key == 'textsize':
            global x
            key += str(x)
            x += 1
        dict.__setitem__(self, key, value)

...

config1 = cp.ConfigParser(dict_type=MultiDict)
config1.read('ini_file.ini')

"textsize" is the option named twice in my config file.  When I run the code, I get a DuplicateOptionError for "textsize".  No one seems to know how to solve this, so it could be a bug.  If it's sloppy coding, I apoligize.
msg413449 - (view) Author: Cyril Jouve (Cyril Jouve) * Date: 2022-02-17 19:53
you need to pass `strict=False` to ConfigParser :


        When `strict` is True, the parser won't allow for any section or option
        duplicates while reading from a single source (file, string or
        dictionary). Default is True.
msg413456 - (view) Author: Mark Lonnemann (malonn) Date: 2022-02-17 20:58
I know, thanks though.  I just thought there was a way to do it via dict_type=.  I've read it can be done, but is complex.  I've yet to see any examples of how.
History
Date User Action Args
2022-04-11 14:59:56adminsetgithub: 90926
2022-02-17 20:58:48malonnsetmessages: + msg413456
2022-02-17 19:53:35Cyril Jouvesetnosy: + Cyril Jouve
messages: + msg413449
2022-02-16 17:17:23malonncreate