diff --git a/src/azure-cli-core/HISTORY.rst b/src/azure-cli-core/HISTORY.rst index c317544fb2a..98073964331 100644 --- a/src/azure-cli-core/HISTORY.rst +++ b/src/azure-cli-core/HISTORY.rst @@ -2,9 +2,10 @@ Release History =============== + 2.0.45 ++++++ -* Minor fixes +* Fix issue of loading empty configuration file. 2.0.44 ++++++ diff --git a/src/azure-cli-core/azure/cli/core/_session.py b/src/azure-cli-core/azure/cli/core/_session.py index d348c428133..ddf800987d4 100644 --- a/src/azure-cli-core/azure/cli/core/_session.py +++ b/src/azure-cli-core/azure/cli/core/_session.py @@ -6,21 +6,29 @@ import json import os import time + try: import collections.abc as collections except ImportError: import collections - from codecs import open as codecs_open +from knack.log import get_logger + +try: + t_JSONDecodeError = json.JSONDecodeError +except AttributeError: # in Python 2.7 + t_JSONDecodeError = ValueError + class Session(collections.MutableMapping): - '''A simple dict-like class that is backed by a JSON file. + """ + A simple dict-like class that is backed by a JSON file. All direct modifications will save the file. Indirect modifications should be followed by a call to `save_with_retry` or `save`. - ''' + """ def __init__(self, encoding=None): super(Session, self).__init__() @@ -38,7 +46,9 @@ def load(self, filename, max_age=0): self.save() with codecs_open(self.filename, 'r', encoding=self._encoding) as f: self.data = json.load(f) - except (OSError, IOError): + except (OSError, IOError, t_JSONDecodeError): + get_logger(__name__).warning("Fail to load or parse file %s. It is overridden by default settings.", + self.filename) self.save() def save(self):