From 492a603d5e5feee7e30607288ec5e3ab9121a843 Mon Sep 17 00:00:00 2001 From: Harmen Stoppels Date: Wed, 21 Dec 2022 22:18:12 +0100 Subject: json: remove python 2 only code (#34615) --- lib/spack/spack/test/spec_yaml.py | 13 ------------- lib/spack/spack/util/environment.py | 9 ++------- lib/spack/spack/util/spack_json.py | 28 +++------------------------- 3 files changed, 5 insertions(+), 45 deletions(-) diff --git a/lib/spack/spack/test/spec_yaml.py b/lib/spack/spack/test/spec_yaml.py index b939674550..f1adf1cad9 100644 --- a/lib/spack/spack/test/spec_yaml.py +++ b/lib/spack/spack/test/spec_yaml.py @@ -507,16 +507,3 @@ ordered_spec = collections.OrderedDict( ("version", "1.2.11"), ] ) - - -@pytest.mark.regression("31092") -def test_strify_preserves_order(): - """Ensure that ``spack_json._strify()`` dumps dictionaries in the right order. - - ``_strify()`` is used in ``spack_json.dump()``, which is used in - ``Spec.dag_hash()``, so if this goes wrong, ``Spec`` hashes can vary between python - versions. - - """ - strified = sjson._strify(ordered_spec) - assert list(ordered_spec.items()) == list(strified.items()) diff --git a/lib/spack/spack/util/environment.py b/lib/spack/spack/util/environment.py index f46b2508e0..924a0d3aff 100644 --- a/lib/spack/spack/util/environment.py +++ b/lib/spack/spack/util/environment.py @@ -23,7 +23,6 @@ import spack.config import spack.platforms import spack.spec import spack.util.executable as executable -import spack.util.spack_json as sjson from spack.util.path import path_to_os_path, system_path_filter is_windows = sys.platform == "win32" @@ -1013,11 +1012,7 @@ def environment_after_sourcing_files(*files, **kwargs): ] ) output = shell(source_file_arguments, output=str, env=environment, ignore_quotes=True) - environment = json.loads(output) - - # If we're in python2, convert to str objects instead of unicode - # like json gives us. We can't put unicode in os.environ anyway. - return sjson.encode_json_dict(environment) + return json.loads(output) current_environment = kwargs.get("env", dict(os.environ)) for f in files: @@ -1054,7 +1049,7 @@ def sanitize(environment, exclude, include): return subset # Don't modify input, make a copy instead - environment = sjson.decode_json_dict(dict(environment)) + environment = dict(environment) # include supersedes any excluded items prune = set_intersection(set(environment), *exclude) diff --git a/lib/spack/spack/util/spack_json.py b/lib/spack/spack/util/spack_json.py index 41f5f1c376..a5e12851d8 100644 --- a/lib/spack/spack/util/spack_json.py +++ b/lib/spack/spack/util/spack_json.py @@ -9,7 +9,7 @@ from typing import Any, Dict, Optional import spack.error -__all__ = ["load", "dump", "SpackJSONError", "encode_json_dict", "decode_json_dict"] +__all__ = ["load", "dump", "SpackJSONError"] _json_dump_args = {"indent": 2, "separators": (",", ": ")} @@ -17,40 +17,18 @@ _json_dump_args = {"indent": 2, "separators": (",", ": ")} def load(stream: Any) -> Dict: """Spack JSON needs to be ordered to support specs.""" if isinstance(stream, str): - load = json.loads # type: ignore[assignment] - else: - load = json.load # type: ignore[assignment] - - return _strify(load(stream, object_hook=_strify), ignore_dicts=True) - - -def encode_json_dict(data: Dict) -> Dict: - """Converts python 2 unicodes to str in JSON data.""" - return _strify(data) + return json.loads(stream) + return json.load(stream) def dump(data: Dict, stream: Optional[Any] = None) -> Optional[str]: """Dump JSON with a reasonable amount of indentation and separation.""" - data = _strify(data) if stream is None: return json.dumps(data, **_json_dump_args) # type: ignore[arg-type] json.dump(data, stream, **_json_dump_args) # type: ignore[arg-type] return None -def decode_json_dict(data: Dict) -> Dict: - """Converts str to python 2 unicodes in JSON data.""" - return _strify(data) - - -def _strify(data: Dict, ignore_dicts: bool = False) -> Dict: - """Helper method for ``encode_json_dict()`` and ``decode_json_dict()``. - - Converts python 2 unicodes to str in JSON data, or the other way around.""" - # this is a no-op in python 3 - return data - - class SpackJSONError(spack.error.SpackError): """Raised when there are issues with JSON parsing.""" -- cgit v1.2.3-70-g09d2