docker_login: Use with statement for accessing files (#64382) (#64392) (#64524)

* docker_login: Use with statement for accessing files (#64382)

* Update changelogs/fragments/64382-docker_login-fix-invalid-json.yml

Co-Authored-By: Felix Fontein <felix@fontein.de>
(cherry picked from commit 52c4c1b00d)
This commit is contained in:
Felix Fontein 2019-11-12 03:01:44 +01:00 committed by Matt Davis
parent a0ec2976b2
commit afc4218ac4
2 changed files with 6 additions and 2 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- "docker_login - Use ``with`` statement when accessing files, to prevent that invalid JSON output is produced."

View file

@ -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()