Is PyRun_String() broken?
David Gravereaux
davygrvy at pobox.com
Tue Jun 5 06:40:55 EDT 2001
Martin von Loewis <loewis at informatik.hu-berlin.de> wrote:
>David Gravereaux <davygrvy at pobox.com> writes:
>
>> I've tried using Py_single_input for the start token, but the result of the
>> operation is sent to stdout. How is it possible to eval code and get the result
>> back? As far as I've able to RTFM and "use the source luke" is to determine
>> that eval_code2() when coming back out of the eval falls into the POP_TOP case
>> and does Py_DECREF(v) on the result to destroy it.
>>
>> I WANT IT! WHERE'D IT GO?
>
>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.
> 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:
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
^
SyntaxError: invalid syntax
% $i eval {eval("3+4")}
7
% $i eval {eval("def f: pass")}
python0: Traceback (most recent call last):
File "<string>", line 0, in ?
File "<string>", line 1
def f: pass
^
SyntaxError: invalid syntax
% $i eval {import sys}
python0: File "<string>", line 1
import sys
^
SyntaxError: invalid syntax
%
If I use Py_file_input, PyRun_String accepts the code, but no return value is
used. Just some string that say 'None'. I'll assume that means "no exception
generated".
D:\itcl_exp>tclsh84
% load tclpython20.dll
% set i [python::interp new]
python0
% $i eval {import sys}
None
% $i eval {sys.version}
None
% $i eval {3+4}
None
%
I'm at a loss for how to get python to accept arbitrary code, and give me 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?
>> If all that PyRun_String() returns is "exception or not", where/how is it
>> possible to get the last result of the operation? Is it true that POP_TOP in
>> eval_code2() of ceval.c discards the object before returning? How can I not pop
>> the top, and grab the result then Py_DECREF it after I'm done?
>
>You cannot. If you have a Py_file_input that has "computes some
>value", that file input should either assign to some variable, or be a
>function that you can call so that it returns a result.
It looks I'll have to dig inside the node compiler or do something very dirty to
achieve what I need. That last TOP_POP has to be stripped from the node, and I
must have it drop out of eval_code2() at that point so I can pop the top myself
and get the result.
So if I have to know the difference between a statement and an expression when
using PyRun_String to get a return value, can I assume that the evaluation of
arbitrary code for a result is a concept python doesn't support? I doubt it.
There's a way to do this somehow and I haven't found it yet.
>Why do you want the result of the last expression statement, anyway?
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. Is there another function that I could try instead?
--
David Gravereaux <davygrvy at pobox.com>
More information about the Python-list
mailing list