python runtime_error ai_generated true

失败:未引发 <class 'SystemExit'> 意外异常:SystemExit(0)

Failed: DID NOT RAISE <class 'SystemExit'> UNEXPECTED EXCEPTION: SystemExit(0)

ID: python/pytest-doctest-capture-stdout

其他格式: JSON · Markdown 中文 · English
80%修复率
83%置信度
0证据数
2024-12-02首次发现

版本兼容性

版本状态引入弃用备注
7.x active — — —
8.x active — — —

根因分析

调用 sys.exit() 或带 --help 的 argparse 的 doctest 退出了解释器;doctest 期望返回值,而不是进程退出。

English

A doctest that calls sys.exit() or argparse with --help exits the interpreter; doctest expects a return value, not a process exit.

generic

解决方案

  1. 90% 成功率
    >>> import sys
    >>> try:
    ...     main(["--help"])
    ... except SystemExit as e:
    ...     print("exit", e.code)
    exit 0
  2. 95% 成功率
    def main(argv=None) -> int:
        ...
        return 0
    
    if __name__ == "__main__":
        raise SystemExit(main())

无效尝试

常见但无效的做法:

  1. 85% 失败

    Continues after failure but doesn't prevent SystemExit.

  2. 70% 失败

    In doctest source, suppress changes the observable output.

  3. 80% 失败

    Hides a real usability bug in the CLI.