开发者

Why am I getting errors with execs nested in functions? [duplicate]

This question already has answers here: Why doesn't exec work in a function with a subfunction? (6 answers) Closed 7 years ago.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜