Skip to content

Commit

Permalink
Merge pull request #630 from openforcefield/bugfix-ff-string-parse
Browse files Browse the repository at this point in the history
Store IOError message from attribute, not str method
  • Loading branch information
mattwthompson authored Jul 6, 2020
2 parents e5d91c2 + bbe83a1 commit e1b6e74
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
3 changes: 3 additions & 0 deletions docs/releasehistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ Bugfixes
- `PR #631 <https://github.com/openforcefield/openforcefield/pull/631>`_: Fixes a bug in which calling
:py:meth:`openforcefield.utils.utils.utils.unit_to_string <openforcefield.utils.utils.unit_to_string>` returned
``None`` when the unit is dimensionless. Now ``"dimensionless"`` is returned.
- `PR #630 <https://github.com/openforcefield/openforcefield/pull/630>`_: Closes issue `Issue #629
<https://github.com/openforcefield/openforcefield/issues/629>`_ in which the wrong exception is raised when
attempting to instantiate a ``ForceField`` from an unparsable string.

New features
""""""""""""
Expand Down
6 changes: 6 additions & 0 deletions openforcefield/tests/test_forcefield.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,12 @@ def test_create_forcefield_from_file(self):
assert len(forcefield._parameter_handlers['ImproperTorsions']._parameters) == 4
assert len(forcefield._parameter_handlers['vdW']._parameters) == 35

def test_load_bad_string(self):
with pytest.raises(IOError) as exception_info:
ForceField('1234')
assert 'Source 1234 could not be read.' in str(exception_info.value)
assert 'syntax error' in str(exception_info.value)

@pytest.mark.skip(reason='Needs to be updated for 0.2.0 syntax')
def test_create_forcefield_from_file_list(self):
# These offxml files are located in package data path, which is automatically installed and searched
Expand Down
4 changes: 2 additions & 2 deletions openforcefield/typing/engines/smirnoff/forcefield.py
Original file line number Diff line number Diff line change
Expand Up @@ -983,14 +983,14 @@ def parse_smirnoff_from_source(self, source):
smirnoff_data = parameter_io_handler.parse_file(source)
return smirnoff_data
except ParseError as e:
exception_msg = str(e)
exception_msg = e.msg
except (FileNotFoundError, OSError):
# If this is not a file path or a file handle, attempt parsing as a string.
try:
smirnoff_data = parameter_io_handler.parse_string(source)
return smirnoff_data
except ParseError as e:
exception_msg = str(e)
exception_msg = e.msg

# If we haven't returned by now, the parsing was unsuccessful
valid_formats = [
Expand Down
2 changes: 1 addition & 1 deletion openforcefield/typing/engines/smirnoff/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def parse_string(self, data):
smirnoff_data = xmltodict.parse(data, attr_prefix='')
return smirnoff_data
except ExpatError as e:
raise ParseError(e)
raise ParseError(str(e))

def to_file(self, file_path, smirnoff_data):
"""Write the current forcefield parameter set to a file.
Expand Down

0 comments on commit e1b6e74

Please # to comment.