I have code executing in a subprocess that is supposed to raise an exception.
I would like to raise the same exception in the main process when the exception is returned from the subprocess (preferably while preserving the stack trace) but I am not sure how to do this.
I capture the stderr from the subprocess just fine but I can't find how to parse it so I get the type of exception. How would I accomplish this?
I am using python 2.7
main method
import subprocess
example=subprocess.Popen(["python","example.py"],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE)
return_obj, return_error = example.communicate()
if return_error:
# replace with error from subprocess
print "Should raise ",NameError('HiThere')
raise TypeError('Wrong type')
subprocess
raise NameError('HiThere')
import
ing instead of running another interpreter. Note you can still run any functions insideexample.py
in a separate process if you do so. – Breastif return_error: raise Exception, return_error
– Pruterexecfile()
to run the example.py script in the same process in order to get the exception as an object easily. – Gilliam