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>
pull/64491/head
Benjamin Leber 5 years ago committed by Felix Fontein
parent 9a8d73456c
commit 52c4c1b00d

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

@ -259,7 +259,8 @@ class LoginManager(DockerBaseClass):
def write_config(self, path, config): def write_config(self, path, config):
try: 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: except Exception as exc:
self.fail("Error: failed to write config to %s - %s" % (path, str(exc))) self.fail("Error: failed to write config to %s - %s" % (path, str(exc)))
@ -277,7 +278,8 @@ class LoginManager(DockerBaseClass):
try: try:
# read the existing config # read the existing config
config = json.load(open(path, "r")) with open(path, "r") as file:
config = json.load(file)
except ValueError: except ValueError:
self.log("Error reading config from %s" % (path)) self.log("Error reading config from %s" % (path))
config = dict() config = dict()

Loading…
Cancel
Save