By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

python function in pipe

P: n/a
Hi everyone!

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.

Is there any solution?

Best regards
Bart.

Apr 28 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Apr 28, 6:37 am, Bart <u...@o2.plwrote:
Hi everyone!

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.

Is there any solution?

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.

Else you can wrap the module under test in another
module:

# mytest.py

import module_under_test

if __name__ == "__main__":
module_under_test.foo()

Then, using os.popen3 (or subprocess),
run "python mytest.py" as your command.

--
Hope this helps,
Steven

Apr 28 '07 #2

P: n/a
On Apr 28, 7:37 am, Bart <u...@o2.plwrote:
Hi everyone!

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.

Is there any solution?

Best regards
Bart.
Maybe something like this:

moduleA.py:
-----------
def someFunc():
print "hello"
raise ValueError

someFunc()
----------

import subprocess

print "Main executing"
try:
p = subprocess.Popen(["python", "6test.py"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)

result = p.wait()
if result == 0:
print "output:", p.stdout.read()
else:
print "output before error:", p.stdout.read()
print "error:", p.stderr.read()

except (OSError, TypeError, ValueError), e:
print "subprocess was never started"
print e
Apr 28 '07 #3

P: n/a
p = subprocess.Popen(["python", "6test.py"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
The file name is wrong there; it should be:

p = subprocess.Popen(["python", "moduleA.py"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)

Apr 28 '07 #4

P: n/a
En Sat, 28 Apr 2007 13:58:59 -0300, <at*************@gmail.comescribió:
On Apr 28, 6:37 am, Bart <u...@o2.plwrote:
>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
Apr 28 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.