Capturing errors raised by other scripts ?

N

northof40

I'm using the subroutine module to run run python script A.py from
B.py (this is on windows fwiw).

A.py is not my script and it may raise arbitary errors before exiting.
How can I determine what's happened before A.py exited ?

To simulate this I've got this script (which is meant to simulate
A.py):

class customError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)

try:
raise customError(2*2)
except customError as e:
print 'Custom exception occurred, value:', e.value


I then run my A.py like this :
.... pathtojob="python.exe A.py"
.... p = subprocess.Popen(pathtojob, stderr=fdErr, stdout=fdOut)
.... except:
.... print "bad stuff happened"
....

When I do this I the exception handler is not fired and the text
"Custom exception occurred, value: 4" ends up in the stdout file.

I'd really like it to end up in stderr because then I could say
"anything in stderr ? then ignore the output and flag an error".

I don't want to have to parse the stdout for error like messages and I
can't make changes to A.py.

I'm sure there's a better way to do this - can anyone offer some
advice ?

thanks

Richard.
 
M

MRAB

northof40 said:
I'm using the subroutine module to run run python script A.py from
B.py (this is on windows fwiw).

A.py is not my script and it may raise arbitary errors before exiting.
How can I determine what's happened before A.py exited ?

To simulate this I've got this script (which is meant to simulate
A.py):

class customError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)

try:
raise customError(2*2)
except customError as e:
print 'Custom exception occurred, value:', e.value


I then run my A.py like this :

... pathtojob="python.exe A.py"
... p = subprocess.Popen(pathtojob, stderr=fdErr, stdout=fdOut)
... except:
... print "bad stuff happened"
...

When I do this I the exception handler is not fired and the text
"Custom exception occurred, value: 4" ends up in the stdout file.

I'd really like it to end up in stderr because then I could say
"anything in stderr ? then ignore the output and flag an error".

I don't want to have to parse the stdout for error like messages and I
can't make changes to A.py.

I'm sure there's a better way to do this - can anyone offer some
advice ?

thanks
A.py is printing the error message.

The 'print' statement prints to stdout unless you direct it elsewhere
with '>>', for example:

print >> my_file, 'message'

If you don't want error messages to go to stdout, then don't print them
to stdout, it's as simple as that!
 
N

northof40

A.py is printing the error message.

The 'print' statement prints to stdout unless you direct it elsewhere
with '>>', for example:

     print >> my_file, 'message'

If you don't want error messages to go to stdout, then don't print them
to stdout, it's as simple as that!

Thanks for your reply. I take your point about print - perhaps I
hadn't really thought that through. Unfortunately I don't have control
of the real script that's being run so if the programmer of that
script has used print there's nothing I can do about it.

Perhaps I could modify the question. If A.py terminates due an
unhandled exception is there anyway for B.py to know that's happened
(without A.py's cooperation ?).

thanks

Richard.


If A.py terminates due to an unhandled exception
 
L

Lie Ryan

Not really; not all that is written to stderr signifies errors per se.
Warning and Debugging info are often written to stderr as well.
Thanks for your reply. I take your point about print - perhaps I
hadn't really thought that through. Unfortunately I don't have control
of the real script that's being run so if the programmer of that
script has used print there's nothing I can do about it.

Tracebacks is almost always written to stderr, not stdout; unless the
programmer explicitly redirect the traceback into stdout.
Perhaps I could modify the question. If A.py terminates due an
unhandled exception is there anyway for B.py to know that's happened
(without A.py's cooperation ?).

A well-behaved program will exit with status code 0; if and only if it
exited cleanly. I believe a python program that exited due to unhandled
exception always return non-zero status code.

However, if the program handled an error and called exit(0) explicitly
or the execution of the program falls to the end of the module; there is
no way to distinguish it from regular clean exit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top