Use with open() as: to close file handles#7575
Use with open() as: to close file handles#7575cclauss wants to merge 1 commit intoArduPilot:masterfrom
with open() as: to close file handles#7575Conversation
with open() as: to close file handles
peterbarker
left a comment
There was a problem hiding this comment.
I don't have a problem with adding SIM in. But the title on this PR is clear and the patches don't match it.
Please have your PRs do one thing and do it well.
|
SIM and flake8 hate each other, so let's focus only on SIM115. |
|
These patches all look correct. How were they tested? We can not afford to break this stuff, and if it's wrong it's definitely going to break stuff. |
|
Can you please recommend a test plan? In all cases, we read or write whole files in a single |
|
@tpwrules Your review, please. |
Work out how to run each script so that the changes lines are crossed. Run the script. Make sure the script still does what it is supposed to do. Incidentally, why are we not changing all of these to use |
Like:
%
ruff rule SIM115open-file-with-context-handler (SIM115)
Derived from the flake8-simplify linter.
What it does
Checks for cases where files are opened (e.g., using the builtin
open()function)without using a context manager.
Why is this bad?
If a file is opened without a context manager, it is not guaranteed that
the file will be closed (e.g., if an exception is raised), which can cause
resource leaks. The rule detects a wide array of IO calls where context managers
could be used, such as
open,pathlib.Path(...).open(),tempfile.TemporaryFile()or
tarfile.TarFile(...).gzopen().Example
Use instead:
References
open