简体   繁体   English

为什么RestrictedPython在与Python 3.6一起使用时表现不同?

[英]Why does RestrictedPython behave differently when used with Python 3.6?

I'm trying to use RestrictedPython (see documentation ) with Python 3.6 . 我正在尝试使用Python 3.6 RestrictedPython (请参阅文档 )。 The following code yields different results in Python 2.7 and in 3.6 : 以下代码在Python 2.73.6产生不同的结果:

from RestrictedPython import compile_restricted
from RestrictedPython.Guards import safe_builtins
restricted_globals = dict(__builtins__ = safe_builtins)

src = '''
open('/etc/passwd')
'''

code = compile_restricted(src, '<string>', 'exec')
exec(code) in restricted_globals

In Python 2.7 , the result is as expected: Python 2.7 ,结果如预期:

Traceback (most recent call last):
  File "...Playground.py", line 10, in <module>
    exec(code) in restricted_globals
  File "<string>", line 2, in <module>
NameError: name 'open' is not defined

But in Python 3.6 , the results is: 但是在Python 3.6 ,结果是:

Traceback (most recent call last):
  File "...Playground.py", line 10, in <module>
    exec(code) in restricted_globals
  File "<string>", line 2, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '/etc/passwd'

The open name/method is now obviously known, which should not be the case. 现在显然已知open名称/方法,但情况并非如此。 Why does this code behave different when used in 3.6 than when used in 2.7 ? 为什么这个代码在3.6使用时的行为与在2.7使用时的行为不同?

The way you should write your code for version 3.6 is presented here: 您应该为3.6版编写代码的方式如下所示:

https://github.com/zopefoundation/RestrictedPython https://github.com/zopefoundation/RestrictedPython

Under "Problematic Code Example". 在“有问题的代码示例”下。

Namely, the following code will work, that is, open will be not defined: 即,以下代码将起作用,即open将不定义:

from RestrictedPython import compile_restricted
from RestrictedPython import safe_builtins

source_code = """
open('/etc/passwd')
"""
byte_code = compile_restricted(source_code, '<inline>', 'exec')
exec(byte_code, {'__builtins__': safe_builtins}, {})

As for the reason why this happens, I don't have one, but I wanted to present a way of making it work anyways. 至于发生这种情况的原因,我没有,但我想提出一种让它无论如何都能运作的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Python 3.x 中,为什么 os.chdir() 在与 os.listdir() 和 os.scandir() 一起使用时表现不同? (Python 3.9.7) - In Python 3.x, why does os.chdir() behave differently when used with os.listdir() and os.scandir() ? (Python 3.9.7) 为什么 Python 3 for loop output 和行为不同? - Why does Python 3 for loop output and behave differently? 为什么Python列表的行为会根据声明的不同而有所不同? - Why does Python list of lists behave differently depending on their declaration? 为什么Python **运算符在数组和标量上的行为不同 - Why does the Python ** operator behave differently on arrays and scalars 为什么我的python类方法本身的行为与我运行模块时的行为不同? - Why does my python class method behave differently by itself than when I run the module? 为什么这个argparse代码在Python 2和3之间表现不同? - Why does this argparse code behave differently between Python 2 and 3? Python 2-为什么“ with”在嵌入式C代码中表现不同? - python 2 - why does 'with' behave differently in embedded c code? 当类在函数中时,为什么类中的全局行为会有所不同? - why does global in a class behave differently when the class is within a function? 为什么在包装时sys.excepthook会有不同的行为? - Why does sys.excepthook behave differently when wrapped? 为什么 Python 对相同的函数(例如“sum”或“and”)表现不同? - Why does Python behave differently for the same function such as 'sum' or 'and'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM