Fix StopIteration in restore_yaml_comments when default_data ends with a comment or blank line#196
Open
gaoflow wants to merge 2 commits into
Open
Conversation
…h comment or blank line `restore_yaml_comments` used a bare `next()` call inside an inner `while True` loop to scan past comment/blank lines in `default_data`. If the file ended on a comment or blank line (very common — editors often append a trailing newline after the last comment), `next()` raised `StopIteration`, which propagated out of the function and crashed any caller, including `Configuration.dump()`. Fix: catch `StopIteration`, set `line = None`, and break out of both the inner and outer loops so the orphaned comment is silently discarded rather than crashing the caller. Fixes beetbox#148.
|
Thank you for the PR! The changelog has not been updated, so here is a friendly reminder to check if you need to add an entry. |
Author
|
Added a changelog entry under Verification:
|
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.
What
restore_yaml_commentsinconfuse/yaml_util.pyuses a barenext()call inside an innerwhile Trueloop to scan past comment or blank lines indefault_data. If the file ends on a comment or blank line — which is extremely common, since editors and most YAML files append a trailing newline after the last comment —next()raisesStopIteration, which propagates out of the function entirely and crashes any caller.The most visible symptom:
Configuration.dump()raisesStopIterationwhen the application'sconfig_default.yamlhas an empty or comment-only last line (issue #148).Root cause
Fix
Wrap the
next()call in atry/except StopIteration, setline = None, and break out of both the inner and outer loops. The trailing comment is silently discarded (there is no following key to attach it to), matching the documented behaviour for orphaned comments.Tests
Three regression tests added to
test/test_yaml.py:test_trailing_comment_does_not_raise— default_data ends with# trailing commenttest_trailing_blank_line_does_not_raise— default_data ends with\n\ntest_comments_still_restored_before_key— verifies normal comment restoration still worksAll 295 existing tests continue to pass.
Closes #148.
This pull request was prepared with the assistance of AI, under my direction and review.