1

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=xxx but this executes in scope, not in the global scope (also I think the global keyword 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 via globals()['target']=xxx which is disabled
  • I know you can painfully access the super-object with ().__class__.__base__, but with the limit of 35 characters, it doesn't seem to be the way (this string alone is 21 chars already, you need two more to start with 0;, so only 11 char remaining to assign something...).

Any ideas ? Thanks!

7
  • 1
    I don't think there's a way to escape the scope and get your hands on the real globals. Even if you somehow managed to import __main__, you'd only get a copy of it. Commented Aug 23, 2017 at 11:30
  • Ok thanks. But if I had access to globals() that would be doable? Do you think I can re-import it ? Commented Aug 23, 2017 at 11:35
  • 1
    No, globals() would give you scope. It wouldn't help. Commented Aug 23, 2017 at 11:40
  • I'm a bit lost. I thought locals() would give me the local scope, corresponding to scope. So globals() is not... "global" ? Commented Aug 23, 2017 at 11:42
  • 1
    exec cmd in scope sets scope as the global and local scope of any commands in cmd. Commented Aug 23, 2017 at 11:48

1 Answer 1

1

The answer is: write in __builtins__.

Example input:

1;__builtins__['a']="string1"
Result: 1
1;__builtins__['a']+="string2"

... which actually seems very simple since it is right above the variable marked # change this in the question. I do not fully understand yet why the __builtins__ var is passed in the exec scope, especially since it should be None (and hence not a dictionary), but is assignable like a dictionary.

But it works, tested in python 2.7

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.