Skip to content

ConfigParser replaces unnamed section on every read call #127096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
PalmtopTiger opened this issue Nov 21, 2024 · 7 comments
Closed

ConfigParser replaces unnamed section on every read call #127096

PalmtopTiger opened this issue Nov 21, 2024 · 7 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@PalmtopTiger
Copy link
Contributor

PalmtopTiger commented Nov 21, 2024

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

@PalmtopTiger PalmtopTiger added the type-bug An unexpected behavior, bug, or error label Nov 21, 2024
@sobolevn
Copy link
Member

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

@PalmtopTiger
Copy link
Contributor Author

Done

@picnixz picnixz added the stdlib Python modules in the Lib dir label Nov 23, 2024
@ggggjlgl
Copy link

ggggjlgl commented Nov 24, 2024

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

@Agent-Hellboy
Copy link
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.

@PalmtopTiger
Copy link
Contributor Author

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

@ggggjlgl
Copy link

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.

jaraco pushed a commit that referenced this issue Jan 26, 2025
…ser (#127228)

* Do not recreate unnamed section on every read in ConfigParser

* Remove duplicate section creation code
jaraco pushed a commit to jaraco/cpython that referenced this issue Feb 2, 2025
… in ConfigParser (pythonGH-127228)

* Do not recreate unnamed section on every read in ConfigParser

* Remove duplicate section creation code
(cherry picked from commit 914c232)

Co-authored-by: Andrey Efremov <duxus@yandex.ru>
@jaraco
Copy link
Member

jaraco commented Feb 2, 2025

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

@jaraco jaraco closed this as completed Feb 2, 2025
jaraco added a commit that referenced this issue Feb 2, 2025
…nfigParser (GH-127228) (#129593)

* Do not recreate unnamed section on every read in ConfigParser

* Remove duplicate section creation code
(cherry picked from commit 914c232)

Co-authored-by: Andrey Efremov <duxus@yandex.ru>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

6 participants