Skip to content

Commit

Permalink
Fix crash when object is not iterable
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Sep 6, 2024
1 parent ad3ea79 commit c4c887c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/php/object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,21 @@ PyObject *phpy_object_get_iterator(zval *object) {

void phpy_object_iterator_reset(zval *object) {
auto oo = phpy_object_get_object(object);
// Return value: New reference
if (oo->iterator != NULL) {
Py_DECREF(oo->iterator);
}
oo->iterator = PyObject_GetIter(oo->object);
// Return value: New reference
if (oo->current != NULL) {
Py_DECREF(oo->current);
}
oo->current = PyIter_Next(oo->iterator);
oo->index = 0;
// Return value: New reference
oo->iterator = PyObject_GetIter(oo->object);
if (oo->iterator == NULL) {
phpy::php::throw_error_if_occurred();
} else {
// Return value: New reference
oo->current = PyIter_Next(oo->iterator);
}
}

PyObject *phpy_object_iterator_next(zval *object) {
Expand Down
10 changes: 10 additions & 0 deletions tests/phpunit/IterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,14 @@ function testNextInt()
}
$this->assertEquals($array, range(1, 12));
}

function testNotIterable()
{
$slice = PyCore::slice(1, 10);
try {
$iter = PyCore::iter($slice);
} catch (PyError $error) {
$this->assertStringContainsString("'slice' object is not iterable", $error->getMessage());
}
}
}

0 comments on commit c4c887c

Please sign in to comment.