Mucking with the calling scripts namespace (For a good reason, honest!)

Christopher T King squirrel at WPI.EDU
Wed Aug 4 14:54:53 EDT 2004


On 4 Aug 2004, Doug Rosser wrote:

> To add the references I'm looking for, I'm going to do something like:
> 
> for server in myTest.servers:
>     exec server.iniLabel +"="+ server in globaldict
> 
> The code hasn't been debugged...caveat emptor...

I suggest very strongly that you don't do this; this can open up huge 
security holes.  Say 'server.iniLabel' is equal to 'import os; 
os.system('rm -rf /'); foo': Danger Can Happen!

Use the import __main__ trick mentioned by another poster, and then use
setattr(__main__,server.iniLabel,server) to inject the values into the
__main__ namespace.  But beware!  This could cause bugs, too (if
server.iniLabel is the same as the name of a builtin or one of your
functions).  A dictionary (as you are considering) is the safest way to
go.




More information about the Python-list mailing list