def get_result(self, code):
"""
Get result of execution of the line of code
:param code: string of code
:type code: str
:return: str
"""
try:
tree = ast.parse(code)
except Exception, e:
return str(e)
if not tree.body:
return ""
if isinstance(tree.body[0], Expr):
try:
result = eval(code, self.globals)
except Exception, e:
return str(e)
return str(result)
else:
try:
exec code in self.globals
except Exception, e:
return str(e)
return ""
评论列表
文章目录