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

Conversation

PalmtopTiger
Copy link
Contributor

@PalmtopTiger PalmtopTiger commented Nov 24, 2024

The fix is based on the code from the _handle_header method. Except that I removed the part related to checking for section duplication because an unnamed section cannot be repeated within a file, and because UNNAMED_SECTION is an object and elements_added is defined as set[str].

@PalmtopTiger PalmtopTiger requested a review from jaraco as a code owner November 24, 2024 15:26
Copy link

cpython-cla-bot bot commented Nov 24, 2024

All commit authors signed the Contributor License Agreement.
CLA signed

Copy link
Member

@jaraco jaraco left a comment

Choose a reason for hiding this comment

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

This is great. Nice work on adding the regression test. As I review it, I realize there are some ways that we can make this code even more safe and readable, but those concerns are outside the scope of the reported bug, so I'll deal with those separately. Just one open question.

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

Comment on lines 1109 to 1114
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.

@PalmtopTiger
Copy link
Contributor Author

There is still a problem with elements_added : set[str]. Should I change this line to elements_added : set[str | _UnnamedSection]?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants