Bug #75819 [NEW]: Generators have different behavior than Iterators
| From: | php at fleshgrinder dot com | Date: | Mon, 15 Jan 2018 17:44:02 +0000 |
| Subject: | Bug #75819 [NEW]: Generators have different behavior than Iterators | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-213551@lists.php.net to get a copy of this message | ||
From: php at fleshgrinder dot com
Operating system: -
PHP version: 7.2.1
Package: Class/Object related
Bug Type: Bug
Bug description:Generators have different behavior than Iterators
Description:
------------
Generators behave differently than Iterators when calling valid on them
followed by a loop. A non-rewindable iterator can be asked if it is
valid by calling valid on it followed by a loop. The same is not
possible with a Generator because it throws an exception stating that an
already closed generator cannot be iterated.
This is extremely hard to debug because the problem is not apparent at
all to the reader of the code.
Iterator code: https://3v4l.org/v0qAZ
Generator code: https://3v4l.org/2E7EJ
Also, note that HHVM/Hack behaves as expected.
Test script:
---------------
$it = (function () { if (false) { yield 'foo'; }})();
var_dump($it->valid());
foreach ($it as $k => $v) { var_dump($k, $v); }
Expected result:
----------------
bool(false)
Actual result:
--------------
bool(false)
Fatal error: Uncaught Exception: Cannot traverse an already closed
generator in /in/2E7EJ:7
Stack trace:
#0 {main}
thrown in /in/2E7EJ on line 7
Process exited with code 255.
--
Edit bug report at https://bugs.php.net/bug.php?id=75819&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=75819&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=75819&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=75819&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=75819&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=75819&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=75819&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=75819&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=75819&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=75819&r=support
Expected behavior: https://bugs.php.net/fix.php?id=75819&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=75819&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=75819&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=75819&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=75819&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=75819&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=75819&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=75819&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=75819&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=75819&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=75819&r=mysqlcfg