我试图用python创建一个python3ide和文本编辑器,以进一步了解tkinter
。在它里面(因为它是IDE),我试图运行用户输入的代码。我能找到的最好的方法就是使用exec()
。其工作原理如下:
>>> exec(input('PROMPT: '))
PROMPT: print('Hello World') #Entered in by user, this could be anything.
Hello World # <-- Output
然而,exec()
函数知道它的环境。你知道吗
>>> important_variable = 'important value' #Say that this is important.
>>> exec(input('PROMPT: '))
PROMPT: important_variable = 'meaningless value' #In the IDE, user unknowingly re-assigns to a meaningless value
>>> important_variable #look at the value
'meaningless value' #Yes, the value was changed.
这不是我想要的。我不想连接到主程序,除了我输入的值。(例如,将sys.stdin
、sys.stdout
和sys.stderr
值更改为tkinter
GUI)
我的想法是使用exec()
函数的扩展用法(即help()
):
exec(source, globals=None, locals=None, /)
Execute the given source in the context of globals and locals.
The source may be a string representing one or more Python statements
or a code object as returned by compile().
The globals must be a dictionary and locals can be any mapping,
defaulting to the current globals and locals.
If only globals is given, locals defaults to it.
我试着用一个空的dict来表示globals,而把locals留空了。乍一看这似乎管用。你知道吗
>>> important_variable = 'important value'
>>> exec_globals = {} #create a black dict of globals
>>> exec(input('PROMPT: '), exec_globals) #pass in the blank dict
PROMPT: important_variable = 'meaningless value' #change to value?
>>> important_variable #look at the value
'important value' #value is kept!
但是,运行代码的程序出现异常:
>>> exec_globals = {} #create a black dict of globals
>>> exec(input('PROMPT: '), exec_globals) #pass in the blank dict
PROMPT: THIS SHALL CAUSE A ERROR!
Traceback (most recent call last):
File "<pyshell#288>", line 1, in <module>
exec(input('PROMPT: '), exec_globals) # <-- YOU CAN SEE THE CODE
File "<string>", line 1
THIS SHALL CAUSE A ERROR!
^
SyntaxError: invalid syntax
如何防止这种情况发生,并从用户输入的代码中删除与程序的任何连接。不过,我仍然希望在程序中使用一些控件,比如更改sys.stdin
、sys.stdout
和sys.stderr
的值。是exec(source, blank_dict)
的方法,还是有更好的方法?你知道吗
经过反复挖掘,我找到了问题的答案。你知道吗
答案在
code
模块中。正如python documentation on the code module所述:基本上是用来模拟代码运行的工具。代码模块提供的函数不仅有助于read-eval-print循环,还可以在不连接主程序的情况下运行代码。以这个名为
runcode.py
的程序为例:其输出如下:
如果您是从一个真实的文件中读取,请将
'<FAKE MEANINGLESS FILE NAME>'
替换为该文件的绝对路径。这将为您提供更高级的回溯(错误消息将包括行中的内容)最棒的是,它可以与设置和结束脚本一起工作,如下所示:
就这样!你知道吗
相关问题 更多 >
编程相关推荐