示例工程 17-generators-coroutines

17-generators-coroutines/03-pep479-stopiteration/README.md

markdown · 0.7 KB

# 03 · pep479-stopiteration
 
**演示**:生成器内部抛出/泄漏 `StopIteration` 会被 PEP 479 转成 `RuntimeError`;正确做法是 `return` 或捕获后返回;生成器的 `return value` 保存在 `StopIteration.value`。
**运行**:
 
```sh
uv run python examples/17-generators-coroutines/03-pep479-stopiteration/pep479_stopiteration.py
```
 
**预期输出**(3.12.9):
 
```text
raise: RuntimeError: generator raised StopIteration
next: RuntimeError: generator raised StopIteration
fixed: []
first: 1
return value: done
```
 
**依赖**:仅标准库。
**注意**:`for`/`list()` 会吞掉 `StopIteration.value`;需要返回值时显式捕获 `StopIteration` 或用 `yield from` 接收。