exec with partial globals

Helmut Jarausch jarausch at igpm.rwth-aachen.de
Tue Oct 30 08:00:03 EDT 2012


Hi,

I'd like to give the user the ability to enter code which may only rebind
a given set of names but not all ones.
This does NOT work
A=1
B=2
Code=compile('A=7','','exec')
exec(Code,{'A':0})
print("I've got A={}".format(A)) # prints 1


How can 'filter' the gobal namespace such that modifying 'A' is allowed
but any attempt to modify 'B' should give an exception.


Many thanks for a hint,
Helmut.



More information about the Python-list mailing list