Skip to content

Commit

Permalink
fixed #486: Errors while setting Parameter expressions persisting
Browse files Browse the repository at this point in the history
  • Loading branch information
schachmett committed Aug 4, 2018
1 parent 6c87262 commit 59340e0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lmfit/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,8 @@ def __set_expression(self, val):
if val is None:
self._expr_ast = None
if val is not None and self._expr_eval is not None:
self._expr_eval.error = []
self._expr_eval.error_msg = None
self._expr_ast = self._expr_eval.parse(val)
check_ast_errors(self._expr_eval)
self._expr_deps = get_ast_names(self._expr_ast)
Expand Down
17 changes: 17 additions & 0 deletions tests/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,23 @@ def test_expr_with_bounds(self):
pars.add('c4', expr='csum-c1-c2-c3', min=0, max=1)
assert_(isclose(pars['c4'].value, 0.2))

def test_invalid_expr_exceptions(self):
"test if an exception is raised for invalid expressions (GH486)"""
p1 = Parameters()
p1.add('t', 2.0, min=0.0, max=5.0)
p1.add('x', 10.0)
with self.assertRaises(SyntaxError):
p1.add('y', expr='x*t + sqrt(t)/')
assert(len(p1['y']._expr_eval.error) > 0)
p1.add('y', expr='x*t + sqrt(t)/3.0')
p1['y'].set(expr='x*3.0 + t**2')
assert('x*3' in p1['y'].expr)
assert(len(p1['y']._expr_eval.error) == 0)
with self.assertRaises(SyntaxError):
p1['y'].set(expr='t+')
assert(len(p1['y']._expr_eval.error) > 0)
assert_almost_equal(p1['y'].value, 34.0)


if __name__ == '__main__':
unittest.main()

0 comments on commit 59340e0

Please sign in to comment.