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
Use context managers for some file handling in system modules. #65222
Conversation
The test
|
840f9a4
to
f5b15c0
Compare
f5b15c0
to
9f6505f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are minimal behavior changes, but I think these are fine. The old behavior (exceptions are not caught when they happened during file closing) was probably not intended anyway.
f.close() | ||
except Exception as e: | ||
self.module.fail_json(msg="failed to read hostname: %s" % | ||
to_native(e), exception=traceback.format_exc()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changes behavior slightly (exception is now caught when error happens during closing), but I think that's ok.
f.close() | ||
except Exception as e: | ||
self.module.fail_json(msg="failed to update hostname: %s" % | ||
to_native(e), exception=traceback.format_exc()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
f.close() | ||
except Exception as e: | ||
self.module.fail_json(msg="failed to read hostname: %s" % | ||
to_native(e), exception=traceback.format_exc()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
f.close() | ||
except Exception as e: | ||
self.module.fail_json(msg="failed to update hostname: %s" % | ||
to_native(e), exception=traceback.format_exc()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
@felixfontein Any reason why this has not been merged yet? |
TBH I somehow assumed this (or parts of it) was core-support as well. Would have also been nice if anyone else (f.ex. maintainers of the involved modules) would have bothered to take a look as well... |
Sorry for missing this PR it seems to have slipped through the cracks. We've just come across this PR when going through the old PRs and issues and this one slipped through. We are happy to review a PR against the latest code today to move to context managers, especially since we no longer need old Python support. |
SUMMARY
Follow-up to #63840 The changes in this PR shouldn't be changing semantics. And it's also much smaller, so hopefully faster to get reviewed and merged.
ISSUE TYPE
COMPONENT NAME
system, core
ADDITIONAL INFORMATION
Context managers are more Pythonic for dealing with resources, like files or connections.