<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div>On May 8, 2012, at 3:07 PM, F L wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">
<div class="hmmessage"><div dir="ltr">
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style>
<div dir="ltr"><div><font size="2">Hello everyone,</font></div><div><font size="2"><br></font></div><div><font size="2">We are trying to implement our own interactive interpreter in our application</font></div><div><font size="2">using an embedded Python interpreter.</font></div><div><font size="2"><br></font></div><div><font size="2">I was wondering what would be the best way to retreive as text the result of </font></div><div><font size="2">executing Python code. The text must be exactly the same as it would be in the</font></div><div><font size="2">standalone interpreter.</font></div><div><font size="2"><br></font></div><div><font size="2">We used to do this by changing the embedded interpreter's sys.stdout and sys.stderr</font></div><div><font size="2">with a FILE created in C++. We could then execute code using the PyRun_InteractiveOne</font></div><div><font size="2">function and easily retrieve the result from the FILE. The problem is, our application</font></div><div><font size="2">shouldn't create any files if possible.</font></div><div><font size="2"><br></font></div><div><font size="2">We also tried replacing sys.stdout and sys.stderr with a custom made Python object</font></div><div><font size="2">which could be more easily read from C++. We then used the function PyRun_String</font></div><div><font size="2">to execute the code. The problem here is that using the Py_file_input start token</font></div><div><font size="2">doesn't write everything we need to sys.stdout. (i.e. executing 2+2 does not write 4).</font></div><div><font size="2">It also doesn't print a traceback to sys.stderr when there is an exception. Using the</font></div><div><font size="2">other two start tokens is impossible since we need to be able to execute more than </font></div><div><font size="2">one instruction at once. We also tried using the function PyRun_SimpleString but we</font></div><div><font size="2">encounter the same problems.</font></div><div><font size="2"><br></font></div><div><font size="2">We are using python 2.7. </font></div><div><font size="2"><br></font></div><div><font size="2">Any suggestions? </font></div><div><font size="2"><br></font></div><div><font size="2">Thank you for your time.</font></div></div></div></div></blockquote><br></div><div>[byte]</div><div><br></div><div>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?</div><div><br></div><div>Documented here: <a href="http://docs.python.org/library/subprocess.html">http://docs.python.org/library/subprocess.html</a> </div><div><br></div><div>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.</div><div><br></div><div>-Bill</div><br></body></html>