python
runtime_error
ai_generated
partial
运行时错误:生成器在 throw() 后没有停止
RuntimeError: generator didn't stop after throw()
ID: python/pytest-fixture-yield-without-finally
80%修复率
81%置信度
0证据数
2024-09-14首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.10 | active | — | — | — |
根因分析
在 fixture 的 yield 之后没有正确处理异常,导致 teardown 代码未执行或生成器未正确关闭。
English
在 fixture 的 yield 之后没有正确处理异常,导致 teardown 代码未执行或生成器未正确关闭。
解决方案
-
90% 成功率 确保 yield 后没有异常中断
@pytest.fixture def my_fixture(): setup() yield resource teardown() -
95% 成功率 使用 try/finally 确保清理
@pytest.fixture def my_fixture(): try: yield resource finally: teardown()
无效尝试
常见但无效的做法:
-
yield 后直接写清理代码
70% 失败
尝试在 yield 后直接写清理代码,但异常发生时清理代码不执行
-
在 finally 中再次 yield
50% 失败
尝试使用 try/finally 但 finally 中又 yield 导致错误