Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Zend/tests/generators/errors/count_error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Generators can't be counted
--FILE--
<?php

function gen() { yield; }

$gen = gen();

try {
count($gen);
} catch (Exception $e) {
echo $e;
}

?>
--EXPECTF--
Exception: Counting of 'Generator' is not allowed in %s:%d
Stack trace:
#0 %s(%d): count(Object(Generator))
#1 {main}
8 changes: 8 additions & 0 deletions Zend/zend_generators.c
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,13 @@ ZEND_METHOD(Generator, __wakeup)
}
/* }}} */

/* count_elements implementation */
static void zend_generator_count_elements(zval *object, zend_long *count) /* {{{ */
{
zend_throw_exception(NULL, "Counting of 'Generator' is not allowed", 0);
}
/* }}} */

/* get_iterator implementation */

static void zend_generator_iterator_dtor(zend_object_iterator *iterator) /* {{{ */
Expand Down Expand Up @@ -1129,6 +1136,7 @@ void zend_register_generator_ce(void) /* {{{ */
zend_generator_handlers.dtor_obj = zend_generator_dtor_storage;
zend_generator_handlers.clone_obj = NULL;
zend_generator_handlers.get_constructor = zend_generator_get_constructor;
zend_generator_handlers.count_elements = zend_generator_count_elements;

INIT_CLASS_ENTRY(ce, "ClosedGeneratorException", NULL);
zend_ce_ClosedGeneratorException = zend_register_internal_class_ex(&ce, zend_ce_exception);
Expand Down