473,486 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

an eval()-like exec()

Hi!

A python interactive interpreter works by having the user type in some
code, compiling and running that code, then printing the results. For
printing, the results are turned into strings.

I would like make an interpreter which does this, without the last
part: i.e. where the results are returned as objects, instead of as
strings. I.e. have I would like to see something that behaves like
this:
>>ip = MyInterpreter()
# this started a new interpreter
>>ip.run("import math") is None
True
>>ip.run("math.pi") is math.pi
True

Neither exec() or eval() is usable for this, as far as I see, because
eval can't handle arbitrary python code (eval("import math") ), and
exec() doesn't return the results.

Subclassing an code.InteractiveInterpreter or code.InteractiveConsole
seems like a usable idea, but I couldn't find out what to do to get
the results before they are turned into strings.

Using compile() and then eval() didn't seem usable either.

Any ideas?

Thanks in advance,
Daniel Abel

Aug 27 '07 #1
3 1660
A python interactive interpreter works by having the user type in some
code, compiling and running that code, then printing the results. For
printing, the results are turned into strings.

I would like make an interpreter which does this, without the last
part: i.e. where the results are returned as objects, instead of as
strings. I.e. have I would like to see something that behaves like
this:
>ip = MyInterpreter()
# this started a new interpreter
>ip.run("import math") is None
True
>ip.run("math.pi") is math.pi
True

Neither exec() or eval() is usable for this, as far as I see, because
eval can't handle arbitrary python code (eval("import math") ), and
exec() doesn't return the results.

Subclassing an code.InteractiveInterpreter or code.InteractiveConsole
seems like a usable idea, but I couldn't find out what to do to get
the results before they are turned into strings.

Using compile() and then eval() didn't seem usable either.

Any ideas?
Well, my first thought is that exec and eval serve two different
purposes, and you should just have both of them and use the
appropriate one based on the situation. However, I think it is
possible to enable the behavior you want:

Expand|Select|Wrap|Line Numbers
  1. def myeval(statement, globals_=None, locals_=None):
  2. try:
  3. return eval(statement, globals_, locals_)
  4. except SyntaxError:
  5. if locals_ is None:
  6. import inspect
  7. locals_ = inspect.currentframe().f_back.f_locals
  8. exec statement in globals_, locals_
  9.  
It seems to work for me.

Matt
Aug 27 '07 #2
En Mon, 27 Aug 2007 15:59:25 -0300, Abel Daniel <ab**@freemail.hu>
escribi�:
Hi!

A python interactive interpreter works by having the user type in some
code, compiling and running that code, then printing the results. For
printing, the results are turned into strings.
This last stage is done by calling sys.displayhook - you can replace that
function with your own one, and get the object to be printed.
Any of your previous attempts (using compile(...,"single")+eval, using
exec, or creating your own code.InteractiveInterpreter) should work with
this.

--
Gabriel Genellina

Aug 28 '07 #3
On Aug 27, 6:06 pm, "Matt McCredie" <mccre...@gmail.comwrote:
A python interactive interpreter works by having the user type in some
code, compiling and running that code, then printing the results. For
printing, the results are turned into strings.
I would like make an interpreter which does this, without the last
part: i.e. where the results are returned as objects, instead of as
strings. I.e. have I would like to see something that behaves like
this:
>>ip = MyInterpreter()
# this started a new interpreter
>>ip.run("import math") is None
True
>>ip.run("math.pi") is math.pi
True
Neither exec() or eval() is usable for this, as far as I see, because
eval can't handle arbitrary python code (eval("import math") ), and
exec() doesn't return the results.
Subclassing an code.InteractiveInterpreter or code.InteractiveConsole
seems like a usable idea, but I couldn't find out what to do to get
the results before they are turned into strings.
Using compile() and then eval() didn't seem usable either.
Any ideas?

Well, my first thought is that exec and eval serve two different
purposes, and you should just have both of them and use the
appropriate one based on the situation. However, I think it is
possible to enable the behavior you want:

Expand|Select|Wrap|Line Numbers
  1. def myeval(statement, globals_=None, locals_=None):
  2.     try:
  3.         return eval(statement, globals_, locals_)
  4.     except SyntaxError:
  5.         if locals_ is None:
  6.             import inspect
  7.             locals_ = inspect.currentframe().f_back.f_locals
  8.         exec statement in globals_, locals_
  9.  

It seems to work for me.

Matt
Unless it's something like:

raise_(SyntaxError)

where raise_ is a function equivalent to the corresponding statement.

Aug 28 '07 #4

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

Similar topics

6
1940
by: Paddy | last post by:
Hi, I got tripped up on the way eval works with respect to modules and so wrote a test. It seems that a function carries around knowledge of the globals() present when it was defined. (The...
9
8450
by: HikksNotAtHome | last post by:
This is a very simplified example of an Intranet application. But, is there an easier (or more efficient) way of getting the decimal equivalent of a fraction? The actual function gets the select...
33
3704
by: Stuart | last post by:
why won't the following work for(var i=0;i<pics;i++){ eval('img'+i) = new Image(wth,hgt) eval('img'+i+'.src') = 'http://www.mypics/'+i+'1.gif' } basically I am trying to create a numer of...
9
5481
by: Jim Washington | last post by:
I'm still working on yet another parser for JSON (http://json.org). It's called minjson, and it's tolerant on input, strict on output, and pretty fast. The only problem is, it uses eval(). It's...
0
3802
by: Michelle Keys | last post by:
Subject: DataBinder.Eval Error! Server Error in '/MSPOS' Application. ------------------------------------------------------------------------ -------- DataBinder.Eval:...
3
1887
by: Pauljh | last post by:
Hi All, I'm running some javascript over a server side generated web page and have multiple generated empty select statements, that I want to populate when the page is loaded. As HTML doesn't do...
8
2160
by: abhishek | last post by:
>>a,b=3,4 7 Now I want to evaluate y by substituting for the evaluated value of x. eval(y) will try to add "a+b" to 3 and return an error. I could do this, 10 but this becomes unwieldy if I...
13
3152
by: My Pet Programmer | last post by:
The way I usually set up and work with the XMLHttpRequest to execute server side functions and get results is this: var url = "someurl?params=" + params; var conn = createRequest(); // gets an...
6
5880
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
0
1203
by: Lie Ryan | last post by:
On Tue, 30 Sep 2008 16:04:34 -0500, William Purcell wrote: when you pass mydict, it is used as the global variables in the eval, right? Then, you passed a code to eval('...', mydict), sometimes...
0
7099
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5430
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4864
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3069
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.