I realize the question is a bit specific, sorry.
I'm trying to solve a python puzzle where what I input get exec'ed. My aim is to store something of arbitrary length in the global name space (e.g, change the variable target). You'll see below that my input is limited to 35 chars.
Here's the program:
#!/usr/bin/env python2
from sys import modules
modules.clear()
del modules
raw_input2 = raw_input
exception2 = Exception
__builtins__.__dict__.clear()
__builtins__ = None
target = None # change this !
while 1:
try:
scope = {'r':0}
exec 'r = ' + raw_input2()[:35] in scope
print 'Result:', scope['r']
except exception2, e:
print "Error: ", e
As said, my point is to store data somewhere, since vars get reset each loop.
- I know I could use
globals()['target']=xxx, but the builtins were disabled. - In theory I could use
global target;target=xxxbut this executes inscope, not in the global scope (also I think theglobalkeyword must come at the beginning of the anonymous function) - I know all variables are stored in some
__dict__object, but the only way I know to write in it is viaglobals()['target']=xxxwhich is disabled - I know you can painfully access the super-object with
().__class__.__base__, but with the limit of35characters, it doesn't seem to be the way (this string alone is21chars already, you need two more to start with0;, so only11char remaining to assign something...).
Any ideas ? Thanks!
scopeand get your hands on the real globals. Even if you somehow managed to import__main__, you'd only get a copy of it.globals()that would be doable? Do you think I can re-import it ?globals()would give youscope. It wouldn't help.locals()would give me the local scope, corresponding toscope. Soglobals()is not... "global" ?exec cmd in scopesetsscopeas the global and local scope of any commands incmd.