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

Use context managers for some file handling in system modules. #65222

Closed
wants to merge 1 commit into from

Conversation

atombrella
Copy link
Contributor

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
  • Bugfix Pull Request
COMPONENT NAME

system, core

ADDITIONAL INFORMATION

Context managers are more Pythonic for dealing with resources, like files or connections.

@ansibot ansibot added affects_2.10 This issue/PR affects Ansible v2.10 bug This issue/PR relates to a bug. community_review In order to be merged, this PR must follow the community review workflow. module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. solaris Solaris community support:community This issue/PR relates to code supported by the Ansible community. system System category labels Nov 23, 2019
@ansibot
Copy link
Contributor

ansibot commented Nov 23, 2019

The test ansible-test sanity --test pylint [explain] failed with 1 error:

lib/ansible/modules/system/solaris_zone.py:240:64: blacklisted-name: Black listed name "_"

click here for bot help

@ansibot ansibot added ci_verified Changes made in this PR are causing tests to fail. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed community_review In order to be merged, this PR must follow the community review workflow. labels Nov 23, 2019
@ansibot ansibot added core_review In order to be merged, this PR must follow the core review workflow. support:core This issue/PR relates to code supported by the Ansible Engineering Team. and removed ci_verified Changes made in this PR are causing tests to fail. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Nov 23, 2019
@ansibot ansibot added community_review In order to be merged, this PR must follow the community review workflow. and removed core_review In order to be merged, this PR must follow the core review workflow. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Nov 23, 2019
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Dec 3, 2019
Copy link
Contributor

@felixfontein felixfontein left a 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())
Copy link
Contributor

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())
Copy link
Contributor

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())
Copy link
Contributor

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())
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here.

@ansibot ansibot removed the needs_triage Needs a first human triage before being processed. label Dec 4, 2019
@atombrella
Copy link
Contributor Author

@felixfontein Any reason why this has not been merged yet?

@felixfontein
Copy link
Contributor

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...

@ansibot ansibot added needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html and removed community_review In order to be merged, this PR must follow the community review workflow. labels Mar 26, 2020
@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Mar 26, 2020
@ansibot ansibot added collection Related to Ansible Collections work collection:community.general labels Apr 29, 2020
@ansibot ansibot added pre_azp This PR was last tested before migration to Azure Pipelines. and removed stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. labels Dec 6, 2020
@jborean93
Copy link
Contributor

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.

@jborean93 jborean93 closed this Jan 26, 2022
@atombrella atombrella deleted the solaris_ctx_man branch January 27, 2022 08:42
@ansible ansible locked and limited conversation to collaborators Feb 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.10 This issue/PR affects Ansible v2.10 bug This issue/PR relates to a bug. collection:community.general collection Related to Ansible Collections work module This issue/PR relates to a module. needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. pre_azp This PR was last tested before migration to Azure Pipelines. solaris Solaris community support:community This issue/PR relates to code supported by the Ansible community. support:core This issue/PR relates to code supported by the Ansible Engineering Team. system System category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants