[PR #9579/0de39a6f backport][stable-10] use open() as context manager (#9596)

use open() as context manager (#9579)

* use open() as context manager

* add changelog frag

(cherry picked from commit 0de39a6f47)

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
This commit is contained in:
patchback[bot] 2025-01-21 21:04:28 +01:00 committed by GitHub
parent 98d25a3e4d
commit e9b58cfc09
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 72 additions and 102 deletions

View file

@ -154,11 +154,8 @@ def main():
changed, reason = existing_line.opts.remove(opts)
if changed and not module.check_mode:
try:
f = open(path, 'wb')
with open(path, 'wb') as f:
f.write(to_bytes(crypttab, errors='surrogate_or_strict'))
finally:
f.close()
module.exit_json(changed=changed, msg=reason, **module.params)
@ -173,12 +170,9 @@ class Crypttab(object):
os.makedirs(os.path.dirname(path))
open(path, 'a').close()
try:
f = open(path, 'r')
with open(path, 'r') as f:
for line in f.readlines():
self._lines.append(Line(line))
finally:
f.close()
def add(self, line):
self._lines.append(line)