コード例 #1
0
def run_straight_cappython(code):
    data = StringIO()
    env = safeeval.safe_environment()
    env.set_importer(no_imports)
    def safe_write(string):
        data.write(unicode(string, encoding="ascii").encode("utf-8"))
    env.bind("write", safe_write)
    try:
        safeeval.safe_eval(code.encode("utf-8") + "\n", env)
    except Exception, e:
        return unicode(traceback.format_exc())
コード例 #2
0
def run_with_emulated_print(code):
    data = StringIO()
    env = safeeval.safe_environment()
    env.set_importer(no_imports)
    printer = functools.partial(crazy_print, lambda: data)
    env.bind("__print__", lambda *a: printer(None, False, *a))
    env.bind("__print_file__", lambda f, *a: printer(f, False, *a))
    env.bind("__print_comma__", lambda *a: printer(None, True, *a))
    env.bind("__print_file_comma__", lambda f, *a: printer(f, True, *a))
    parsed = transforming_parser(code)
    generated_code = sourcecodegen.generate_code(parsed)
    safeeval.safe_eval(generated_code, env)
    return data.getvalue().decode("utf-8")