[New-bugs-announce] [issue18159] ConfigParser getters not available on SectionProxy

João Bernardo report at bugs.python.org
Fri Jun 7 19:43:50 CEST 2013


New submission from João Bernardo:

The configparser.RawConfigParser class implements some `get` methods: 
    get, getint, getfloat, getboolean

but if any of these get overridden on a subclass(with other arguments) or new ones are added (e.g. getlist), there's no way a SectionProxy instance will be able to use them.


    class DemoParser(ConfigParser):
        def getlist(self, section, option):
            return self.get(section, option).split()

    parser = DemoParser()
    parser.read(some_file)

    # These 2 lines should be equivalent, like "getint", but the
    # second doesn't work because of the SectionProxy instance
    parser.getlist('some_section', 'foo')
    parser['some_section'].getlist('foo')



Reading the code, for SectionProxy, it redefines all the get* methods from RawConfigParser, and that looks pretty bad...

A more elegant approach would be to fetch the function on the parser instance and bound to the section name with `lambda` or `functools.partial`... Something like:


    class SectionProxy(...):
        ...
        
        def __getattr__(self, attr):
            if not attr.startswith('get'):
                raise AttributeError(attr)
            fn = getattr(self._parser, attr)
            return lambda *args, **kw: fn(self._name, *args, **kw)

----------
components: Library (Lib)
messages: 190767
nosy: JBernardo
priority: normal
severity: normal
status: open
title: ConfigParser getters not available on SectionProxy
type: behavior
versions: Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue18159>
_______________________________________


More information about the New-bugs-announce mailing list