-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAstWarpper.py
More file actions
28 lines (24 loc) · 864 Bytes
/
Copy pathAstWarpper.py
File metadata and controls
28 lines (24 loc) · 864 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import ast
import traceback
import re
from AstVisitor import NodeVisitor
class AstWrapper:
def __init__(self, file):
with open(file, "r") as source:
self.tree = ast.parse(source.read())
# tree = ast.parse("print(2+2)")
self.NV = NodeVisitor()
self.NV.visit(self.tree)
def dump(self):
# вывод получившегося дерева
print(ast.dump(self.tree))
def execute(self, g_dict):
d = {"__builtins__": None}
glob_dict = {**g_dict, **d}
try:
exec(compile(source=self.tree, filename="<ast>", mode='exec'), glob_dict)
except TypeError as e:
lineno = re.search('line (\d)', traceback.format_exc()).groups('0')[0]
print(f'TypeError on {lineno} line. Message was: {e}')
import sys
sys.exit()