[New-bugs-announce] [issue29333] ConfigParser calls Interpolation.before_read after reading

Gereon Kaiping report at bugs.python.org
Fri Jan 20 06:50:36 EST 2017

New submission from Gereon Kaiping:

In its current implementation, `ConfigParser` calls its Interpolation's `before_read` method in the very last step of `_read`, when all properties have already been overwritten by their new uninterpolated values.

I am developing a program with modular config files: It is possible to supply several configuration files on the command line, and they are all fed through the `.read` method.

Now it would be amazing to use `read` time interpolation instead of `get` time interpolation to construct things like

outputfilename = %(outputfilename)s_extension_from_this_ini_module

By looking at the `Interpolation` class, it seems that behaviour like this should be supported by supplying a `before_read` as follows.

    def before_read(self, parser, section, option, value):
        L = []
        interpolations = parser[section]
            parser, option, L, value, section, interpolations, 1)
        return ''.join(L)

However, this is not possible, because `before_read` is only called *after* all values in the config file have been read and all old values in the ConfigParser object have been overridden.

The attached file contains a subclass of `BasicInterpolation` and a subclass of `ConfigParser` which in concert allow me to write recursive property definitions as given above.

The downside of this change is that (a) interpolation values can't be defined *after* they are used any more, and (b) it is not possible to hack this parser to accept multi-line option names as interpolations. (To me personally, both of these don't sound like useful features to begin with.)

files: earlyinterpolation.py
messages: 285902
nosy: Anaphory
priority: normal
severity: normal
status: open
title: ConfigParser calls Interpolation.before_read after reading
Added file: http://bugs.python.org/file46355/earlyinterpolation.py

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list