python function in pipe

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Apr 28 15:48:21 EDT 2007


En Sat, 28 Apr 2007 13:58:59 -0300, <attn.steven.kuo at gmail.com> escribió:

> On Apr 28, 6:37 am, Bart <u... at o2.pl> wrote:
>> Im using module that gives errors to stderr/stdout  (generated by SWIG)
>> Problem is that I need to parse this errors/information from module.
>> os.popen3 looks nice but this executes  command  not  function.
>
> Perhaps you're looking for the unittest module?
>
> You can use assertRaises if an exception is raised
> by the function.
> You can also redirect sys.stderr and sys.stdout as needed
> and test the captured strings with assertEquals.

Maybe I'm wrong, but I think this won't help the OP with his problem.
As I understand it, he has a C function that uses stdout and stderr to  
report errors (perhaps using printf, fprintf, perror and similar  
functions) and he wants to call it from Python. Reassigning  
sys.stdout/stderr in Python won't help.

One way would be to make an executable from the C code, and call it using  
the subprocess module or some popen variant. How to pass arguments depends  
on the application, and may not be efficient.

Another way would be to change the C code, adding a FILE parameter (let's  
call it logfile) and replacing every printf("...", xxx) with  
fprintf(logfile, "...", xxx); the same for related functions.

Yet another way, and perhaps the easiest and less intrusive on the C code,  
would be to use freopen to replace stdout and stderr with another file  
(and going back again at the end!).

-- 
Gabriel Genellina



More information about the Python-list mailing list