def execwrap(content, from_file=None):
from_file = from_file or '<stdin>'
if isinstance(content, six.string_types):
content = compile_text(content, from_file=from_file)
def _inner():
global_env = exec_globals()
local_env = global_env
six.exec_(content, global_env, local_env)
return global_env
globals_ = {}
output_handler = None
try:
with reopen_stdout_stderr() as output_handler:
globals_ = _inner()
except Exception:
if output_handler is not None:
output = "%s%s" % (output_handler.read(),
_exception(from_file))
else:
output = _exception(from_file)
else:
output = output_handler.read()
output = strutil.ensure_text(output)
return output, globals_
评论列表
文章目录