Skip to content

Commit

Permalink
When a py exception is thrown, a null pointer must be returned
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Dec 21, 2023
1 parent 922552e commit c2e6544
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 24 deletions.
6 changes: 2 additions & 4 deletions src/python/callable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,9 @@ static PyObject *Callable_call(ZendCallable *self, PyObject *args, PyObject *kwd
zval retval;
zend_result result = phpy::php::call_fn(NULL, &self->callable, &retval, argc, argv);
if (result == FAILURE) {
PyErr_Format(PyExc_NameError, "Function call failed");
Py_INCREF(Py_None);
return Py_None;
PyErr_Format(PyExc_RuntimeError, "Function call failed");
return NULL;
}

RETURN_PYOBJ(&retval);
}

Expand Down
3 changes: 1 addition & 2 deletions src/python/module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ static PyObject *phpy_call(PyObject *self, PyObject *args) {

if (result == FAILURE) {
PyErr_Format(PyExc_NameError, "Function '%s' call failed", Z_STRVAL(zfn));
Py_INCREF(Py_None);
return Py_None;
return NULL;
}
RETURN_PYOBJ(&retval);
}
Expand Down
3 changes: 1 addition & 2 deletions src/python/object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ static PyObject *Object_call(ZendObject *self, PyObject *args) {

if (result == FAILURE) {
PyErr_Format(PyExc_NameError, "Function '%s' call failed", Z_STRVAL(zfn));
Py_INCREF(Py_None);
return Py_None;
return NULL;
}

RETURN_PYOBJ(&retval);
Expand Down
26 changes: 26 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,29 @@ static function import(string $name)
return self::$modules[$name];
}
}


class FnCallableClass
{
private $phpunit;
private $uuid;

function __construct($phpunit, $uuid)
{
$this->phpunit = $phpunit;
$this->uuid = $uuid;
}

function __invoke($namespace)
{
$this->phpunit->assertEquals($namespace, 'app.user');
return $this->uuid;
}

function test($namespace)
{
$this->phpunit->assertEquals($namespace, 'app.user');
return $this->uuid;
}
}

32 changes: 16 additions & 16 deletions tests/phpunit/FnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,7 @@ function testObjectInvoke()
{
$m = PyCore::import('app.user');
$uuid = uniqid();
$obj = new class($this, $uuid) {
private $phpunit;
private $uuid;

function __construct($phpunit, $uuid)
{
$this->phpunit = $phpunit;
$this->uuid = $uuid;
}

function __invoke($namespace)
{
$this->phpunit->assertEquals($namespace, 'app.user');
return $this->uuid;
}
};
$obj = new FnCallableClass($this, $uuid);
$rs = $m->test_callback($obj);
$this->assertEquals($rs, $uuid);
}
Expand All @@ -68,4 +53,19 @@ function testObjectNotInvoke()
$this->assertFalse($success);
}

function testObjectErrorArgs()
{
$m = PyCore::import('app.user');
$uuid = uniqid();
$success = true;
$obj = new FnCallableClass($this, $uuid);
try {
$rs = $m->test_callback(fn() => [$obj, 'test']());
} catch (PyError $error) {
$this->assertStringContainsString('Function call failed', $error->getMessage());
$this->assertStringContainsString('Too few arguments', $error->getPrevious()->getMessage());
$success = false;
}
$this->assertFalse($success);
}
}

0 comments on commit c2e6544

Please sign in to comment.