json: remove python 2 only code (#34615)

This commit is contained in:
Harmen Stoppels 2022-12-21 22:18:12 +01:00 committed by GitHub
parent dab68687bd
commit 492a603d5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 5 additions and 45 deletions

View File

@ -507,16 +507,3 @@ def test_legacy_yaml(tmpdir, install_mockery, mock_packages):
("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())

View File

@ -23,7 +23,6 @@
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 _source_single_file(file_and_args, environment):
]
)
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 set_intersection(fullset, *args):
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)

View File

@ -9,7 +9,7 @@
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 @@
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."""