Inconsistent behaviour with AppendIterator and empty generators
まだ誰も着手していません。
- 主要言語
- C
- スター
- 40.4k
- フォーク
- 8.1k
- 平均マージ
- 2日 13時間
- マージ済み PR(30日)
- 96
説明
Description
I've encountered inconsistent behaviour in the handling of empty generators with AppendIterator. Please can you confirm whether this is a bug or some weird expected behaviour.
Scenario 1
When iterating over a generator that yields nothing, foreach has no items to iterate over so var_dump isn't called, as expected.
<?php
function yieldEmpty()
{
yield from [];
}
foreach (yieldEmpty() as $item) {
var_dump($item);
}
Scenario 2
When attaching a generator yielding items and a generator yielding nothing and attaching both to an AppendIterator, foreach iterates over the items from the former and the three values are output, as expected.
<?php
function yieldRows()
{
yield from ['one', 'two', 'three'];
}
function yieldEmpty()
{
yield from [];
}
$iterator = new AppendIterator();
$iterator->append(yieldRows());
$iterator->append(yieldEmpty());
foreach ($iterator as $item) {
var_dump($item);
}
Outputs (as expected):
string(3) "one"
string(3) "two"
string(5) "three"
Scenario 3
When attaching ONLY a generator yielding nothing and attaching it to an AppendIterator, foreach unexpectedly fails with an exception.
<?php
function yieldEmpty()
{
yield from [];
}
$iterator = new AppendIterator();
$iterator->append(yieldEmpty());
foreach ($iterator as $item) {
var_dump($item);
}
Expected output (following on from scenario 1 and 2):
string(3) "one"
string(3) "two"
string(5) "three"
Actual output:
PHP Fatal error: Uncaught Exception: Cannot traverse an already closed generator in /Users/richard.coupland/test3.php:11
Stack trace:
#0 /Users/richard.coupland/test3.php(11): AppendIterator->rewind()
#1 {main}
thrown in /Users/richard.coupland/test3.php on line 11
Fatal error: Uncaught Exception: Cannot traverse an already closed generator in /Users/richard.coupland/test3.php:11
Stack trace:
#0 /Users/richard.coupland/test3.php(11): AppendIterator->rewind()
#1 {main}
thrown in /Users/richard.coupland/test3.php on line 11
PHP Version
8.2.7
Operating System
No response
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、PHP 8.2.7 で yieldEmpty および yieldRows ジェネレーターを使用して 3 つの AppendIterator シナリオを再現し、foreach と AppendIterator->rewind() 中に発生する失敗に焦点を当てます。異なる挙動が意図されたものかバグかを判断し、その後、空のジェネレーターのケースで定義済みの成功する挙動になるよう、回帰テストのカバレッジを追加または更新します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- php
- 領域
- backend
- issue の種類
- バグ
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 活発
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 55/100