How to fix “execution of 'Constant' statements is denied” error?

我的未来我决定 提交于 2021-01-29 08:40:35

问题


I'm following the tutorial in web.py documentation for templates

    import web
    render = web.template.render('templates')
    print (render.hello('world'))

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


回答1:


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 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')

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?

This is a useful technique to add dictionary comprehension .append('DictComp') to the template allowed ast nodes as it already permits list comprehension.



来源:https://stackoverflow.com/questions/58518448/how-to-fix-execution-of-constant-statements-is-denied-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!