# test_x 的 teardown 期间出错
RuntimeError: 生成器在 throw() 之后未停止
处理上述异常时又出现另一个异常:
ValueError: 对已关闭文件进行 I/O 操作

- **ID:** `python/pytest-fixture-finalizer-exception`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

基于生成器的 fixture 在裸 except 或抛异常的 finally 中吞掉了 GeneratorExit/异常，阻止了正确的 teardown。pytest 随后报告 teardown 错误，掩盖了真正的测试结果。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 7.x | active | — | — |
| 8.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   Never catch BaseException or bare except in a yield fixture. Use try/finally:
  @pytest.fixture
  def res():
      r = acquire()
      try:
          yield r
      finally:
          r.close()
   ```
2. **** (90% 成功率)
   ```
   Use addfinalizer for complex cleanup:
  @pytest.fixture
  def res(request):
      r = acquire()
      request.addfinalizer(r.close)
      return r
   ```

## 无效尝试

- **** — If the except swallows GeneratorExit, the 'generator didn't stop' error persists. (80% 失败率)
- **** — Loses cleanup; resources leak across tests. (70% 失败率)
