Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression in 3.1.28 - Cannot traverse an already closed generator #128

Closed
dendemann opened this issue Dec 15, 2015 · 2 comments
Closed

Regression in 3.1.28 - Cannot traverse an already closed generator #128

dendemann opened this issue Dec 15, 2015 · 2 comments

Comments

@dendemann
Copy link

@dendemann dendemann commented Dec 15, 2015

After updating from 3.1.27 to 3.1.28 a smarty foreach looping a generator does not work anymore. An exception "Cannot traverse an already closed generator" is thrown.

Looking at the smarty cache, it seems that the newer version introduces an additional Smarty_Internal_Runtime_Foreach::count that might be the problem.

$_from contains the generator.

3.1.27:

<?php
$_from = $_smarty_tpl->tpl_vars['mail_list']->value['page_links']->get_pages();
if (!is_array($_from) && !is_object($_from)) {
settype($_from, 'array');
}
$_smarty_tpl->tpl_vars["page"] = new Smarty_Variable;
$_smarty_tpl->tpl_vars["page"]->_loop = false;
foreach ($_from as $_smarty_tpl->tpl_vars["page"]->value) {
$_smarty_tpl->tpl_vars["page"]->_loop = true;
$foreach_page_Sav = $_smarty_tpl->tpl_vars["page"];
?>

3.1.28:

<?php
$_from = $_smarty_tpl->tpl_vars['mail_list']->value['page_links']->get_pages();
if (!is_array($_from) && !is_object($_from)) {
settype($_from, 'array');
}
$__foreach_page_3_saved_item = isset($_smarty_tpl->tpl_vars['page']) ? $_smarty_tpl->tpl_vars['page'] : false;
$_smarty_tpl->tpl_vars['page'] = new Smarty_Variable();
$__foreach_page_3_total = $_smarty_tpl->smarty->ext->_foreach->count($_from);
if ($__foreach_page_3_total) {
foreach ($_from as $_smarty_tpl->tpl_vars['page']->value) {
$__foreach_page_3_saved_local_item = $_smarty_tpl->tpl_vars['page'];
?>
@dendemann

This comment has been minimized.

Copy link
Author

@dendemann dendemann commented Dec 15, 2015

Here is a small sample that can be used to reproduce the issue:

  $generator = function() {
    yield 1;
  };

  $smarty = new Smarty();
  $smarty->assign('generator', $generator());
  $smarty->display('string:{foreach item="x" from=$generator}{$x}{/foreach}');
uwetews added a commit that referenced this issue Dec 16, 2015
@uwetews

This comment has been minimized.

Copy link
Contributor

@uwetews uwetews commented Dec 16, 2015

The fix is now in the master branch.

Reason was that Generator is an Iterator where a call to iterator_count does close it and which can not be rewind.

@uwetews uwetews closed this Dec 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.