沙盒/运行python码一行一行 [英] sandboxing/running python code line by line

查看:265
本文介绍了沙盒/运行python码一行一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够做这些两件事情:



发明原则@ 18:20
Live ClojureScript游戏编辑器



如果你不想检查视频,我的问题是这样的:

$ b说说我有这个代码:

  .... 
xs = [ ]
for x in xrange(10):
xs.append(x)
...

我想做一个环境,我可以执行代码,声明语句,并在更改时观察/跟踪本地/全局变量。也许给它一个vars的列表来跟踪在本地/全球词典。喜欢逐步通过代码并保存状态信息。



最佳地,我想保存每个状态以及相关联的上下文数据(本地/全局变量),以便我可以验证例如谓词。



我想做一些像Bret Victor的binarySearch例子发明原则@ 18:20



我有意义吗?我发现复杂的解释在文本,但视频展示了我想要尝试:)



感谢您的时间






我尝试过/读/ googled:




  • code.InteractiveConsole / code.InteractiveInterpreter

  • livecoding module:似乎适用于纯功能/无状态代码

  • exec / eval magic:似乎我不能像我一样得到像粒子一样细的控制ike。

  • trace 模块似乎也不是这样。

  • python eval(...)沙盒),全局变量在沙箱中,除非在def中为什么?< - 这是接近我想要的,但它编译整个字符串/代码块并运行它在一个步骤。如果我可以运行这样的文件,但检查每行/语句之间的本地。

  • 运行python源代码一行一行< - 这不是我想要的

  • Ruby和Python如何实现他们的交互式控制台? - 这个主题建议我再查看代码模块更多



我的下一步将查找 ast 并编译代码并逐个运行,但我真的需要一些指导。我应该多看看反思和 inspect -module ??



我已经使用了 Spin模型检查器,但它使用自己的DSL,我只是喜欢在实现中做造型n语言,在这种情况下是python。



哦和BTW我知道沙盒代码的安全隐患,但我不是想做一个安全的执行环境, '尝试制作一个非常互动的环境,以粗体模型检查或谓词断言为例。

解决方案

在我的初始成功 sys.settrace(),我最终切换到<一个href =http://docs.python.org/library/ast.html =nofollow noreferrer> ast 模块(抽象语法树)。我解析我要分析的代码,然后在每个赋值之后插入新的调用,以报告变量名称及其新值。我还插入调用循环迭代和函数调用。然后我执行修改的树。

  tree = parse(source)

visitor = TraceAssignments()
new_tree = visitor.visit(tree)
fix_missing_locations(new_tree)

code = compile(new_tree,PSEUDO_FILENAME,'exec')

self。环境[CONTEXT_NAME] = builder
自我环境中的exec代码

我正在努力一个像Bret Victor这样的实时编码工具,你可以看到我的工作代码,以及其行为方式的一些示例在测试中。您还可以从项目页面。


I'd love to be able to do something like these two are doing:

Inventing on principle @18:20 , Live ClojureScript Game Editor

If you don't wanna check the videos, my problem is this:

Say I had this code:

....
xs = []
for x in xrange(10):
    xs.append(x)
...

I'd like to make an environment where I can execute the code, statement for statement and watch/trace the locals/globals as they change. Maybe give it a list of vars to keep track of in the locals/globals dictionaries. Like stepping through the code and saving the state info.

Optimally I'd like to save every state and it's associated context-data (locals/globals) so I can verify predicates for instance.

I'd like to do something like Bret Victor's binarySearch example Inventing on principle @18:20

Am I making sense? I find it complicated to explain in text, but the videos showcase what I want to try :)

Thanks for your time


What I've tried/read/googled:

My next step would be looking into ast and compiling the code and running it bit-by-bit, but I really need some guidance.. Should I look more into reflection and the inspect-module??

I've used the Spin model checker before, but it uses its own DSL and I'd just love to do the modelling in the implementation language, in this case python.

Oh and BTW I know about the security implications of sandboxing code, but I'm not trying to make a secure execution environment, I'm trying to make a very interactive environment, aiming for crude model checking or predicate assertion for instance.

解决方案

After my initial success with sys.settrace(), I ended up switching to the ast module (abstract syntax trees). I parse the code I want to analyse and then insert new calls after each assignment to report on the variable name and its new value. I also insert calls to report on loop iterations and function calls. Then I execute the modified tree.

        tree = parse(source)

        visitor = TraceAssignments()
        new_tree = visitor.visit(tree)
        fix_missing_locations(new_tree)

        code = compile(new_tree, PSEUDO_FILENAME, 'exec')

        self.environment[CONTEXT_NAME] = builder
        exec code in self.environment

I'm working on a live coding tool like Bret Victor's, and you can see my working code on GitHub, and some examples of how it behaves in the test. You can also find links to a demo video, tutorial, and downloads from the project page.

这篇关于沙盒/运行python码一行一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆