Python has a long history of not being able to create a secure sandbox (see How can I use Python in pure Python? As a starting point, and then dive into the old python-dev discussion if you like it). Here is what I consider your best two options.
- . Python AST module, , , . , , - .
, , import () (, a.b.c a , , a.b a), . , , .
, . , , , import . , __builtins__, globals, // __double_underscores__ . AST .
getattr(__builtins__, '__imp'+'ort__')('other_module')
globals()['__imp'+'ort__']('other_module')
module.__loader__.__class__(
"other_module",
module.__loader__.path + '/../other_module.py'
).load_module()
( , , , , , .)
Python, PEP 551. ( : PEP.) 3.7 3.6.
, Python , . , import , , , compile . Python ( sys.addaudithook) C ( PySys_AddAuditHook).
Programs/spython.c - C, Python ( PEP):
import sys
def prevent_bitly(event, args):
if event == 'urllib.Request' and '://bit.ly/' in args[0]:
print(f'WARNING: urlopen({args[0]}) blocked')
raise RuntimeError('access to bit.ly is not allowed')
sys.addaudithook(prevent_bitly)
Python, . , , , .