Skip to content

Explicitly close files to prevent resource leak#175

Merged
ryanmrichard merged 1 commit intoCMakePP:masterfrom
Stephen0512:master
Jan 23, 2026
Merged

Explicitly close files to prevent resource leak#175
ryanmrichard merged 1 commit intoCMakePP:masterfrom
Stephen0512:master

Conversation

@Stephen0512
Copy link
Copy Markdown
Contributor

Description
This pull request fixes a file-handling issue in the tests related to redirecting sys.stdout. The tests previously redirected output by assigning sys.stdout to open(os.devnull, "w") without ensuring the file handle was properly closed later on. While Python's garbage collector will eventually close the file once it is no longer referenced, it is generally better practice to explicitly close files or use a context manager.

This change replaces the direct use of open() with a context-managed approach using contextlib.redirect_stdout, ensuring that the file is always closed and sys.stdout is safely restored. The behavior of the tests remains unchanged. The issue was identified during an ongoing research project.

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Jan 23, 2026

CLA assistant check
All committers have signed the CLA.

@Stephen0512 Stephen0512 changed the title explicitly close file Explicitly close files to prevent resource leak Jan 23, 2026
Copy link
Copy Markdown
Collaborator

@ryanmrichard ryanmrichard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! These changes look great to me.

@ryanmrichard ryanmrichard merged commit 65f17f0 into CMakePP:master Jan 23, 2026
15 checks passed
@github-actions
Copy link
Copy Markdown

🚀 [bumpr] Bumped!
New version:v1.1.13
Changes:v1.1.12...v1.1.13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants