Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8822 > unrolled thread

embedding: how do I redirect print output?

Started byUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
First post2011-07-05 14:50 +0200
Last post2011-07-06 08:24 +0200
Articles 6 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  embedding: how do I redirect print output? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2011-07-05 14:50 +0200
    Re: embedding: how do I redirect print output? Thomas Jollans <t@jollybox.de> - 2011-07-05 16:18 +0200
    Re: embedding: how do I redirect print output? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-06 00:36 +1000
      Re: embedding: how do I redirect print output? Chris Angelico <rosuav@gmail.com> - 2011-07-06 01:20 +1000
      Re: embedding: how do I redirect print output? Andrew Berg <bahamutzero8825@gmail.com> - 2011-07-05 16:04 -0500
      Re: embedding: how do I redirect print output? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2011-07-06 08:24 +0200

#8822 — embedding: how do I redirect print output?

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2011-07-05 14:50 +0200
Subjectembedding: how do I redirect print output?
Message-ID<kkdbe8-eum.ln1@satorlaser.homedns.org>
Hi!

I'm trying to add some scripting capabilities to an application. Since it is 
a GUI application, I need some way to display output from Python. For 3.x, 
where "print" is a function, I'd just exchange this function with one that 
redirects the output to a log window, right.

However, I'm using 2.6 here, where that can't work. So, what I was thinking 
was to redirect "sys.stdout" and "sys.stderr" to a log window and possibly 
replace "sys.stdin" with something that causes meaningful errors if some 
code tries to use it, but that last point is just icing on the cake.

What seems cumbersome is that I'll need to write something that supports the 
file interface using C, which is still a bit awkward. I'm wondering, isn't 
there an easier way to achieve this? How would you do it?


Thank you!

Uli

-- 
Domino Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

[toc] | [next] | [standalone]


#8828

FromThomas Jollans <t@jollybox.de>
Date2011-07-05 16:18 +0200
Message-ID<mailman.629.1309875877.1164.python-list@python.org>
In reply to#8822
On 07/05/2011 02:50 PM, Ulrich Eckhardt wrote:
> I'm trying to add some scripting capabilities to an application. Since it is 
> a GUI application, I need some way to display output from Python. For 3.x, 
> where "print" is a function, I'd just exchange this function with one that 
> redirects the output to a log window, right.
> 
> However, I'm using 2.6 here, where that can't work. So, what I was thinking 
> was to redirect "sys.stdout" and "sys.stderr" to a log window and possibly 
> replace "sys.stdin" with something that causes meaningful errors if some 
> code tries to use it, but that last point is just icing on the cake.
> 
> What seems cumbersome is that I'll need to write something that supports the 
> file interface using C, which is still a bit awkward. I'm wondering, isn't 
> there an easier way to achieve this? How would you do it?

You can mess with low-level file descriptors. Example:

# create new stdout:
fout = file('out.txt', 'w')

# close stdout (fd 1)
import os
os.close(1)

# use fout as new stdout
os.dup2(fout.fileno(), 1)

# test. This will end up in out.txt
print "Testing."

# do the same for stdin (0) and stderr (2)

# - EOF -

However, I suggest you consider isolating your scripts from you main
program and run them in a separate interpreter. You can provide dummy
modules that communicate with your application via sockets to the
scripts. Setting the standard files in the child process is easy: the
Popen constructor takes stdin/stdout/stderr arguments.

-T

[toc] | [prev] | [next] | [standalone]


#8833

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-07-06 00:36 +1000
Message-ID<4e13217c$0$29993$c3e8da3$5496439d@news.astraweb.com>
In reply to#8822
Ulrich Eckhardt wrote:

> Hi!
> 
> I'm trying to add some scripting capabilities to an application. Since it
> is a GUI application, I need some way to display output from Python. For
> 3.x, where "print" is a function, I'd just exchange this function with one
> that redirects the output to a log window, right.
> 
> However, I'm using 2.6 here, where that can't work. So, what I was
> thinking was to redirect "sys.stdout" and "sys.stderr" to a log window and
> possibly replace "sys.stdin" with something that causes meaningful errors
> if some code tries to use it, but that last point is just icing on the
> cake.
> 
> What seems cumbersome is that I'll need to write something that supports
> the file interface using C, which is still a bit awkward. I'm wondering,
> isn't there an easier way to achieve this? How would you do it?

Why do you think it needs to be in C? As far as I can tell, so long as it
quacks like a file object (that is, has a write method), it should work.


>>> import StringIO, sys
>>> class Test:
...     def __init__(self):
...             self.out = sys.stdout
...             self.log = StringIO.StringIO()
...     def write(self, text):
...             self.log.write(text.upper())
...             self.out.write(''.join(reversed(text)))
...
>>> fake_out = Test()
>>> sys.stdout = fake_out
>>> print "Hello world"
dlrow olleH
>>> print "Goodbye cruel world!!!"
!!!dlrow leurc eybdooG
>>> sys.stdout = sys.__stdout__
>>> fake_out.log.getvalue()
'HELLO WORLD\nGOODBYE CRUEL WORLD!!!\n'



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#8837

FromChris Angelico <rosuav@gmail.com>
Date2011-07-06 01:20 +1000
Message-ID<mailman.636.1309879209.1164.python-list@python.org>
In reply to#8833
On Wed, Jul 6, 2011 at 12:36 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
>>>> print "Hello world"
> dlrow olleH
>

You, sir, have a warped and twisted mind.

And I love it!!

Now to secretly put code into some module somewhere and wait for
people to start tearing their hair out.... wait, did I say that out
loud?

ChrisA

[toc] | [prev] | [next] | [standalone]


#8860

FromAndrew Berg <bahamutzero8825@gmail.com>
Date2011-07-05 16:04 -0500
Message-ID<mailman.655.1309899864.1164.python-list@python.org>
In reply to#8833
On 2011.07.05 10:20 AM, Chris Angelico wrote:
> You, sir, have a warped and twisted mind.
>
> And I love it!!
>
> Now to secretly put code into some module somewhere and wait for
> people to start tearing their hair out.... wait, did I say that out
> loud?
from pytroll import print

[toc] | [prev] | [next] | [standalone]


#8909

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2011-07-06 08:24 +0200
Message-ID<5dbde8-fcm.ln1@satorlaser.homedns.org>
In reply to#8833
Steven D'Aprano wrote:
> Why do you think it [sink for use as sys.stdout] needs to be in C? As
> far as I can tell, so long as it quacks like a file object (that is,
> has a write method), it should work.

Good point & thanks for the example fish!

Uli


-- 
Domino Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web