473,320 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

interactive execution

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.
Jul 18 '05 #1
5 1171
Jive Dadson wrote:
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

Jul 18 '05 #2
Jive Dadson wrote:
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
Jul 18 '05 #3


Jeff Shannon wrote:

Jive Dadson wrote:
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


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
Jul 18 '05 #4
Yeah. I got it.

exec "foo = 555" in globals(), locals() does the trick.
Jul 18 '05 #5
Jive Dadson wrote:
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.
myglobals = {'a':2, 'b':5}
mylocals = {'c': 3}
exec "d = a * b + c" in myglobals, mylocals
myglobals {'a': 2, '__builtins__': {...}, 'b': 5} mylocals {'c': 3, 'd': 13}


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

Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave Reed | last post by:
I seem to remeber reading somewhere there was a statement you could put in your python program to stop its execution and start the interactive interpreter at that point (and then you could change...
1
by: Hung Jung Lu | last post by:
Hi, Is there a term for the ability of changing program code without the need of restarting the program? I mean, the most typical developer's cycle is: (a) write code (b) set break points,...
3
by: Tomas | last post by:
Hi! I'm looking for a replacement for the standard interactive python shell. So far I've tried IPython and PyCrust. I liked both, but I'm not 100% happy with any of them. My main complaint about...
2
by: siggy2 | last post by:
Hi All, (sorry for my bad english) I wrote a __tiny__ and __stupid__ recursive script directly into pythonwin interactive window with a time.sleep(1) and a print before each recursion... I...
22
by: SeeBelow | last post by:
Is there any way, in C, of interacting with a running program, but still using code that is portable, at least between linux and Windows? By "interacting" it could be something as simple as...
2
by: Allan Adler | last post by:
In C, I can execute system("gs gleep.ps"); and have C run a ghostscript program gleep.ps, which might ask for user input and, when it gets it, take some actions and report back to the C...
3
by: Pankaj | last post by:
I am facing a very basic problem as any new bie would face. I know perl and now i want to use python In perl, it is very simple , just "perl scriptname.pl" will execute the script. But i...
4
by: dan.gass | last post by:
Is there a way to temporarily halt execution of a script (without using a debugger) and have it put you in an interactive session where you have access to the locals? And possibly resume? For...
2
by: Rennie7676 | last post by:
Hi, I'm trying to write VC++ code for using Plink (command-line tool of PuTTY) interactively from within my program which is a console type windows application. The basic purpose is to run...
4
by: Summercool | last post by:
I just wonder if there is an official interactive PHP interpreter? kind of like the one IDLE for Python or irb for Ruby.
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.