interactive execution

J

Jive Dadson

How does one execute arbitrary text as code within a module's context?

I've got some code that compiles some text and then executes it. When
the string is "print 'Hello'", it prints "Hello". I get no exception
when I compile and execute "foo = 555". If I then compile and exec
"print foo", I get a name error. The variable foo is undefined. My
assumption is that the "exec" command created a new namespace, put "foo"
in that namespace, and then threw the namespace away. Or something.

I know it must be possible to do this, because it's exactly what
programs like IDLE do.

Thankee.
 
J

Jeff Shannon

Jive said:
How does one execute arbitrary text as code within a module's context?

I've got some code that compiles some text and then executes it. When
the string is "print 'Hello'", it prints "Hello". I get no exception
when I compile and execute "foo = 555". If I then compile and exec
"print foo", I get a name error. The variable foo is undefined. My
assumption is that the "exec" command created a new namespace, put "foo"
in that namespace, and then threw the namespace away. Or something.

You can do

exec codestring in globaldict, localdict

(Or something like that, this is from unused memory and is untested.)
The net effect is that exec uses the subsequent dictionaries as its
globals and locals, reading from and writing to them as necessary.

(Note that this doesn't get you any real security, because malicious
code can still get to __builtins__ from almost any object...)

Jeff Shannon
Technician/Programmer
Credit International
 
G

George Yoshida

Jive said:
I've got some code that compiles some text and then executes it. When
the string is "print 'Hello'", it prints "Hello". I get no exception
when I compile and execute "foo = 555". If I then compile and exec
"print foo", I get a name error. The variable foo is undefined. My
assumption is that the "exec" command created a new namespace, put "foo"
in that namespace, and then threw the namespace away. Or something.

I know it must be possible to do this, because it's exactly what
programs like IDLE do.

exec statement in name_space
will do the trick.
>>> d = {}
>>> exec 'foo=555' in d
>>> d['foo'] 555
>>> exec "print foo" in d
555

- george
 
J

Jive Dadson

Jeff said:
You can do

exec codestring in globaldict, localdict

(Or something like that, this is from unused memory and is untested.)
The net effect is that exec uses the subsequent dictionaries as its
globals and locals, reading from and writing to them as necessary.

(Note that this doesn't get you any real security, because malicious
code can still get to __builtins__ from almost any object...)

Jeff Shannon
Technician/Programmer
Credit International

Promising, but,

Traceback (most recent call last):
File "F:/C++ Projects/zardude/temp.py", line 9, in -toplevel-
exec "foo = 555" in globaldict, localdict
NameError: name 'globaldict' is not defined
 
J

Jeff Shannon

Jive said:
Yeah. I got it.

exec "foo = 555" in globals(), locals() does the trick.

You can do it with your own dicts, too -- but they must already exist,
exec doesn't create them out of nowhere.

This gives you some control over what the exec'ed statement actually
sees, as well as what happens with the results. (But as I mentioned
before, there is no real security here if you're exec'ing arbitrary
code -- there's no sandboxing involved, and the exec'ed string *can*
use that __builtins__ reference (among other things) to do all sorts
of malicious stuff.)

Jeff Shannon
Technician/Programmer
Credit International
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,219
Messages
2,571,118
Members
47,732
Latest member
MarianForc

Latest Threads

Top