运行exec(),不要连接到python3中的主程序

2024-09-23 22:18:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用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.stdinsys.stdoutsys.stderr值更改为tkinterGUI)

我的想法是使用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.stdinsys.stdoutsys.stderr的值。是exec(source, blank_dict)的方法,还是有更好的方法?你知道吗


Tags: thetoininputvaluesysbevariable
1条回答
网友
1楼 · 发布于 2024-09-23 22:18:01

经过反复挖掘,我找到了问题的答案。你知道吗

答案在code模块中。正如python documentation on the code module所述:

The code module provides facilities to implement read-eval-print loops in Python.

基本上是用来模拟代码运行的工具。代码模块提供的函数不仅有助于read-eval-print循环,还可以在不连接主程序的情况下运行代码。以这个名为runcode.py的程序为例:

import code

important_variable = 'important value'

program = '\n'.join([
            'for n in range(10):',
            '    print(n)',
            'important_variable = None',
            'def error(): raise Exception()',
            'error()',
            ])

interpreter = code.InteractiveInterpreter()
interpreter.runsource(program, '<FAKE MEANINGLESS FILE NAME>', 'exec')

print()
print(important_variable)

其输出如下:

0
1
2
3
4
5
6
7
8
9
Traceback (most recent call last):
  File "<FAKE MEANINGLESS FILE NAME>", line 5, in <module> #No connection!
  File "<FAKE MEANINGLESS FILE NAME>", line 4, in error #No connection!
Exception

important value

如果您是从一个真实的文件中读取,请将'<FAKE MEANINGLESS FILE NAME>'替换为该文件的绝对路径。这将为您提供更高级的回溯(错误消息将包括行中的内容)

最棒的是,它可以与设置和结束脚本一起工作,如下所示:

import code

program = '\n'.join([
            'for n in range(10):',
            '    print(n)',
            'def error(): raise Exception()',
            'error()',
            ])

interpreter = code.InteractiveInterpreter()

#Setup code. This could change sys.stdin, stdout, etc. and setup variables!
interpreter.runsource('setup_stuff = None', 'whatever name you want', 'exec')

#Run code
interpreter.runsource(program, '<FAKE MEANINGLESS FILE NAME>', 'exec')

#Process Code now that the program is done, this might get the values in local() or something like that.
interpreter.runsource(program, 'whatever name you want', 'exec')

就这样!你知道吗

相关问题 更多 >