unqualified exec is not allowed in function

459 views
Skip to first unread message

DenesL

unread,
Apr 24, 2008, 11:59:10 AM4/24/08
to web2py Web Framework
My code was getting too complex for the view, so I defined it as a
function in the controller, which I add to the response dictionary
making it available in the view.

First question: would this be the proper way to do it?.

Continuing, in the same controller I have:

exec('from applications.%s.modules import yyy as Z'
%request.application)
reload(Z)

now when I call the controller I get a ticket with:

SyntaxError: unqualified exec is not allowed in function 'ctrler' it
contains a nested function with free variables...

Posts in comp.lang.python suggest 'qualifying' the exec by using
exec <something> in <globals_dic>,<locals_dic>

but I am not sure of how to proceed from here, and that would be my
second question.

Thank you.

mdipierro

unread,
Apr 24, 2008, 12:31:05 PM4/24/08
to web2py Web Framework
I have never seen this error before and I do not know. I will look
into it.
For now I suggest do not use the exec and import using the explicit
path directly.
Hopefully you are doing this only once in you app so you can edit it
as you rename the app.

Massimo

DenesL

unread,
Apr 24, 2008, 2:54:06 PM4/24/08
to web2py Web Framework
Unfortunately I use the imported module (Z) a lot, but that gave me
the idea to define the function in the imported module not in the
controller, and include the module's function in the response:

return dict(func1=Z.func1,...)

and in the view:

{{=XML(func1())}}

It works now.

About the first question Massimo, do you see any problems with this?.

By the way, this removes all the 'pass' and 'backslash string
continuation' problems for me since all the code was pure python.
Maybe this could be useful to limodou and his JSON problem.

Massimo Di Pierro

unread,
Apr 24, 2008, 3:01:32 PM4/24/08
to web...@googlegroups.com
I do not see a problem and that is how it should be done.
Are you still using exec (which is fine and should work)?

Massimo

DenesL

unread,
Apr 24, 2008, 4:24:28 PM4/24/08
to web2py Web Framework
Yes, the exec is still there, unmodified.

The setup with the function inside the controller (function inside
function) also works fine when there is no exec involved.

I don't know if it would be worthwhile to pursue the 'unqualified
exec' error, at least in my case I could just add another exec to
import the function since it is not really related to the functions
already provided by module Z.

Hmmm... performance wise which option is better?
Have the extra code being imported by one exec in several places
versus running two exec statements in one place with a smaller
original exec size.
Reply all
Reply to author
Forward
0 new messages