Why am I getting errors with execs nested in functions? [duplicate]
I'm trying to make a "compiler" for my game (so that people could do intresting stuff but not inject code), for mainly declaritive "code" (It would look like this: {"player_location":"IceHall.A7", "print", "You are teleported somewhere", "tiles":{"FirePlace.B3":{'Type':"Corner", "Actions+":{....}}}}
. This is how a action is represented; It is called when the player does it.
Anyways, it would have to be compiled into a function. When I tryed out something similar in the interactive interpreter (specifically:
def compile(code):
def act():
exec code
return act
). This (which is would be more or less what would be in the final, with the exception of "code" being constructed by me) raised a odd error:
开发者_JAVA技巧File "", line 3 SyntaxError:
unqualified exec is not allowed in function 'act' it is a nested function.
How do I get around this?
The answer, as said in this question, is the lack of context. I wanted exec code in locals(), globals()
精彩评论