caputering IO in python

David Bolen db3l at fitlinxx.com
Wed Aug 23 21:16:09 EDT 2000


Curtis Jensen <cjensen at bioeng.ucsd.edu> writes:

> Thanks.  However, I'm assuming this only works if the module is a python
> module.  My module is a C module.  Your idea might work if the module is
> purely C code too; I'm not sure.

Well, it would only work if the C module handled output by sending it
through the Python sys.stdout object which had been re-assigned.

>                                   However, my C module calls Fortran
> that does the writes to standard output.  The Fortran output is the
> output I wish to capture.  Is this possible?

Is this all linked into a single executable/DLL or by "calls Fortran"
do you mean running a separate executable?

If the former, it's likely pretty tricky, and these specific
suggestions are all untested, although I've done most of these
approaches in the past at one time or another.

You should be able to, in some manner, redirect the underlying stdout
win32 file handle other than to the console.  If the Fortran and C
share an RTL, you might be able to get away with C RTL functions like
dup2() to duplicate a copy of some other file handle If different RTLs
are used, you would probably have to go a level lower and reroute
stdout at the win32 level with SetStdHandle().  In either case, what
you'd want to do is reroute stdout to a new handle that either sent
the data to a file that you could read back in, or perhaps to a pipe
you create (serving just as an in-memory buffer) and then read from.

If the latter, then the easiest would be to use popen() to open a pipe
to the fortran process, and then read from the pipe to get the output.
If you are a GUI application, then the MSC popen() isn't going to work
right, and you'd need to fall back to the underlying win32
CreateProcess() function.

Or, in this case you may find it easier to get back out into Python
and use the win32 extensions that have working popens with a GUI, even
if you just then send the resulting data back into your C module.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list