-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
st.cursect = self._dict() | ||
self._sections[st.sectname] = st.cursect | ||
self._proxies[st.sectname] = SectionProxy(self, st.sectname) | ||
st.elements_added.add(st.sectname) |
There was a problem hiding this comment.
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.
Lib/configparser.py
Outdated
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There is still a problem with |
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].