diff --git a/datablock.py b/datablock.py index fa2907921..a4cace1c5 100644 --- a/datablock.py +++ b/datablock.py @@ -24,7 +24,6 @@ ) from dxtbx.serialize import load from dxtbx.serialize.filename import resolve_path -from dxtbx.serialize.load import _decode_dict from dxtbx.util import get_url_scheme try: @@ -1502,7 +1501,7 @@ def from_dict(obj, check_format=True, directory=None): def from_json(string, check_format=True, directory=None): """Decode a datablock from JSON string.""" return DataBlockFactory.from_dict( - json.loads(string, object_hook=_decode_dict), + json.loads(string), check_format=check_format, directory=directory, ) diff --git a/model/experiment_list.py b/model/experiment_list.py index 0ab62d76c..f1df126a7 100644 --- a/model/experiment_list.py +++ b/model/experiment_list.py @@ -1,14 +1,9 @@ -from __future__ import absolute_import, division, print_function - import copy -import errno import json import os -from builtins import range +import pickle import pkg_resources -import six -import six.moves.cPickle as pickle from dxtbx.datablock import ( BeamComparison, @@ -34,7 +29,6 @@ from dxtbx.sequence_filenames import template_image_range from dxtbx.serialize import xds from dxtbx.serialize.filename import resolve_path -from dxtbx.serialize.load import _decode_dict from dxtbx.util import get_url_scheme try: @@ -174,10 +168,7 @@ def _load_pickle_path(self, imageset_data, param): filename = resolve_path(imageset_data[param], directory=self._directory) if self._check_format and filename: with open(filename, "rb") as fh: - if six.PY3: - return filename, pickle.load(fh, encoding="bytes") - else: - return filename, pickle.load(fh) + return filename, pickle.load(fh, encoding="bytes") return filename or "", None @@ -475,7 +466,7 @@ def _experimentlist_from_file(filename, directory=None): filename = resolve_path(filename, directory=directory) try: with open(filename, "r") as infile: - return json.load(infile, object_hook=_decode_dict) + return json.load(infile) except IOError: raise IOError("unable to read file, %s" % filename) @@ -687,7 +678,7 @@ def from_dict(obj, check_format=True, directory=None): def from_json(text, check_format=True, directory=None): """Load an experiment list from JSON.""" return ExperimentListFactory.from_dict( - json.loads(text, object_hook=_decode_dict), + json.loads(text), check_format=check_format, directory=directory, ) @@ -737,10 +728,8 @@ def from_serialized_format(filename, check_format=True): # First try as a JSON file try: return ExperimentListFactory.from_json_file(filename, check_format) - except IOError as e: - # In an ideal Python 3 world this would be much more simply FileNotFoundError, PermissionError - if e.errno in (errno.ENOENT, errno.EACCES): - raise + except (FileNotFoundError, PermissionError): + raise except Exception: pass diff --git a/newsfragments/316.misc b/newsfragments/316.misc new file mode 100644 index 000000000..f30d5497f --- /dev/null +++ b/newsfragments/316.misc @@ -0,0 +1 @@ +Code cleanup as part of a dangling filepointer investigation diff --git a/serialize/load.py b/serialize/load.py index 69423a5d0..a88cc3435 100644 --- a/serialize/load.py +++ b/serialize/load.py @@ -1,48 +1,11 @@ -from __future__ import absolute_import, division, print_function - import json import os - -import six +import warnings from dxtbx.model.crystal import CrystalFactory from dxtbx.serialize.imageset import imageset_from_dict -def _decode_list(data): - """Decode a list to str from unicode.""" - if six.PY3: - return data - rv = [] - for item in data: - if isinstance(item, six.text_type): - item = item.encode("utf-8") - elif isinstance(item, list): - item = _decode_list(item) - elif isinstance(item, dict): - item = _decode_dict(item) - rv.append(item) - return rv - - -def _decode_dict(data): - """Decode a dict to str from unicode.""" - if six.PY3: - return data - rv = {} - for key, value in data.items(): - if isinstance(key, six.text_type): - key = key.encode("utf-8") - if isinstance(value, six.text_type): - value = value.encode("utf-8") - elif isinstance(value, list): - value = _decode_list(value) - elif isinstance(value, dict): - value = _decode_dict(value) - rv[key] = value - return rv - - def imageset_from_string(string, directory=None): """Load the string and return the models. @@ -53,10 +16,14 @@ def imageset_from_string(string, directory=None): The models """ - return imageset_from_dict( - json.loads(string, object_hook=_decode_dict), directory=directory + warnings.warn( + "This function is deprecated and will be removed in the next release", + DeprecationWarning, + stacklevel=2, ) + return imageset_from_dict(json.loads(string), directory=directory) + def imageset(filename): """Load the given JSON file. @@ -72,7 +39,7 @@ def imageset(filename): filename = os.path.abspath(filename) directory = os.path.dirname(filename) with open(filename, "r") as infile: - return imageset_from_string(infile.read(), directory=directory) + return imageset_from_dict(json.load(infile), directory=directory) def datablock(filename, check_format=True): @@ -112,7 +79,7 @@ def crystal(infile): def experiment_list(infile, check_format=True): - """Load an experiment list from a serialzied format.""" + """Load an experiment list from a serialized format.""" # Resolve recursive import from dxtbx.model.experiment_list import ExperimentListFactory