从lin命令运行时,终止搅拌机,退出代码为“1”

2024-06-01 20:15:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用一个自动混合器python脚本,我想知道如何在发生异常时用退出代码1终止它。在

问题似乎是,即使python脚本失败,blender的退出代码始终为0。在

下面的脚本明确地生成一个非零的退出代码,但是blender将退出代码设置为0

def main():
    raise Exception("Fail")
    sys.exit(1)

我还尝试了--python exit code命令行参数,但没有效果:

^{pr2}$

这会得到一个稍微好一点的结果,因为我得到了以下信息:

Error: script failed, file: 'bake.py', exiting with code 2.

很遗憾,退出代码仍然是0。在

有谁能给我一些解释或解决方案,告诉我如何用正确的退出代码退出进程?在

非常感谢您的任何提示!在


Tags: 代码命令行脚本参数maindefsysexit
1条回答
网友
1楼 · 发布于 2024-06-01 20:15:09

如果使用 python调用的命令行脚本引发异常,而不是使用非零代码退出时, python-exit-code将设置退出代码。在

Set the exit-code in [0..255] to exit if a Python exception is raised (only for scripts executed from the command line), zero disables.

因此,唯一的解决方案是进行检查,并手动引发一个异常,然后捕捉它并在except块中退出。(如果不立即退出,退出代码将恢复为0。在

当我在blender插件中进行单元测试时,这段代码很有用。在

import unittest
from tests.my_test import MyTest
import sys

def run():
    suite = unittest.defaultTestLoader.loadTestsFromTestCase(MyTest)
    success = unittest.TextTestRunner().run(suite).wasSuccessful()
    if not success:
        raise Exception('Tests Failed')

try:
    run()
except Exception:
    sys.exit(1)

相关问题 更多 >