Bug #72692 [Com]: Bad behaviour of AppendIterator with Empty Generator

From: Date: Tue, 17 Sep 2019 15:03:21 +0000
Subject: Bug #72692 [Com]: Bad behaviour of AppendIterator with Empty Generator
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-222785@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72692&edit=1 ID: 72692 Comment by: f dot bosch at genkgo dot nl Reported by: pierrick@php.net Summary: Bad behaviour of AppendIterator with Empty Generator Status: Open Type: Bug Package: SPL related PHP Version: 5.6.24 Block user comment: N Private report: N New Comment: I hit this bug too today, still valid in PHP7.4. See https://3v4l.org/R4AH3. For others who hit this bug: use a closure and yield from as alternative. function createEmptyGen1() { if(false) { yield 1; } } function createEmptyGen2() { if(false) { yield 2; } } $ai = (function () { yield from createEmptyGen1(); yield from createEmptyGen2(); })(); foreach ($ai as $_); Previous Comments: ------------------------------------------------------------------------ [2016-07-28 23:59:57] nikic@php.net Related: Bug #71436 ------------------------------------------------------------------------ [2016-07-28 05:09:13] pierrick@php.net Description: ------------ When giving an new empty generator, AppendIterator will directly throw an Exception "Cannot traverse an already closed generator" even if the given generator is new. Test script: --------------- <?php function createEmptyGen() { if(false) { yield 1; } } $gen = createEmptyGen(); $ai = new AppendIterator(); $ai->append($gen); var_dump(iterator_to_array($gen)); Expected result: ---------------- array(0) { } Actual result: -------------- Fatal error: Uncaught Exception: Cannot traverse an already closed generator in /home/pierrick/php-src/github/foo.php:14 Stack trace: #0 /home/pierrick/php-src/github/foo.php(14): iterator_to_array(Object(Generator)) #1 {main} thrown in /home/pierrick/php-src/github/foo.php on line 14 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=72692&edit=1

« previous php.bugs (#222785) next »