Retrieving result from embedded execution
William R. Wing (Bill Wing)
wrw at mac.com
Tue May 8 15:44:01 EDT 2012
On May 8, 2012, at 3:07 PM, F L wrote:
> Hello everyone,
>
> We are trying to implement our own interactive interpreter in our application
> using an embedded Python interpreter.
>
> I was wondering what would be the best way to retreive as text the result of
> executing Python code. The text must be exactly the same as it would be in the
> standalone interpreter.
>
> We used to do this by changing the embedded interpreter's sys.stdout and sys.stderr
> with a FILE created in C++. We could then execute code using the PyRun_InteractiveOne
> function and easily retrieve the result from the FILE. The problem is, our application
> shouldn't create any files if possible.
>
> We also tried replacing sys.stdout and sys.stderr with a custom made Python object
> which could be more easily read from C++. We then used the function PyRun_String
> to execute the code. The problem here is that using the Py_file_input start token
> doesn't write everything we need to sys.stdout. (i.e. executing 2+2 does not write 4).
> It also doesn't print a traceback to sys.stderr when there is an exception. Using the
> other two start tokens is impossible since we need to be able to execute more than
> one instruction at once. We also tried using the function PyRun_SimpleString but we
> encounter the same problems.
>
> We are using python 2.7.
>
> Any suggestions?
>
> Thank you for your time.
[byte]
I'm pretty new to Python myself, and I may not understand what you are trying to do, but have you looked at the subprocess module?
Documented here: http://docs.python.org/library/subprocess.html
Using subprocess.Popen would let you hook stdin and staout directly to pipes that will pass data back to the calling program with no intermediate steps.
-Bill
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120508/d9783fca/attachment-0001.html>
More information about the Python-list
mailing list