Bug #79629 [NEW]: Generator duplicating first elements in 7.4.6
| From: | thiago dot barcala at gmail dot com | Date: | Mon, 25 May 2020 09:47:05 +0000 |
| Subject: | Bug #79629 [NEW]: Generator duplicating first elements in 7.4.6 | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-227152@lists.php.net to get a copy of this message | ||
From: thiago dot barcala at gmail dot com
Operating system:
PHP version: 7.4.6
Package: Unknown/Other Function
Bug Type: Bug
Bug description:Generator duplicating first elements in 7.4.6
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 bug report at https://bugs.php.net/bug.php?id=79629&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=79629&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=79629&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=79629&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=79629&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=79629&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=79629&r=support
Expected behavior: https://bugs.php.net/fix.php?id=79629&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=79629&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=79629&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=79629&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=79629&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=79629&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=79629&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=79629&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=79629&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=79629&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=79629&r=mysqlcfg