Inconsistent behaviour with AppendIterator and empty generators
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- C
- Estrellas
- 40.4k
- Forks
- 8.1k
- Merge medio
- 2 d 13 h
- PR fusionados (30 d)
- 96
Descripción
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
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza reproduciendo los tres escenarios de AppendIterator con los generadores yieldEmpty y yieldRows en PHP 8.2.7, centrándote en el fallo durante foreach y AppendIterator->rewind(). Determina si el comportamiento diferente es intencionado o si se trata de un error y, a continuación, añade o actualiza la cobertura de regresión para que el caso del generador vacío tenga un comportamiento definido y satisfactorio.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- php
- Área
- backend
- Tipo de issue
- Error
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Estado de actividad
- Activo
- Claridad
- Bastante claro
- Aptitud para principiantes
- 55/100