From 10591fb87fe3889e85c7902a22bebe56d8138913 Mon Sep 17 00:00:00 2001 From: Sergey Kosukhin Date: Thu, 8 Dec 2016 22:42:18 +0100 Subject: Json loader now returns str objects instead of unicode. (#2524) --- lib/spack/spack/util/spack_json.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/spack/spack/util/spack_json.py b/lib/spack/spack/util/spack_json.py index 240ce86c68..236eef8983 100644 --- a/lib/spack/spack/util/spack_json.py +++ b/lib/spack/spack/util/spack_json.py @@ -37,9 +37,11 @@ _json_dump_args = { def load(stream): """Spack JSON needs to be ordered to support specs.""" if isinstance(stream, basestring): - return json.loads(stream) + return _byteify(json.loads(stream, object_hook=_byteify), + ignore_dicts=True) else: - return json.load(stream) + return _byteify(json.load(stream, object_hook=_byteify), + ignore_dicts=True) def dump(data, stream=None): @@ -50,7 +52,25 @@ def dump(data, stream=None): return json.dump(data, stream, **_json_dump_args) +def _byteify(data, ignore_dicts=False): + # if this is a unicode string, return its string representation + if isinstance(data, unicode): + return data.encode('utf-8') + # if this is a list of values, return list of byteified values + if isinstance(data, list): + return [_byteify(item, ignore_dicts=True) for item in data] + # if this is a dictionary, return dictionary of byteified keys and values + # but only if we haven't already byteified it + if isinstance(data, dict) and not ignore_dicts: + return dict((_byteify(key, ignore_dicts=True), + _byteify(value, ignore_dicts=True)) for key, value in + data.iteritems()) + # if it's anything else, return it in its original form + return data + + class SpackJSONError(spack.error.SpackError): """Raised when there are issues with JSON parsing.""" + def __init__(self, msg, yaml_error): super(SpackJSONError, self).__init__(msg, str(yaml_error)) -- cgit v1.2.3-70-g09d2