Skip to content

How to throw an exception in a custom opcode handler? #20973

@matyhtf

Description

@matyhtf

Description

Code: https://github.com/swoole/phpy/blob/main/src/php/operator.cc#L319

In the phpy extension, we use opcode handlers to implement operator overloading for Python objects. However, when an exception is thrown in the opcode handler function, it causes a crash.

My question is how to safely throw an exception in a custom opcode handler?

#define ZEND_USER_OPCODE_CONTINUE   0 /* execute next opcode */
#define ZEND_USER_OPCODE_RETURN     1 /* exit from executor (return from function) */
#define ZEND_USER_OPCODE_DISPATCH   2 /* call original opcode handler */
#define ZEND_USER_OPCODE_ENTER      3 /* enter into new op_array without recursion */
#define ZEND_USER_OPCODE_LEAVE      4 /* return to calling op_array within the same executor */

Which value should be returned?

There is no documentation or example code to refer to, so I can only submit an issue.

php code

$np = PyCore::import('numpy');
$arr = $np->array([34]);

// Custom opcode handler is used here, which calls the Python C API to 
// implement the multiplication operation of objects and may throw exceptions.
$arr2 = $arr3;
PyCore::print($arr2);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions