Re: Cactching Stdout

2006-11-08 Thread Stephan Kuhagen
Dennis Lee Bieber wrote:

>> popen...is this what I need? How can I use them? It is very important
>> for me that I could take the output in real-time.
>> Thanks for the help!
>> Massi
> 
> That's going to be difficult... popen and pipes work when one
> process starts another INDEPENDENT process.

I'm not sure, if it fits the problem, but just a few days ago I had the
problem to catch the stdout and stderr of a module to process it before
writing it to the console. What I did was basically this:

---
import sys
import StringIO

# save the original streams
_stdout_ = sys.stdout
_stderr_ = sys.stderr

# create StringIO string buffers to replace streams
sys.stdout = StringIO.StringIO()
sys.stderr = StringIO.StringIO()

# Now print something, this goes to the string buffers instead of console
print "Hello World to stdout"
sys.stderr.write("Hello World to stderr\n")

# Now process the contents of the buffers...
...
# ...and print them to the real console afterwards
_stdout_.write(sys.stdout)
_stderr_.write(sys.stderr)

# Clean up the string buffers for the next IO
sys.stdout.truncate(0)
sys.stderr.truncate(0)
---

Of course you should put all that into defs and create own write/print
functions to encapsulate the whole buffering/processing/cleanup.

This works great for scripting, but I'm pretty sure, that it does not work
for C Libraries in Python. But I do not know, how Python handles its
streams internally, so it might be worth a try.

Regards
Stephan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cactching Stdout

2006-11-08 Thread Gabriel Genellina

At Wednesday 8/11/2006 06:09, Massi wrote:


Hi everyone! I'm writing a python script which uses a C-written dll. I
call the functions in the dll using ctypes, but I don't know how to
catch the output of the "printf" which the C functions use. In fact I
don't even know if it is possible! I've heard something about PIPE and
popen...is this what I need? How can I use them? It is very important
for me that I could take the output in real-time.


Since you are calling a function the same process, popen&co won't help.
This is just an idea; printf is writting to STDOUT; you could replace 
STDOUT with the write end of a pipe, and read from the other end.



--
Gabriel Genellina
Softlab SRL 


__
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis! 
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
-- 
http://mail.python.org/mailman/listinfo/python-list

Cactching Stdout

2006-11-08 Thread Massi
Hi everyone! I'm writing a python script which uses a C-written dll. I
call the functions in the dll using ctypes, but I don't know how to
catch the output of the "printf" which the C functions use. In fact I
don't even know if it is possible! I've heard something about PIPE and
popen...is this what I need? How can I use them? It is very important
for me that I could take the output in real-time.
Thanks for the help!
Massi

-- 
http://mail.python.org/mailman/listinfo/python-list