Bug #77515 [NEW]: A generator is closed by making a call to Generator::valid

From: Date: Thu, 24 Jan 2019 12:57:41 +0000
Subject: Bug #77515 [NEW]: A generator is closed by making a call to Generator::valid
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-219168@lists.php.net to get a copy of this message
From:             knyjoh at gmail dot com
Operating system: 
PHP version:      7.3.1
Package:          *General Issues
Bug Type:         Bug
Bug description:A generator is closed by making a call to Generator::valid

Description:
------------
A generator is closed by making a call to Generator::valid

It is not the expected behavior that a call to valid() will actually
change a state. If it is considered a correct behavior it should be
documented.
Note that the same problem exists with the current() function.

In example 1 it is clear that when reaching the loop the generator is
not closed, because the loop does not fail. Traversing a closed
generator is not allowed and would throw an exception, which means that
the generator must be open at this stage.

In example 2 however, when reaching the loop an exception is thrown
"Cannot traverse an already closed generator". The generator has
therefore been closed by the call to valid().

Test script:
---------------
1. <?php
2.
3. function generator() {
4.   if (false) {
5.     yield 1; // Force generator
6.   }
7. }
8.
9. // Example 1
10. $values = generator();
11. foreach ($values as $value) {} // This is ok
12.
13. // Example 2
14. $values = generator();
15. $values->valid();
16. foreach ($values as $value) {} // This is not ok

Actual result:
--------------
Fatal error: Uncaught Exception: Cannot traverse an already closed
generator in /in/ab0KK:16
Stack trace:
#0 {main}
  thrown in /in/ab0KK on line 16

-- 
Edit bug report at https://bugs.php.net/bug.php?id=77515&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=77515&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=77515&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=77515&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=77515&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=77515&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=77515&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=77515&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=77515&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=77515&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=77515&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=77515&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=77515&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=77515&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=77515&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=77515&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=77515&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=77515&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=77515&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=77515&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=77515&r=mysqlcfg



Thread (3 messages)

« previous php.bugs (#219168) next »