使用 python 子进程处理断言对话框
我正在使用 python 创建一个子进程来检查并查看是否发生断言。
我想捕获错误输出以及返回代码。这工作正常,但我遇到的问题是,当它遇到断言时,它会给我一个挂在那里的对话框。在检索任何信息之前,我必须单击断言框。有没有办法让它不弹出并继续程序或发送消息关闭窗口?
这是一个问题,因为这是一项自动化服务。
import subprocess
pipe = subprocess.Popen('test2.exe', shell=True, stderr=subprocess.PIPE)
for line in pipe.stderr:
print line
该可执行文件是从 C++ 代码编译的,并且具有一个无法用于测试目的的断言。
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 = subprocess.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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一般来说,这并不是一个简单的解决方案,因为理论上程序可以创建任意数量的窗口等待用户输入。如果您有劣质进程的源代码,最简单的方法是将其修改为调用
_set_abort_behavior(0, _CALL_REPORTFAULT)
禁用消息框。如果你没有源代码,事情就会变得非常非常困难。您可能可以编写一个大型 hack,执行一些操作,例如将调试器附加到下级进程并在对
abort()
的调用上设置断点。如果遇到该断点,则终止该进程并返回适当的错误状态。但这是一个极端的、不平凡的拼凑。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.正如我在评论中提到的,断言弹出窗口在 Python 中并不常见。如果您可以修改在子进程中运行的代码,您也许能够捕获断言并自己处理它,而不是让环境通过弹出窗口来处理它。
如果它特别寻找断言,这应该可以工作,因为它将捕获断言,打印一条错误消息,然后引发一个简单的 SystemExit 异常。
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.
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.