Closed a0a5407d-5e80-4b62-b69c-5266c4fbbd48 closed 9 years ago
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)
This is a reasonable feature request. Overriding __getattr__ doesn't look like the best solution, though. Let me think this over.
Overriding __getattr__ doesn't look like the best solution
Another idea would be to allow the proxy class to be selectable, but this would require the user to do much more coding for this simple thing...
I believe a proxy should be dynamic enough to avoid having to duplicate every function definition.
There are several reasons why get*()
methods are redefined on the section proxy. First of all, explicit is better than implicit. Secondly, the order of arguments is different: parser.get()
has the fallback argument as the last (and keyword-only), whereas parser['section'].get()
behaves like a mapping. You can do parser['section'].get('key', 'some-fallback-value')
and parser['section'].get('no-such-key')
returns None instead of raising NoOptionError
.
This makes it difficult to automagically support parser get*()
methods on the section proxy. This is why I decided to adapt a different mechanism: register a converter and a getter is automatically available:
>>> cp = ConfigParser()
>>> cp.converters['list'] = lambda value: value.strip().split()
>>> cp.getlist('section', 'l')
['a', 'b', 'c']
>>> cp['section'].getlist('l')
['a', 'b', 'c']
>>> cp.getdict('section', 'd')
Traceback (most recent call last):
...
AttributeError: 'ConfigParser' object has no attribute 'getdict'
>>> cp['section'].getdict('d')
Traceback (most recent call last):
...
AttributeError: 'ConfigParser' object has no attribute 'getdict'
This ensures that you can easily add new converters in subclasses or single instances and that the parser-level API and section-level API work like they should. This also makes implementing custom getters easier since there's no logic involved besides the conversion. And if you happen to need custom logic anyway, you can register a converter that is a callable class.
Was the patch accepted yet? Looks good to me
ping?
The reason I didn't commit that patch before was that I wasn't sure whether making this change wouldn't create any unexpected backwards incompatibility.
In fact, if I committed the patch as is, it would. I solved this by leaving getint, getfloat and getboolean on the parser class and keeping _get in use.
The new implementation also automatically covers get*() methods added on subclasses, no need to use converters= in that case.
New changeset 2c46a4ded259 by Łukasz Langa in branch 'default': Closes bpo-18159: ConfigParser getters not available on SectionProxy https://hg.python.org/cpython/rev/2c46a4ded259
New changeset 5eb95d41ee43 by Łukasz Langa in branch 'default': Closes bpo-18159: ConfigParser getters not available on SectionProxy https://hg.python.org/cpython/rev/5eb95d41ee43
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields: ```python assignee = 'https://github.com/ambv' closed_at =
created_at =
labels = ['type-feature', 'library']
title = 'ConfigParser getters not available on SectionProxy'
updated_at =
user = 'https://github.com/jbvsmo'
```
bugs.python.org fields:
```python
activity =
actor = 'python-dev'
assignee = 'lukasz.langa'
closed = True
closed_date =
closer = 'python-dev'
components = ['Library (Lib)']
creation =
creator = 'JBernardo'
dependencies = []
files = ['30704']
hgrepos = []
issue_num = 18159
keywords = ['patch']
message_count = 9.0
messages = ['190767', '190772', '190810', '191891', '193207', '216731', '226900', '226907', '226908']
nosy_count = 3.0
nosy_names = ['lukasz.langa', 'python-dev', 'JBernardo']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue18159'
versions = ['Python 3.5']
```