Skip to content

ConfigParser can't interpolate values from other sections #54085

Closed

Description

@asolovyov
mannequin
BPO 9876
Nosy @freddrake, @merwok, @ambv
Superseder
  • bpo-10499: Modular interpolation in configparser
  • 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:

    assignee = None
    closed_at = <Date 2010-11-22.03:20:05.036>
    created_at = <Date 2010-09-16.16:38:04.774>
    labels = ['type-feature', 'library']
    title = "ConfigParser can't interpolate values from other sections"
    updated_at = <Date 2010-11-22.03:20:05.034>
    user = 'https://bugs.python.org/asolovyov'

    bugs.python.org fields:

    activity = <Date 2010-11-22.03:20:05.034>
    actor = 'lukasz.langa'
    assignee = 'none'
    closed = True
    closed_date = <Date 2010-11-22.03:20:05.036>
    closer = 'lukasz.langa'
    components = ['Library (Lib)']
    creation = <Date 2010-09-16.16:38:04.774>
    creator = 'asolovyov'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 9876
    keywords = []
    message_count = 3.0
    messages = ['116573', '116609', '122080']
    nosy_count = 4.0
    nosy_names = ['fdrake', 'eric.araujo', 'lukasz.langa', 'asolovyov']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = None
    status = 'closed'
    superseder = '10499'
    type = 'enhancement'
    url = 'https://bugs.python.org/issue9876'
    versions = ['Python 3.2']

    Activity

    1. asolovyov commented on Sep 16, 2010

      asolovyovmannequin
      MannequinAuthor

      Often it is useful to access some variable in other section for interpolation needs: for example, parent directory declared in common section could be used in configuration of certain components. Included patch can fix that (using syntax 'section.variable'), is there any chances that it could be applied? :)

      --- ConfigParser.old.py	2010-09-16 19:20:27.000000000 +0300
      +++ ConfigParser.py	2010-09-16 19:22:33.000000000 +0300
      @@ -585,7 +585,7 @@
                   if "%(" in value:
                       value = self._KEYCRE.sub(self._interpolation_replace, value)
                       try:
      -                    value = value % vars
      +                    value = value % _Context(self, section)
                       except KeyError, e:
                           raise InterpolationMissingOptionError(
                               option, section, rawval, e.args[0])
      @@ -667,3 +667,15 @@
                   raise ValueError("invalid interpolation syntax in %r at "
                                    "position %d" % (value, m.start()))
               ConfigParser.set(self, section, option, value)
      +
      +
      +class _Context(object):
      +    def __init__(self, config, section):
      +        self.config = config
      +        self.section = section
      +
      +    def __getitem__(self, key):
      +        if '.' in key:
      +            return self.config.get(*key.split('.'))
      +        else:
      +            return self.config.get(self.section, key)
    2. added
      stdlibStandard Library Python modules in the Lib/ directory
      type-featureA feature request or enhancement
      on Sep 16, 2010
    3. merwok commented on Sep 16, 2010

      @merwok
      Member

      Stable versions don’t get new features, only bug fixes.

    4. ambv commented on Nov 22, 2010

      @ambv
      Contributor

      Hopefully a form of this feature will be introduced as part of bpo-10499. The discussion goes on there.

    5. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions