如何确定程序是否通过Python(理想情况下是子进程)运行崩溃或成功结束

问题描述 投票:1回答:1

我试图通过使用Python测试一些程序。我想看看是否给出了某些输入它们会崩溃,结束没有错误,或者运行时间超过超时。

理想情况下,我想使用子进程,因为我熟悉它。但是我能够使用任何其他有用的库。我认为阅读核心转储通知是一种选择,但我还不知道该怎么做,也不知道这是否是最有效的方法。

python subprocess
1个回答
0
投票

使用osWhat is the return value of os.system() in Python?,解决方案可能是:

status = os.system(cmd)
# status is a 16 bit number, which first 8 bits from left(lsb) talks about signal used by os to close the command, Next 8 bits talks about return code of command.
sig, ret = os.WIFSIGNALED(status), os.WEXITSTATUS(status)
# then check some usual problems:
if sig:
    if status == 11:      # SIGSEGV
        print ('crashed by segfault')
    elif status == 6 :    # SIGABRT
        print('was aborted')
    else: # 14, 9 are related to timeouts if you like them
        print('was stopped abnormally with', status)
else:
    print('program finished properly')

我还没有检查子进程是否返回相同的状态。

© www.soinside.com 2019 - 2024. All rights reserved.