Skip to content

document ConfigParser behaviour when a file has same section multiple times #46457

Closed

Description

@draghuram
mannequin
BPO 2204
Nosy @birkenfeld, @orsenthil, @ambv
Superseder
  • bpo-9452: configparser support for reading from strings and dictionaries
  • Files
  • no-dups-configparser.diff: Patch for throwing an exception
  • 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-08-03.21:54:10.466>
    created_at = <Date 2008-02-28.22:11:59.534>
    labels = ['easy', 'type-bug', 'library', 'docs']
    title = 'document ConfigParser behaviour when a file has same section multiple times'
    updated_at = <Date 2010-08-03.21:54:10.465>
    user = 'https://bugs.python.org/draghuram'

    bugs.python.org fields:

    activity = <Date 2010-08-03.21:54:10.465>
    actor = 'eric.araujo'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2010-08-03.21:54:10.466>
    closer = 'eric.araujo'
    components = ['Documentation', 'Library (Lib)']
    creation = <Date 2008-02-28.22:11:59.534>
    creator = 'draghuram'
    dependencies = []
    files = ['12938']
    hgrepos = []
    issue_num = 2204
    keywords = ['patch', 'easy']
    message_count = 9.0
    messages = ['63109', '63110', '63139', '81145', '81594', '92055', '111417', '111527', '112698']
    nosy_count = 9.0
    nosy_names = ['georg.brandl', 'orsenthil', 'draghuram', 'ygingras', 'trash80', 'honesthacker', 'docs@python', 'BreamoreBoy', 'lukasz.langa']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '9452'
    type = 'behavior'
    url = 'https://bugs.python.org/issue2204'
    versions = ['Python 3.0']

    Activity

    1. draghuram commented on Feb 28, 2008

      draghurammannequin
      MannequinAuthor

      ConfigParser.add_section() raises DuplicateSectionError if add_section()
      is called with the name of a section that is already present. How ever,
      if a section is present multiple times in a file, readfp() does not
      raise any exception. Instead, the sections are "combined" with values in
      latter sections overwriting previous values (if any). This should be
      documented in readfp(). I tried few alternatives but the sentences don't
      look right. I am creating the issue in the hope that some one else may
      give it a try while I continue to come up with a doc patch.

    2. added
      docs
      type-bug
      on Feb 28, 2008
    3. orsenthil commented on Feb 28, 2008

      @orsenthil
      Member

      Should this 'undesirable' behavior be documented? My thoughts are
      a) All the key,value pairs in the named section be retrived from entire
      file. Just appending the dict.
      Otherwise,
      b) An Exception can be thrown if it encounters an invalid Configfile
      having multiple same sections which can cause problem, but I cannot
      readily think of one such case.

      What do others think?

      Thanks,
      Senthil

    4. draghuram commented on Feb 29, 2008

      draghurammannequin
      MannequinAuthor

      Should this 'undesirable' behavior be documented? My thoughts are

      Document should definitely reflect the code whether the behaviour is
      desirable or not.

      a) All the key,value pairs in the named section be retrived from entire
      file. Just appending the dict.

      Isn't this what is happening now unless I am not following you?

      Otherwise,
      b) An Exception can be thrown if it encounters an invalid Configfile
      having multiple same sections which can cause problem, but I cannot
      readily think of one such case.

      I have no problem with throwing an exception in case of duplicate
      sections but I don't think that can be done by default (it may break
      some programs).

    5. ygingras commented on Feb 4, 2009

      ygingrasmannequin
      Mannequin

      The attached patch is a proof of concept for throwing an exception.

      As discussed on the mailing list 1, it has some shortcomings that
      should be addressed before it is merged.

    6. draghuram commented on Feb 10, 2009

      draghurammannequin
      MannequinAuthor

      The attached patch is a proof of concept for throwing an exception.

      If it is not too much of a problem, can you upload the patch to
      http://codereview.appspot.com? Reviewing there is simpler. Also, you
      will have to include some test cases in the patch.

      As discussed on the mailing list 1, it has some shortcomings that
      should be addressed before it is merged.

      I think that the builder interface already has correct behaviour in
      that duplicate sections are not allowed. So you can have
      'unique_sections' control only parser interface and can set it to
      False by default (to be compatible with current behaviour).

      Correct discussion link:
      http://mail.python.org/pipermail/python-dev/2009-February/085838.html

    7. added
      stdlib
      on Apr 22, 2009
    8. trash80 commented on Aug 29, 2009

      trash80mannequin
      Mannequin

      I am using configparser after combining 3 or so ini files, I would like
      to merge similar sections rather than throwing an exception. Right now
      I have to make sure the ini files all have unique sections, but I would
      like to able to use same section names, then combine the files and have
      the sections merged. Even merging the variables within sections would
      be nice.

      Throwing an exception, imho, is no the right direction. Even in same
      name sections are in the same file, the variables where introduced for a
      reason.

      Or maybe a trigger option ie action=merge or action=flag.

    9. ambv commented on Jul 24, 2010

      @ambv
      Contributor

      I personally would keep the current behaviour so that:

      • creating a duplicate section programatically invokes an error
      • reading a file which has duplicate sections causes an implicit merge without any exceptions

      This is quite understandable, more so when you consider how ConfigParser merges configuration from multiple sources as well.

      Brett, the only thing I guess we could do here is to adjust the documentation so it's more clear on the default behaviour.

    10. BreamoreBoy commented on Jul 25, 2010

      BreamoreBoymannequin
      Mannequin

      @Łukasz: could you provide a patch that clarifies the default behaviour.

    11. ambv commented on Aug 3, 2010

      @ambv
      Contributor

      This bug is superseded by bpo-9452 where the patch includes a strict= argument to init which enables checking for section duplicates when reading from a single source.

    12. 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

      docseasystdlibtype-bug

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions