-
-
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?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
Comment on lines
-1109
to
-1112
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With this change, There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? | ||
|
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. |
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.