-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Code cleanup for dangling file pointer investigation #316
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,9 @@ | ||
from __future__ import absolute_import, division, print_function | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🙂 |
||
|
||
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Code cleanup as part of a dangling filepointer investigation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is only useless now we are in the future 👍 |
||
"""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 | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was the
object_hook
stuff really useless?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
on Python 3 it was a no-op