Skip to content

Commit

Permalink
Added PyCore::setOptions(), added numeric_as_object option
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed May 28, 2024
1 parent d84fa79 commit 2c4ea54
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 5 deletions.
7 changes: 6 additions & 1 deletion include/phpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ static inline zval *array_get(zval *zv, long index) {
return zend_hash_index_find(Z_ARR_P(zv), index);
}
/**
* Return value: Borrowed reference.
* Return value: Borrowed reference. If not exist, returns null pointer
*/
static inline zval *array_get(zval *zv, const char *key, size_t l_key) {
return zend_hash_str_find(Z_ARR_P(zv), key, l_key);
Expand Down Expand Up @@ -379,4 +379,9 @@ void string2zval(PyObject *pv, zval *zv);
void tuple2argv(zval *argv, PyObject *args, ssize_t size, int begin = 1);
void release_argv(uint32_t argc, zval *argv);
} // namespace python
struct Options {
bool numeric_as_object;
};
} // namespace phpy

extern phpy::Options phpy_options;
8 changes: 6 additions & 2 deletions src/bridge/core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ static void py2php_scalar_impl(PyObject *pv, zval *zv) {
dict2array(pv, zv);
} else if (PySet_Check(pv)) {
set2array(pv, zv);
} else if (PyLong_CheckExact(pv)) {
long2long(pv, zv);
} else if (PyFloat_Check(pv)) {
ZVAL_DOUBLE(zv, PyFloat_AsDouble(pv));
} else {
phpy::php::new_object(zv, pv);
}
Expand Down Expand Up @@ -178,9 +182,9 @@ static bool py2php_base_type(PyObject *pv, zval *zv) {
ZVAL_BOOL(zv, Py_IsTrue(pv));
} else if (Py_IsNone(pv)) {
ZVAL_NULL(zv);
} else if (PyLong_CheckExact(pv)) {
} else if (!phpy_options.numeric_as_object && PyLong_CheckExact(pv)) {
long2long(pv, zv);
} else if (PyFloat_Check(pv)) {
} else if (!phpy_options.numeric_as_object && PyFloat_Check(pv)) {
ZVAL_DOUBLE(zv, PyFloat_AsDouble(pv));
} else if (ZendObject_Check(pv)) {
ZVAL_ZVAL(zv, zend_object_cast(pv), 1, 0);
Expand Down
18 changes: 17 additions & 1 deletion src/php/core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ using phpy::CallObject;
using phpy::php::arg_1;
using phpy::php::arg_2;

phpy::Options phpy_options;

ZEND_METHOD(PyCore, import) {
size_t l_module;
char *module;
Expand Down Expand Up @@ -165,6 +167,20 @@ ZEND_METHOD(PyCore, scalar) {
Py_DECREF(pyobj);
}

ZEND_METHOD(PyCore, setOptions) {
zval *options;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ARRAY(options)
ZEND_PARSE_PARAMETERS_END_EX(return );

zval *opt;

if ((opt = phpy::php::array_get(options, ZEND_STRL("numeric_as_object")))) {
phpy_options.numeric_as_object = zend_is_true(opt);
}
}

int php_class_core_init(INIT_FUNC_ARGS) {
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "PyCore", class_PyCore_methods);
Expand Down Expand Up @@ -216,7 +232,7 @@ PHP_MINIT_FUNCTION(phpy) {
// doc: https://docs.python.org/3/c-api/init_config.html
PyConfig py_config;
PyConfig_InitPythonConfig(&py_config);
py_config.install_signal_handlers = 0; // ignore signal
py_config.install_signal_handlers = 0; // ignore signal
py_config.parse_argv = 0;
Py_InitializeFromConfig(&py_config);
PyConfig_Clear(&py_config);
Expand Down
5 changes: 5 additions & 0 deletions stubs/phpy_core.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public static function fileno($fp): int|false

}

public static function setOptions(array $options): void
{

}

public static function __callStatic(string $name, array $arguments): mixed
{

Expand Down
8 changes: 7 additions & 1 deletion stubs/phpy_core_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 87e81f483f5af1cadd5eed23f061fa9e204ea85a */
* Stub hash: 6d63045cd222dc890c89fcab74d6ad9ba8bda640 */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_PyCore_import, 0, 1, IS_MIXED, 0)
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
Expand Down Expand Up @@ -38,6 +38,10 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_PyCore_fileno, 0, 1, MAY_B
ZEND_ARG_INFO(0, fp)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_PyCore_setOptions, 0, 1, IS_VOID, 0)
ZEND_ARG_TYPE_INFO(0, options, IS_ARRAY, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_PyCore___callStatic, 0, 2, IS_MIXED, 0)
ZEND_ARG_TYPE_INFO(0, name, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, arguments, IS_ARRAY, 0)
Expand All @@ -54,6 +58,7 @@ ZEND_METHOD(PyCore, fn);
ZEND_METHOD(PyCore, scalar);
ZEND_METHOD(PyCore, next);
ZEND_METHOD(PyCore, fileno);
ZEND_METHOD(PyCore, setOptions);
ZEND_METHOD(PyCore, __callStatic);


Expand All @@ -68,6 +73,7 @@ static const zend_function_entry class_PyCore_methods[] = {
ZEND_ME(PyCore, scalar, arginfo_class_PyCore_scalar, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
ZEND_ME(PyCore, next, arginfo_class_PyCore_next, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
ZEND_ME(PyCore, fileno, arginfo_class_PyCore_fileno, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
ZEND_ME(PyCore, setOptions, arginfo_class_PyCore_setOptions, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
ZEND_ME(PyCore, __callStatic, arginfo_class_PyCore___callStatic, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
ZEND_FE_END
};
11 changes: 11 additions & 0 deletions tests/phpunit/CoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,15 @@ public function testFileno()
$this->assertGreaterThan(0, PyCore::fileno($svr));
$this->assertGreaterThan(1024, $port);
}

public function testNumericAsObject()
{
PyCore::setOptions(['numeric_as_object' => true]);
$rs = PyCore::int(2)->__add__(3)->__mul__(7);
$this->assertEquals(35, PyCore::scalar($rs));

PyCore::setOptions(['numeric_as_object' => false]);
$rs = PyCore::int(2)->__add__(3);
$this->assertEquals(5, $rs);
}
}

0 comments on commit 2c4ea54

Please sign in to comment.