refactor: implement context managers for deterministic file resource cleanup #13151
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.
Proposed changes:
This PR refactors file I/O operations across utility scripts and test suites to ensure safe resource management.
Scope of Changes
open()/close()calls with Python context managers (withstatements). This ensures file descriptors are automatically closed even if exceptions occur during execution, preventing potential resource leaks.test_convert_featurizer.pyto utilizepathlibobjects (tmp_path) rather than manual string manipulation and file creation. This improves test harness readability and cross-platform compatibility.OSErrorrisks inpattern_utils.pyby ensuring the file handle is scoped correctly within the try/except block.Motivation
Leaving file handles open relies on the garbage collector for cleanup, which is non-deterministic. Explicit context management is strictly required for long-running processes (like model training) to avoid hitting system file limits.
Status (please check what you already did):
black(please check Readme for instructions)