Skip to content

ConfigParser replaces unnamed section on every read call #127096

Closed

Description

@PalmtopTiger

Bug report

Bug description:

from configparser import ConfigParser

config = ConfigParser(allow_unnamed_section=True)
config.read(['first.ini', 'second.ini'])

# now the unnamed section contains values ​​only from the second file:
print(config._sections)
# {
# <UNNAMED_SECTION>: {'second_unnamed_option1': '1', 'second_unnamed_option2': '2'},
# 'first_section': {'first_section_option1': '1', 'first_section_option2': '2'},
# 'second_section': {'second_section_option1': '1', 'second_section_option2': '2'}
# }

I think the problem is somewhere here. The unnamed section is recreated on every call.
first.ini.txt
second.ini.txt

CPython versions tested on:

3.13

Operating systems tested on:

Windows

Linked PRs

Activity

  1. sobolevn commented on Nov 21, 2024

    @sobolevn
    Member

    Please, provide the contents of first.ini and second.ini, so it would be easier to reproduce the issue.

  2. PalmtopTiger commented on Nov 21, 2024

    @PalmtopTiger
    ContributorAuthor

    Done

  3. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 23, 2024
  4. ggggjlgl commented on Nov 24, 2024

    @ggggjlgl

    Due to network issues, I am unable to clone the cpython repository. The following are for reference:
    file: configparser.py
    class: class RawConfigParser(MutableMapping)
    method: def _handle_rest(self, st, line, fpname)
    modify to:

        def _handle_rest(self, st, line, fpname):
            # a section header or option header?
            if self._allow_unnamed_section and st.cursect is None:
                st.sectname = UNNAMED_SECTION
                if self._sections.get(st.sectname) is None:
                    st.cursect = self._dict()
                    self._sections[st.sectname] = st.cursect
                else:
                    st.cursect = self._sections[st.sectname]
                self._proxies[st.sectname] = SectionProxy(self, st.sectname)
                st.elements_added.add(st.sectname)
                ...  # Omit subsequent code
  5. Agent-Hellboy commented on Nov 24, 2024

    @Agent-Hellboy
    Contributor

    Due to network issues, I am unable to clone the cpython repository. The following are for reference:

    I am working on adding something in configparser, I can take a look and add a PR if you want.

  6. PalmtopTiger commented on Nov 24, 2024

    @PalmtopTiger
    ContributorAuthor

    I created the PR myself. Thanks to everyone for your help!

  7. ggggjlgl commented on Nov 25, 2024

    @ggggjlgl

    Due to network issues, I am unable to clone the cpython repository. The following are for reference:

    I am working on adding something in configparser, I can take a look and add a PR if you want.

    Thank you. PalmtopTiger created the PR.

  8. added 2 commits that reference this issue on Jan 26, 2025
  9. jaraco commented on Feb 2, 2025

    @jaraco
    Member

    I've set the backport to 3.13 to merge automatically. I believe that closes this issue.

  10. added a commit that references this issue on Feb 2, 2025
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-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions