Вопрос по python – Выполнение кода Python, содержащегося в строке

8

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

if key == K_a:
    ## Move left
    pass
elif key == K_d:
    ## Move right
    pass

Error: User Rate Limit Exceeded

def keydown(self, key):
    ## Run code from text editor

Error: User Rate Limit Exceeded

Ваш Ответ

4   ответа
25

eval(string)

Definition

eval(code, globals=None, locals=None)

__builtins__

Example
eval("print('Hello')")

hello

eval("print('Hello, %s'%name)", {}, {'name':'person-b'})
Security Concerns

eval("import os;os.system('sudo rm -rf /')")

eval("import os;...", {'os':None})

Strange Example

eval

def hello() : print('Hello')
def world() : print('world')
CURRENT_MOOD = 'happy'

eval(get_code(), {'contrivedExample':__main__}, {'hi':hello}.update(locals()))

Gives the current module another name (it becomes contrivedExample to the script). The consumer can call contrivedExample.hello() now.) It defines hi as pointing to hello It combined that dictionary with the list of current globals in the executing module. FAIL

exec

exec Definition


exec "code" [in scope]

exec Example
exec "print('hello')"

hello

eval "print('hello, '+name)" in {'name':'person-b'}
exec Security Concerns

exec "import os;os.system('sudo rm -rf /')"
Print Statement

printfrom __future__ import print_statement

print "hello"

print("hello")
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
0
0

2

exec "codestring"

def keydown(self, key):
    exec user_code

compile()

user_source = "def user_func(args):\n" + '\n'.join("    "+line for line in user_source.splitlines())

d={}
exec user_source in d
user_func = d['user_func']

if key == K_a:
   user_func(args)

Похожие вопросы