Skip to content

Commit

Permalink
add test for and fix namede exceptions : GH #137
Browse files Browse the repository at this point in the history
  • Loading branch information
newville committed Jan 14, 2025
1 parent 64a0556 commit cfb57f0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion asteval/asteval.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ def on_try(self, node): # ('body', 'handlers', 'orelse', 'finalbody')
if htype is None or isinstance(e_type(), htype):
self.error = []
if hnd.name is not None:
self.node_assign(hnd.name, e_value)
self.symtable[hnd.name] = e_value
for tline in hnd.body:
self.run(tline)
break
Expand Down
16 changes: 16 additions & 0 deletions tests/test_asteval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1588,5 +1588,21 @@ def my_func(x, y):
assert etype == 'AttributeError'


@pytest.mark.parametrize("nested", [False, True])
def test_naming_exceptions(nested):
""" fixing Github issue #137"
interp = make_interpreter(nested_symtable=nested)
try_with_named_error = textwrap.dedent("""
try:
2 + ''
except Exception as my_error:
print(my_error)
""")

interp(try_with_named_error, raise_errors=True)
out = read_stdout(interp)
assert 'unsupported operand' in out
assert len(interp.error) == 0

if __name__ == '__main__':
pytest.main(['-v', '-x', '-s'])

0 comments on commit cfb57f0

Please sign in to comment.