Is PyRun_String() broken?

Martin von Loewis loewis at informatik.hu-berlin.de
Tue Jun 5 12:40:33 EDT 2001


David Gravereaux <davygrvy at pobox.com> writes:

> >If you want the result of some computation, that computation better be
> >an expression; statements don't have a result.
> 
> Then I'll take an empty one.  That's fine.

Sorry, I can't follow here. An empty what?

> > So you should use Py_eval_input.

> I tried Py_eval_input, but I can't send it arbitrary code, such as
> "import sys".  I get this for doing it:

Of course not. When evaluating expressions, you must pass it an
expression. import is a statement, not an expression. You cannot
evaluate statements - you can only execute them.

> D:\itcl_exp>tclsh84
> % load tclpython20.dll
> % set i [python::interp new]
> python0
> % $i eval {3+4}
> 7
> % $i eval {def f: pass}
> python0:   File "<string>", line 1
>     def f: pass

This is the central problem. Your python::interp object should have
two commands: exec and eval.

> I'm at a loss for how to get python to accept arbitrary code, and give me a
> result.

You cannot. Arbitrary code does not have a result.

> I can understand 'import sys' having an empty return, but I can not
> accept nothing in return for 'sys.version'.  Does my problem make
> sense?

Not if you have the Python syntax in mind. What result do you expect from

sys.version
print sys.argv

> It looks I'll have to dig inside the node compiler or do something
> very dirty to achieve what I need.

I claim that you don't need to achieve this. Just give your Tcl object
two different commands, for the two different syntactic forms.


> I want the same concept that Perl and Tcl have for doing an eval.  It seems
> python doesn't have the concept of returning values for an eval of arbitrary
> code using PyRun_String.  

Right. It can only evaluate expressions, not arbitrary code. Arbitrary
code it can only execute.

> Is there another function that I could try instead?

No.

Regards,
Martin



More information about the Python-list mailing list