n | def safeval(__source, __globals=None, __locals=None): | n | def safeval(expr, eval_globals=None, eval_locals=None): |
| globals_copy = globals().copy() | | glob = globals().copy() |
| try: | | try: |
n | return eval(__source, __globals, __locals) | n | return eval(expr, eval_globals, eval_locals) |
| except NameError: | | except NameError: |
n | return __source | n | return expr |
| except Exception as e: | | except Exception as e: |
| return e | | return e |
| finally: | | finally: |
n | for key, value in globals_copy.items(): | n | for key, value in glob.items(): |
| if not key in globals(): | | if not key in globals(): |
| globals()[key] = value | | globals()[key] = value |
t | globals_finally = globals().copy() | t | glob1 = globals().copy() |
| for key, value in globals_finally.items(): | | for key, value in glob1.items(): |
| if not key in globals_copy: | | if not glob[key]: |
| del globals()[key] | | del globals()[key] |