如何修复“拒绝执行'常量'语句".错误? [英] How to fix "execution of 'Constant' statements is denied" error?
问题描述
我正在遵循web.py文档中的模板指南
I'm following the tutorial in web.py documentation for templates
import web
render = web.template.render('templates')
print (render.hello('world'))
但是,当运行python文件时,会导致错误:'Constant'语句的执行被拒绝".Google搜索没有找到任何答案,请给我一些帮助.谢谢
However when running the python file results in an error :"execution of 'Constant' statements is denied". Google searching doesn't turn up any answers,I need some help please. Thank You
推荐答案
web.py不允许在模板中执行某些类型的python法律声明.我不知道为什么它不允许您的特定声明,但是有一种方法可以让模板做更多的事情:
web.py disallows some types of python legal statements to be executed within the template. I don't know why it's disallowing your particular statement, but there's a way to allow templates to do more:
web/template.py包含列表 ALLOWED_AST_NODES
,它们是模板中允许的结构(由抽象语法树解析器使用).您可以更改它.
web/template.py contains a list ALLOWED_AST_NODES
which are constructs (used by the abstract syntax tree parser) which are permitted in within templates. You can change that.
在您的代码中,添加一次:
In your code, add once:
from web.template import ALLOWED_AST_NODES
ALLOWED_AST_NODES.append('Constant')
现在,这不是必须的,因此我怀疑您的代码中还有其他问题…….可能是多种版本?我相信常数"是python3 AST中的一个节点,而不是python2中的一个节点吗?
Now, this shouldn't be necessary, so I suspect there's something else going on in your code.... mix of versions, perhaps? I believe 'Constant' is a node in python3 AST, but not in python2?
这是将字典理解 .append('DictComp')
添加到允许的模板ast节点的有用技术,因为它已经允许列表理解.
This is a useful technique to add dictionary comprehension .append('DictComp')
to the template allowed ast nodes as it already permits list comprehension.
这篇关于如何修复“拒绝执行'常量'语句".错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!