pythonconstantsweb.py

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


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


Solution

  • 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.