471,596 Members | 810 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 1582
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Paddy | last post: by
9 posts views Thread by HikksNotAtHome | last post: by
33 posts views Thread by Stuart | last post: by
9 posts views Thread by Jim Washington | last post: by
reply views Thread by Michelle Keys | last post: by
3 posts views Thread by Pauljh | last post: by
8 posts views Thread by abhishek | last post: by
13 posts views Thread by My Pet Programmer | last post: by
reply views Thread by Lie Ryan | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

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.