Skip to content

Commit

Permalink
Fix evaluating strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
pearu committed Sep 1, 2021
1 parent 2693f48 commit 1601cce
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 3 additions & 1 deletion numpy/f2py/crackfortran.py
Expand Up @@ -2629,7 +2629,9 @@ def _eval_scalar(value, params):
if _is_kind_number(value):
value = value.split('_')[0]
try:
value = str(eval(value, {}, params))
# TODO: use symbolic from PR #19805
value = eval(value, {}, params)
value = (repr if isinstance(value, str) else str)(value)
except (NameError, SyntaxError, TypeError):
return value
except Exception as msg:
Expand Down
12 changes: 11 additions & 1 deletion numpy/f2py/tests/test_crackfortran.py
Expand Up @@ -38,7 +38,6 @@ def test_module(self):
assert self.module.t0('23') == b'2'



class TestPublicPrivate():
def test_defaultPrivate(self, tmp_path):
f_path = tmp_path / "mod.f90"
Expand Down Expand Up @@ -166,3 +165,14 @@ def test_ignore_inner_quotes(self):
def test_multiple_relevant_spaces(self):
assert_equal(markinnerspaces("a 'b c' 'd e'"), "a 'b@_@c' 'd@_@e'")
assert_equal(markinnerspaces(r'a "b c" "d e"'), r'a "b@_@c" "d@_@e"')


class TestEval(util.F2PyTest):

def test_eval_scalar(self):
eval_scalar = crackfortran._eval_scalar

assert eval_scalar('123', {}) == '123'
assert eval_scalar('12 + 3', {}) == '15'
assert eval_scalar('a + b', dict(a=1, b=2)) == '3'
assert eval_scalar('"123"', {}) == "'123'"

0 comments on commit 1601cce

Please sign in to comment.