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

Comments

@dendemann
Copy link

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
Copy link
Author

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
Copy link
Contributor

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 as completed Dec 16, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants