开发者

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

开发者 https://www.devze.com 2023-03-16 07:59 出处:网络
This question already has answers here: Why doesn't exec work in a function with a subfunction? (6 answers)
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

精彩评论

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