473,471 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

python function in pipe

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
4 2434
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
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 = 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: pepericou | last post by:
I'm trying to developp an application which use Zope and DCOracle2 module to access Oracle database. I've install the DCOracle2 product on Windows XP, but when I create a python function to access...
1
by: David Gilbert | last post by:
I'm trying to maintain the interface to KQueue for FreeBSD. At one point, the C module stores an opaque object in a C structure such that the opaque pointer can be used later by the application. ...
2
by: lamthierry | last post by:
Let's say I have a python function do some math like the following: def doMath(self): self.val = self.val + 1 How can I call this python function from C++? Assuming I have some sort of...
1
by: sndive | last post by:
Is there a better way to make a call from C than PyRun_SimpleString("import foo_in_python\nfoo_in_python.bar(whatever)\n"); ? I already imported the foo_in_python using...
0
varuns
by: varuns | last post by:
if i need to call a python function from c, i can use PyImport _Import() python-c API. Following code shows calling python function "add1" from python module "def1" int add(int x, int y) { ...
5
by: fernando | last post by:
Could someone post an example on how to register a python function as a callback in a C function? It expects a pointer to PyObject... how do I expose that? Basically, the signature of the function...
3
by: parthpatel | last post by:
hello how can i call python function from xul ??? i m trying to call python from xul how can i do that ??
0
by: grbCPPUsr | last post by:
I am new to Python. I would like to use Python for the specialized purpose of dynamic expressions parsing & evaluation in my C++ application. I would like to encapsulate the expressions to be...
9
by: grbgooglefan | last post by:
I am trying to pass a C++ object to Python function. This Python function then calls another C++ function which then uses this C++ object to call methods of that object's class. I tried...
2
by: natachai | last post by:
I am new in python and pysqlite. Right now, I am reallly trying to figure it out the way that I can make python function read the table in sqlite database and calculate data using SQL language. ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.