Fix UnboundLocalError in ConfigFileWriter._update_subattributes for empty nested sections - #10588
Open
Adityaj0 wants to merge 1 commit into
Open
Conversation
…mpty nested sections current_indent (and the loop index i) were only assigned when a sub-option line matched OPTION_REGEX. Updating a nested section that currently has no sub-keys (e.g. `s3 =` immediately followed by another [section] header, or as the last line of the file) left these variables unbound, raising UnboundLocalError instead of writing the value. Initialize current_indent to None and i to the pre-loop index so both previously-crashing cases insert the new values correctly, matching the behavior of the equivalent non-empty-section cases. Fixes aws#10587
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #10587.
ConfigFileWriter._update_subattributes(used byaws configure setto write into nested config sections likes3/s3api) only assignedcurrent_indentinside the branch where a sub-option line matchedOPTION_REGEX. When the target nested section is currently empty (no sub-keys yet — a valid config state, e.g.s3 =immediately followed by another[section], ors3 =as the last line of the file),current_indent(and, in the end-of-file case, the loop variablei) is never assigned, and the subsequent reference raisesUnboundLocalErrorinstead of writing the value.Fix
Initialize
current_indent = Noneandi = index - 1before the loop.Nonenever equalsstarting_indent(always an int), so behavior for every previously-passing case (sections with existing sub-keys) is unchanged — this only fixes the two previously-crashing empty-section paths.Test plan
test_add_to_empty_nested_stanza_followed_by_sectionandtest_add_to_empty_nested_stanza_at_eoftotests/unit/customizations/configure/test_writer.py.UnboundLocalErroragainst the pre-fix code, and pass after the fix (including correctly writing the value, not just avoiding the crash).tests/unit/customizations/configure/(109 passed) andtests/functional/configure/(33 passed) — no regressions..changes/next-release/.🤖 Generated with Claude Code