t | def safeval(expr, global_=None, local_=None): | t | def safeval(expr, global_=None, local_=None): |
| if local_ is not None and global_ is not None: | | if local_ is not None and global_ is not None: |
| return None | | return None |
| if local_ is None: | | if local_ is None: |
| local_ = {} | | local_ = {} |
| if global_ is None: | | if global_ is None: |
| global_ = {} | | global_ = {} |
| try: | | try: |
| return eval(expr, global_, local_) | | return eval(expr, global_, local_) |
| except NameError: | | except NameError: |
| try: | | try: |
| a = eval(expr) | | a = eval(expr) |
| return a | | return a |
| except NameError: | | except NameError: |
| return expr | | return expr |
| except KeyError: | | except KeyError: |
| return None | | return None |
| except Exception as e: | | except Exception as e: |
| return e | | return e |