Bug #79629 [Opn->Dup]: Generator duplicating first elements in 7.4.6
| From: | requinix@php.net | Date: | Mon, 25 May 2020 09:51:00 +0000 |
| Subject: | Bug #79629 [Opn->Dup]: Generator duplicating first elements in 7.4.6 | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-227153@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79629&edit=1
ID: 79629
Updated by: requinix@php.net
Reported by: thiago dot barcala at gmail dot com
Summary: Generator duplicating first elements in 7.4.6
-Status: Open
+Status: Duplicate
Type: Bug
Package: Unknown/Other Function
PHP Version: 7.4.6
Block user comment: N
Private report: N
New Comment:
Duplicate of bug #79600
Previous Comments:
------------------------------------------------------------------------
[2020-05-25 09:47:05] thiago dot barcala at gmail dot com
Description:
------------
It seems like the fix for https://bugs.php.net/bug.php?id=78434 changed the
behavior of generators.
In version 7.4.5 the attached test script gives the expected output, but on 7.4.6, iterating over
the generator yields the fist elements two times.
If I replace:
```
if ($termination) {
yield from $iterable;
return;
}
```
... with:
```
if ($termination) {
foreach ($iterable as $item) {
yield $item;
}
return;
}
```
It works as expected in both PHP versions.
Test script:
---------------
<?php
function flatten(iterable $iterable, $termination = false) {
if ($termination) {
yield from $iterable;
return;
}
foreach ($iterable as $item) {
yield from flatten($item, true);
}
}
function testGenerator() {
yield [1, 2];
}
echo 'Dumping with iterator_to_array:' . PHP_EOL;
var_dump(iterator_to_array(flatten(testGenerator())));
echo 'Echoing keys/elements with foreach:' . PHP_EOL;
foreach (flatten(testGenerator()) as $key => $item) {
echo "key $key => value $item\n";
}
Expected result:
----------------
Dumping with iterator_to_array:
array(2) {
[0]=>
int(1)
[1]=>
int(2)
}
Echoing keys/elements with foreach:
key 0 => value 1
key 1 => value 2
Actual result:
--------------
Dumping with iterator_to_array:
array(2) {
[0]=>
int(1)
[1]=>
int(2)
}
Echoing keys/elements with foreach:
key 0 => value 1
key 0 => value 1
key 1 => value 2
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79629&edit=1