Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python: Use a context manager for opening files (SIM115) to solve some ResourceWarnings #4224

Merged
merged 13 commits into from
Jan 16, 2025

Conversation

echoix
Copy link
Member

@echoix echoix commented Aug 25, 2024

This PR starts fixing some ResourceWarnings. I'm starting small, I am juggling with 4 sets of changes that are tested individually and one on top of the others, plus on top of my coverage branch. There are at least 368 places where a context manager should be used to open and close files. It shouldn't be done in one PR.

This first set is ready to be merged. It already solves some resource warnings appearing in macOS Pytest jobs that I was seeing.

Later on, I will merge some changes, but will explicitly make sure that changes in an input/output pair are in separate PRs. For example, if stds_export is changed, stds_import will be in another one. For changes in gunittest, it will be on their own. That way, it will help isolate the possibility of changing the code and the code treating the expected value, and will help make sure the existing code works well with only this part of the changes.

@github-actions github-actions bot added temporal Related to temporal data processing Python Related code is in Python libraries module labels Aug 25, 2024
temporal/t.rast.what/t.rast.what.py Outdated Show resolved Hide resolved
temporal/t.rast.what/t.rast.what.py Outdated Show resolved Hide resolved
python/grass/script/db.py Outdated Show resolved Hide resolved
temporal/t.rast.what/t.rast.what.py Outdated Show resolved Hide resolved
temporal/t.rast.what/t.rast.what.py Outdated Show resolved Hide resolved
echoix and others added 2 commits August 26, 2024 17:35
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@echoix echoix requested a review from wenzeslaus September 7, 2024 01:48
Copy link
Member

@wenzeslaus wenzeslaus left a comment

Choose a reason for hiding this comment

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

Most of the PR looks good and can be merged, but the t.rast.what code is a lot of review for the change it brings and there is a problem with closing sys.stdout, so I suggest removing that file from this PR.

temporal/t.rast.what/t.rast.what.py Outdated Show resolved Hide resolved
@echoix
Copy link
Member Author

echoix commented Sep 18, 2024

I'll take a new look at my next available coding time.

Since that time, I've found other ways to not have to indent everything, by moving the static contents up and keeping the part that has to be indented small. Otherwise, it might be easier to look at the diff side by side rather than inline, and VS Code with whitespace highlighting is really good at seing that there are literally no change apart 4 spaces + black.

@echoix
Copy link
Member Author

echoix commented Oct 17, 2024

This PR is not ready yet, there's still the stdout handling to go through

@echoix echoix force-pushed the fix-resourcewarning-1 branch from 55a2f38 to ecab2a0 Compare December 31, 2024 15:12
@echoix
Copy link
Member Author

echoix commented Dec 31, 2024

This PR is not ready yet, there's still the stdout handling to go through

Ready now to review. See the review comment thread for why contextlib.nullcontext is used for sys.stdout, allowing to use the same shape of code (with a with context handler) when using a file or stdout.

@echoix echoix enabled auto-merge (squash) January 16, 2025 03:20
@echoix echoix merged commit ca25eb3 into OSGeo:main Jan 16, 2025
26 checks passed
@github-actions github-actions bot added this to the 8.5.0 milestone Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libraries module Python Related code is in Python temporal Related to temporal data processing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants