开发者

Handle assertion dialog box with python subprocess

开发者 https://www.devze.com 2023-03-23 12:27 出处:网络
I am using python to create a sub process to check and see that no assertions occur. I want to catch the error output along with the return code. That works fine, but the problem I run into is that

I am using python to create a sub process to check and see that no assertions occur.

I want to catch the error output along with the return code. That works fine, but the problem I run into is that when it runs into the assertion it gives me a dialog box that just hangs there. I have to then click the assertion box before I retrieve any information. Is there a way to make it not pop up and continue with the program or to send a message to close the window?

This is a problem since this is an automation service.

import subprocess

pipe = subpr开发者_如何转开发ocess.Popen('test2.exe', shell=True, stderr=subprocess.PIPE)

for line in pipe.stderr:
    print line

The executable is compiled from c++ code and has an assertion that will fail for testing purposes.


There's not really an easy solution in general, since a program could in theory create any number of windows waiting for user input. If you have the source code for the inferior process, the easiest thing to do would be to modify it to call _set_abort_behavior(0, _CALL_REPORTFAULT) to disable the message box.

If you don't have the source code, it's going to be much, much tougher. You could probably write a big hack that did something like attaching a debugger to the inferior process and setting a breakpoint on the call to abort(). If that breakpoint gets hit, kill the process and return an appropriate error status. But that's an extreme non-trivial kludge.


As I mentioned in the comments, pop-ups for assertions isn't a normal thing in Python. If you can modify the code running in a subprocess, you might be able to capture the assertion and handle it yourself, rather than letting the environment handle it with a popup.

import sys

try:
    code that raises the assertion
catch AssertionError, e:
    sys.stderr.write("Assertion failed: " + str(e))
    sys.exit(1)

If it's looking for assertions in particular, this should work, because it will capture the assertion, print an error message and then raise a simple SystemExit exception instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消