json: remove python 2 only code (#34615)
This commit is contained in:
		@@ -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())
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
 
 | 
			
		||||
@@ -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."""
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user