Repo File Sync: Update DocBuild File is not always closed#241
Open
odaysec wants to merge 1 commit intomicrosoft:masterfrom
Open
Repo File Sync: Update DocBuild File is not always closed#241odaysec wants to merge 1 commit intomicrosoft:masterfrom
odaysec wants to merge 1 commit intomicrosoft:masterfrom
Conversation
Fix issue problem the file opens in `MakeYml` should be wrapped in `with` context managers so they are always closed, even if an exception occurs. We still need to preserve current behavior: copy all lines from the base YAML file into the output file, then parse the base YAML from the beginning to check for an `extra` key, raise if present, and finally keep an open handle to the output YAML in `self.Yml` for later use by `CloseYml`. The safest change is: - Open `self.YmlFilePathBase` with `with open(..., 'r') as ymlbase:` so that it is always closed after use. - Within that `with`, open `self.YmlFilePathOut` with another `with open(..., 'w') as yamlout:` and perform the line copy and YAML parse while both files are open and automatically managed. - After the nested `with` blocks, reopen `self.YmlFilePathOut` in append mode (`'a'`) and assign that handle to `self.Yml`. This preserves the design where `self.Yml` stays open beyond `MakeYml` and will be closed in `CloseYml`, without risking leaks if an exception occurs during the initial processing. - Remove the explicit `ymlbase.close()` and the assignment `self.Yml = yamlout` inside `MakeYml`, since those handles are now managed by context managers and a fresh handle is created for `self.Yml`.
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.
Fix issue problem the file opens in
MakeYmlshould be wrapped inwithcontext managers so they are always closed, even if an exception occurs. We still need to preserve current behavior: copy all lines from the base YAML file into the output file, then parse the base YAML from the beginning to check for anextrakey, raise if present, and finally keep an open handle to the output YAML inself.Ymlfor later use byCloseYml.The safest change is:
self.YmlFilePathBasewithwith open(..., 'r') as ymlbase:so that it is always closed after use.with, openself.YmlFilePathOutwith anotherwith open(..., 'w') as yamlout:and perform the line copy and YAML parse while both files are open and automatically managed.withblocks, reopenself.YmlFilePathOutin append mode ('a') and assign that handle toself.Yml. This preserves the design whereself.Ymlstays open beyondMakeYmland will be closed inCloseYml, without risking leaks if an exception occurs during the initial processing.ymlbase.close()and the assignmentself.Yml = yamloutinsideMakeYml, since those handles are now managed by context managers and a fresh handle is created forself.Yml.Reading and writing files
The with statement, The try statement
Python PEP 343 The "with" Statement
CWE-772