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. 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
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
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
Yeah. I got it.
exec "foo = 555" in globals(), locals() does the trick.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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.
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
| |