[Python-bugs-list] [ python-Bugs-405837 ] getting PyRun_String() true result

nobody nobody@sourceforge.net
Mon, 05 Mar 2001 10:51:34 -0800


Bugs #405837, was updated on 2001-03-04 06:53
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=405837&group_id=5470

Category: Python Interpreter Core
Group: Feature Request
Status: Open
Priority: 5
Submitted By: Nobody/Anonymous
Assigned to: Nobody/Anonymous
Summary: getting PyRun_String() true result

Initial Comment:
It seems impossible to build am embedded Python
interpreter extension which actually allows getting
the result of the evaluation of a string from the
interpreter, as it is done in interactive mode, as in
the following:
  def f: pass
  f
prints:
  <function f at 0x8069694>

But in C (called twice with the 2 above strings):
  PyRun_String(string, Py_file_input, globals, globals)
returns None.

I found a workaround by patching the core in ceval.c,
eval_code2() (inspired by the PRINT_EXPR case):

...
case POP_TOP:
  v = POP();
PyDict_SetItemString(f->f_globals, "_", v); /* added */
  Py_DECREF(v);
  continue;
...

and then:
  PyRun_String(string, Py_file_input, globals, globals)
  result =PyDict_GetItemString(globals, "_")
returns the '<function f at 0x8069694>' correct result.


My goal is to allow the tclpython extension (at
http://jfontain.free.fr/) to work without having to
insert print statements on the Python side to be able
to pass data to the Tcl interpreter.

Please forgive me if there is an obvious way to do the
above without patching the core, but I am new to Python
(I like it already though :-)

Jean-Luc Fontaine

----------------------------------------------------------------------

Comment By: Nobody/Anonymous
Date: 2001-03-05 10:51

Message:
Logged In: NO 

> To evaluate a string, use Py_RunString with Py_eval_input,
> or perhaps Py_single_input.

Py_eval_input is for "isolated expressions", and
Py_single_input "for a single statement", so how do I
execute whole modules except by using Py_file_input, the
only remaining option?

I actually tested all the above options thoroughly and found
that only Py_file_input did the job, but without a way to
get at the result.

Please let me know whether there is something that I missed,
as I am stuck at the moment. If needed, I will be happy to
send you sample code that illustrates the problem.

Thank you very much for your prompt response.

Jean-Luc

PS: passing "def f(): pass\n" to Py_eval_input returns a
"SyntaxError: invalid syntax"


----------------------------------------------------------------------

Comment By: Martin v. Löwis
Date: 2001-03-04 10:22

Message:
Logged In: YES 
user_id=21627

Sure there is. PyRun_SimpleString executes a string in "file
mode"; this has no result. The interactive interpreter, when
it prints a result, runs the string in "eval mode" - only
evaluation gives a result.

To evaluate a string, use Py_RunString with Py_eval_input,
or perhaps Py_single_input.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=405837&group_id=5470