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 <felix@fontein.de>
This commit is contained in:
Benjamin Leber 2019-11-06 09:40:30 +01:00 committed by Felix Fontein
parent 9a8d73456c
commit 52c4c1b00d
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()