From 52c4c1b00dd1aac5c6154b8b734aea47984056b2 Mon Sep 17 00:00:00 2001 From: Benjamin Leber Date: Wed, 6 Nov 2019 09:40:30 +0100 Subject: [PATCH] docker_login: Use with statement for accessing files (#64382) (#64392) * docker_login: Use with statement for accessing files (#64382) * Update changelogs/fragments/64382-docker_login-fix-invalid-json.yml Co-Authored-By: Felix Fontein --- .../fragments/64382-docker_login-fix-invalid-json.yml | 2 ++ lib/ansible/modules/cloud/docker/docker_login.py | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/64382-docker_login-fix-invalid-json.yml diff --git a/changelogs/fragments/64382-docker_login-fix-invalid-json.yml b/changelogs/fragments/64382-docker_login-fix-invalid-json.yml new file mode 100644 index 00000000000..85b458102b0 --- /dev/null +++ b/changelogs/fragments/64382-docker_login-fix-invalid-json.yml @@ -0,0 +1,2 @@ +bugfixes: +- "docker_login - Use ``with`` statement when accessing files, to prevent that invalid JSON output is produced." diff --git a/lib/ansible/modules/cloud/docker/docker_login.py b/lib/ansible/modules/cloud/docker/docker_login.py index 8b805fce93a..8a2377322c2 100644 --- a/lib/ansible/modules/cloud/docker/docker_login.py +++ b/lib/ansible/modules/cloud/docker/docker_login.py @@ -259,7 +259,8 @@ class LoginManager(DockerBaseClass): def write_config(self, path, config): try: - json.dump(config, open(path, "w"), indent=5, sort_keys=True) + with open(path, "w") as file: + json.dump(config, file, indent=5, sort_keys=True) except Exception as exc: self.fail("Error: failed to write config to %s - %s" % (path, str(exc))) @@ -277,7 +278,8 @@ class LoginManager(DockerBaseClass): try: # read the existing config - config = json.load(open(path, "r")) + with open(path, "r") as file: + config = json.load(file) except ValueError: self.log("Error reading config from %s" % (path)) config = dict()