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