Skip to content
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 3 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions datablock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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),
Copy link
Collaborator

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?

Copy link
Member Author

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

check_format=check_format,
directory=directory,
)
Expand Down
23 changes: 6 additions & 17 deletions model/experiment_list.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
from __future__ import absolute_import, division, print_function
Copy link
Collaborator

Choose a reason for hiding this comment

The 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,
Expand All @@ -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:
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions newsfragments/316.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Code cleanup as part of a dangling filepointer investigation
51 changes: 9 additions & 42 deletions serialize/load.py
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):
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Expand All @@ -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.
Expand All @@ -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):
Expand Down Expand Up @@ -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

Expand Down