embedding: how do I redirect print output?

  • Thread starter Ulrich Eckhardt
  • Start date
U

Ulrich Eckhardt

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
 
T

Thomas Jollans

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
 
S

Steven D'Aprano

Ulrich said:
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.

.... 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)))
....'HELLO WORLD\nGOODBYE CRUEL WORLD!!!\n'
 
C

Chris Angelico

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
 
A

Andrew Berg

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
 
U

Ulrich Eckhardt

Steven said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top