Using RestrictedPython
Rasjid Wilcox
rasjidw at openminddev.net
Mon Oct 6 04:08:13 EDT 2003
Hi,
I found references on Google to a discussion a little while ago about using
RestrictedPython instead of rexec and Bastion. But I've had trouble
finding documentation.
Below is my test program. Am I doing this the right way?
- - - -
#!/usr/bin/env python
from RestrictedPython import compile_restricted
def g(x):
return "Function g(x)"
fn = '''
def f(x):
return "x: " + str(x) + " x^2: " + str(x*x)
'''
fn2 = '''
def h(x):
f = open('test-out.txt', 'w')
f.write(x)
f.close()
s = 'Just wrote to a file'
return s
'''
exp = 'str(f(3))'
cofn = compile_restricted(fn, '<string> ', 'exec')
exec(cofn)
coexp = compile_restricted(exp, '<string>', 'eval')
print "Restricted eval: " + str(eval(coexp))
f = g
print "Restricted eval: " + str(eval(coexp))
exec(fn2)
print "Native exec: " + str(h("Native exec\n"))
h = None
#cofn2 = compile(fn2, '<string>', 'exec')
cofn2 = compile_restricted(fn2, '<string>', 'exec')
exec(cofn2)
try:
print h("Restricted exec\n")
except:
print "Can't run restricted code?"
- - - -
I know it runs and it seems to do what I expect, but is it 'right'?
Thanks,
Rasjid.
More information about the Python-list
mailing list