[Tutor] Redirecting Python Interpreter Output

Ketut Mahaindra KMahaindra@beijing.sema.slb.com
Sun, 11 Aug 2002 10:55:00 +0800


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

--Boundary_(ID_/hO1gunmB8QyqtTaxyfutA)
Content-type: text/plain; charset=iso-8859-1
Content-transfer-encoding: 7BIT

Hi,

Thanks, your suggestion give some shed of light to me ... :)

Since I am quite new to Python also,
I will need more in depth look on the StringIO class and
how to assign an object to the sys.stdout variable ... ;)

I will try it out, any help is greatly appreciated ...
especially if you're willing to make that "small apps" 
that you mentioned previously ... :)

-- 
best regards

ito

@> -----Original Message-----
@> From: Danny Yoo [mailto:dyoo@hkn.eecs.berkeley.edu]
@> Sent: Sunday, August 11, 2002 10:45 AM
@> To: Ketut Mahaindra
@> Cc: 'tutor@python.org'; 'SA'
@> Subject: RE: [Tutor] Redirecting Python Interpreter Output
@> 
@> 
@> 
@> 
@> On Sun, 11 Aug 2002, Ketut Mahaindra wrote:
@> 
@> > But now what if I want to catch the output from C++ ? 
@> Anybody got any
@> > experience on this ?
@> >
@> > So, for example I have a PyRun_SimpleString() or 
@> PyRun_SimpleFile or
@> > PyObject_CallObject on my C++ code ... Can I capture the 
@> output of the
@> > Python interpreter ?
@> 
@> Probably; what you can do is assign a file-like object to 
@> the 'sys.stdout'
@> variable of your embedded interpreter.  A 
@> StringIO.StringIO() instance
@> should do nicely.
@> 
@> Afterwards, once you allow the user to do a 
@> PyRun_SimpleString(), you can
@> pull out what was written to stdout by looking into the StringIO's
@> contents.
@> 
@> If you'd like, we can try to write a small sample 
@> application that does
@> this, although I've haven't played with embedding Python before!
@> 

--Boundary_(ID_/hO1gunmB8QyqtTaxyfutA)
Content-type: text/html; charset=iso-8859-1
Content-transfer-encoding: 7BIT

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2653.12">
<TITLE>RE: [Tutor] Redirecting Python Interpreter Output</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=2>Hi,</FONT>
</P>

<P><FONT SIZE=2>Thanks, your suggestion give some shed of light to me ... :)</FONT>
</P>

<P><FONT SIZE=2>Since I am quite new to Python also,</FONT>
<BR><FONT SIZE=2>I will need more in depth look on the StringIO class and</FONT>
<BR><FONT SIZE=2>how to assign an object to the sys.stdout variable ... ;)</FONT>
</P>

<P><FONT SIZE=2>I will try it out, any help is greatly appreciated ...</FONT>
<BR><FONT SIZE=2>especially if you're willing to make that &quot;small apps&quot; </FONT>
<BR><FONT SIZE=2>that you mentioned previously ... :)</FONT>
</P>

<P><FONT SIZE=2>-- </FONT>
<BR><FONT SIZE=2>best regards</FONT>
</P>

<P><FONT SIZE=2>ito</FONT>
</P>

<P><FONT SIZE=2>@&gt; -----Original Message-----</FONT>
<BR><FONT SIZE=2>@&gt; From: Danny Yoo [<A HREF="mailto:dyoo@hkn.eecs.berkeley.edu">mailto:dyoo@hkn.eecs.berkeley.edu</A>]</FONT>
<BR><FONT SIZE=2>@&gt; Sent: Sunday, August 11, 2002 10:45 AM</FONT>
<BR><FONT SIZE=2>@&gt; To: Ketut Mahaindra</FONT>
<BR><FONT SIZE=2>@&gt; Cc: 'tutor@python.org'; 'SA'</FONT>
<BR><FONT SIZE=2>@&gt; Subject: RE: [Tutor] Redirecting Python Interpreter Output</FONT>
<BR><FONT SIZE=2>@&gt; </FONT>
<BR><FONT SIZE=2>@&gt; </FONT>
<BR><FONT SIZE=2>@&gt; </FONT>
<BR><FONT SIZE=2>@&gt; </FONT>
<BR><FONT SIZE=2>@&gt; On Sun, 11 Aug 2002, Ketut Mahaindra wrote:</FONT>
<BR><FONT SIZE=2>@&gt; </FONT>
<BR><FONT SIZE=2>@&gt; &gt; But now what if I want to catch the output from C++ ? </FONT>
<BR><FONT SIZE=2>@&gt; Anybody got any</FONT>
<BR><FONT SIZE=2>@&gt; &gt; experience on this ?</FONT>
<BR><FONT SIZE=2>@&gt; &gt;</FONT>
<BR><FONT SIZE=2>@&gt; &gt; So, for example I have a PyRun_SimpleString() or </FONT>
<BR><FONT SIZE=2>@&gt; PyRun_SimpleFile or</FONT>
<BR><FONT SIZE=2>@&gt; &gt; PyObject_CallObject on my C++ code ... Can I capture the </FONT>
<BR><FONT SIZE=2>@&gt; output of the</FONT>
<BR><FONT SIZE=2>@&gt; &gt; Python interpreter ?</FONT>
<BR><FONT SIZE=2>@&gt; </FONT>
<BR><FONT SIZE=2>@&gt; Probably; what you can do is assign a file-like object to </FONT>
<BR><FONT SIZE=2>@&gt; the 'sys.stdout'</FONT>
<BR><FONT SIZE=2>@&gt; variable of your embedded interpreter.&nbsp; A </FONT>
<BR><FONT SIZE=2>@&gt; StringIO.StringIO() instance</FONT>
<BR><FONT SIZE=2>@&gt; should do nicely.</FONT>
<BR><FONT SIZE=2>@&gt; </FONT>
<BR><FONT SIZE=2>@&gt; Afterwards, once you allow the user to do a </FONT>
<BR><FONT SIZE=2>@&gt; PyRun_SimpleString(), you can</FONT>
<BR><FONT SIZE=2>@&gt; pull out what was written to stdout by looking into the StringIO's</FONT>
<BR><FONT SIZE=2>@&gt; contents.</FONT>
<BR><FONT SIZE=2>@&gt; </FONT>
<BR><FONT SIZE=2>@&gt; If you'd like, we can try to write a small sample </FONT>
<BR><FONT SIZE=2>@&gt; application that does</FONT>
<BR><FONT SIZE=2>@&gt; this, although I've haven't played with embedding Python before!</FONT>
<BR><FONT SIZE=2>@&gt; </FONT>
</P>

</BODY>
</HTML>

--Boundary_(ID_/hO1gunmB8QyqtTaxyfutA)--