From 4fa62b2bfa76484bfb134a81cc5170ad9f42ff0e Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Mon, 22 Jan 2024 12:51:38 +0800 Subject: [PATCH] Fix incorrect behavior, enum must not be converted to integer --- src/bridge/core.cc | 2 +- src/python/callable.cc | 2 +- tests/lib/app/user.py | 11 +++++++++++ tests/phpunit/IntTest.php | 8 ++++++++ 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/bridge/core.cc b/src/bridge/core.cc index 0beafb7..d7ca1e6 100644 --- a/src/bridge/core.cc +++ b/src/bridge/core.cc @@ -163,7 +163,7 @@ 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_Check(pv)) { + } else if (PyLong_CheckExact(pv)) { long2long(pv, zv); } else if (PyFloat_Check(pv)) { ZVAL_DOUBLE(zv, PyFloat_AsDouble(pv)); diff --git a/src/python/callable.cc b/src/python/callable.cc index 28feeaa..3c38743 100644 --- a/src/python/callable.cc +++ b/src/python/callable.cc @@ -78,7 +78,7 @@ 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) { - if (EG(exception) ) { + if (EG(exception)) { zend_exception_error(EG(exception), E_ERROR); } PyErr_Format(PyExc_RuntimeError, "Function call failed"); diff --git a/tests/lib/app/user.py b/tests/lib/app/user.py index 51d40b6..86b6b6b 100644 --- a/tests/lib/app/user.py +++ b/tests/lib/app/user.py @@ -1,8 +1,19 @@ +from enum import Enum + + def test(): print("app.user.main.test()") + storage = {} + +class Color(Enum): + RED = 1 + BLUE = 2 + GREEN = 3 + + class User: def __init__(self, name): self._name = name diff --git a/tests/phpunit/IntTest.php b/tests/phpunit/IntTest.php index a86173b..d93d2c9 100644 --- a/tests/phpunit/IntTest.php +++ b/tests/phpunit/IntTest.php @@ -10,4 +10,12 @@ function testIntOverflow() $this->assertEquals(strval($i->__pow__(3)), '1881564851360655187875'); } + + function testEnum() + { + $m = PyCore::import('app.user'); + $v = $m->Color->GREEN; + $this->assertIsNotInt($v); + $this->assertEquals($v->value, 3); + } }