Skip to content
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

gh-127096: Do not recreate unnamed section on every read in ConfigParser #127228

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions Lib/configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,10 +1106,12 @@ 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
st.cursect = self._dict()
self._sections[st.sectname] = st.cursect
self._proxies[st.sectname] = SectionProxy(self, st.sectname)
st.elements_added.add(st.sectname)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice this line gets removed. Why remove it?

Copy link
Contributor Author

@PalmtopTiger PalmtopTiger Nov 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"st.elements_added.add(st.sectname)" is removed because it is responsible for checking for section duplication. But an unnamed section cannot be duplicated within a file. When reading the next file, the parser is reset to its initial state and the elements_added set is cleared. Moreover, there was no duplication check in the original code, only adding to the set. Also, UNNAMED_SECTION is an object, but elements_added is defined as set[str].
The lines 1109-1111 have been moved under the "else" statement since they should only be executed when the section is created.

Comment on lines -1109 to -1112
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice now that these four lines are essentially a copy-paste of lines 1134-1137. We should consolidate those too, to avoid this logic drifting. I'll plan to do that before or after this PR.

if st.sectname in self._sections:
st.cursect = self._sections[st.sectname]
else:
st.cursect = self._dict()
self._sections[st.sectname] = st.cursect
self._proxies[st.sectname] = SectionProxy(self, st.sectname)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this change, _handle_rest is starting to handle too much. In fact, it was probably already handling too much. Already, we can see comments (orig line 1106) that have drifted from their original purpose when this functionality came into play. I'm thinking we should extract a method for the unnamed section handling. I'll plan to do that before or after this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found a simple way to eliminate duplicate code. Added a commit.


st.indent_level = st.cur_indent_level
# is it a section header?
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_configparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2174,6 +2174,15 @@ def test_disabled_error(self):
with self.assertRaises(configparser.UnnamedSectionDisabledError):
configparser.ConfigParser().add_section(configparser.UNNAMED_SECTION)

def test_multiple_configs(self):
cfg = configparser.ConfigParser(allow_unnamed_section=True)
cfg.read_string('a = 1')
cfg.read_string('b = 2')

self.assertEqual([configparser.UNNAMED_SECTION], cfg.sections())
self.assertEqual('1', cfg[configparser.UNNAMED_SECTION]['a'])
self.assertEqual('2', cfg[configparser.UNNAMED_SECTION]['b'])


class MiscTestCase(unittest.TestCase):
def test__all__(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Do not recreate unnamed section on every read in
:class:`configparser.ConfigParser`. Patch by Andrey Efremov.
Loading