Nested Scopes Question (exec)
Rainer Deyke
root at rainerdeyke.com
Sun Jun 24 21:05:03 EDT 2001
"Rainer Deyke" <root at rainerdeyke.com> wrote in message
news:DWvZ6.362805$oc7.43089434 at news2.rdc2.tx.home.com...
> The wording here should probably be changed to "may or may not".
Otherwise
> people might start to rely on the fact that dictionary usually doesn't
> affect the local variables and do stuff like this:
>
> def f(x):
> d = locals()
> d['x'] += 5
> exec 'print x' in d
Oops, bad example. How about this:
def f(x):
d = locals()
d['x'] += 5
assert d['x'] != x
I know, it's totally contrived. More realistically:
def run_arbitrary_code(code):
try:
exec code in locals()
except:
print 'Exception raised by arbitrary code: "%s"' % code
run_arbitrary_code('del code;raise SyntaxError')
If modifying the dictionary returned from 'locals' changes the local
variables, then the function 'run_arbitrary_code' can be forced to throw an
exception by malicious code.
--
Rainer Deyke (root at rainerdeyke.com)
Shareware computer games - http://rainerdeyke.com
"In ihren Reihen zu stehen heisst unter Feinden zu kaempfen" - Abigor
More information about the Python-list
mailing list