Skip to content

Commit

Permalink
Optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Dec 26, 2023
1 parent 5177330 commit 825c5b6
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 31 deletions.
22 changes: 22 additions & 0 deletions include/phpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ uint32_t phpy_object_iterator_index(zval *object);

#define STR_AND_LEN(str) str, sizeof(str) - 1

#define Py_TypeName(pv) Py_TYPE(pv)->tp_name

#ifndef Py_IsTrue
#define Py_IsTrue PyObject_IsTrue
#endif
Expand Down Expand Up @@ -300,6 +302,26 @@ struct CallObject {
~CallObject();
void call();
};
class StrObject {
private:
PyObject *str_ = nullptr;
ssize_t len_;
const char *val_;

public:
StrObject(PyObject *pv);
~StrObject() {
if (str_) {
Py_DECREF(str_);
}
}
const char *val() {
return val_;
}
ssize_t len() {
return len_;
}
};
namespace python {
PyObject *new_array(zval *zv);
PyObject *new_array(PyObject *pv);
Expand Down
28 changes: 19 additions & 9 deletions src/bridge/core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ END_EXTERN_C()
#include "zend_exceptions.h"

using phpy::CallObject;
using phpy::StrObject;

const int var_dump_level = 3;

Expand Down Expand Up @@ -246,10 +247,8 @@ static void dict2array(PyObject *pv, zval *zv) {
auto value = PyDict_GetItem(pv, next);
zval item;
py2php_fn(value, &item);

ssize_t len;
const char *key = phpy::python::string2utf8(PyObject_Str(next), &len);
add_assoc_zval_ex(zv, key, len, &item);
StrObject key(next);
add_assoc_zval_ex(zv, key.val(), key.len(), &item);
}
Py_DECREF(iter);
}
Expand Down Expand Up @@ -323,14 +322,18 @@ void debug_dump(uint32_t i, zval *item) {
printf("[%d] type=%d, ptr=%p \n", i, item->u1.v.type, item->value.arr);
}

void debug_dump(uint32_t i, PyObject *pObj) {
void debug_dump(uint32_t i, PyObject *pv) {
ssize_t len;
PyObject *str = PyObject_Str(pv);
PyObject *repr = PyObject_Repr(pv);
printf("[%d] type=%s, str=%s, repr=%s, ptr=%p\n",
i,
Py_TYPE(pObj)->tp_name,
phpy::python::string2utf8(PyObject_Str(pObj), &len),
phpy::python::string2utf8(PyObject_Repr(pObj), &len),
pObj);
Py_TypeName(pv),
phpy::python::string2utf8(str, &len),
phpy::python::string2utf8(repr, &len),
pv);
Py_DECREF(str);
Py_DECREF(repr);
}

void var_dump(zval *var) {
Expand Down Expand Up @@ -429,6 +432,13 @@ void CallObject::parse_args(zval *array) {
Py_DECREF(arg_list);
}

StrObject::StrObject(PyObject *pv) {
if (!PyUnicode_Check(pv)) {
pv = str_ = PyObject_Str(pv);
}
val_ = phpy::python::string2utf8(pv, &len_);
}

namespace phpy {
namespace python {
const char *string2utf8(PyObject *pv, ssize_t *len) {
Expand Down
4 changes: 2 additions & 2 deletions src/php/dict.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ ZEND_METHOD(PyDict, offsetGet) {
};
auto value = PyDict_GetItem(object, pk);
if (value == NULL) {
ssize_t len;
PyErr_Print();
zend_throw_error(NULL, "PyDict: error key [%s]", phpy::python::string2utf8(pk, &len));
phpy::StrObject key(pk);
zend_throw_error(NULL, "PyDict: error key [%s]", key.val());
return;
}
py2php(value, return_value);
Expand Down
5 changes: 2 additions & 3 deletions src/php/error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ void new_error(zval *zv, PyObject *error) {

PyObject *pstr = PyObject_Str(pvalue);
if (pstr) {
ssize_t len;
const char *error_message = phpy::python::string2utf8(pstr, &len);
zend_update_property_stringl(PyError_ce, Z_OBJ_P(zv), STR_AND_LEN("message"), error_message, len);
phpy::StrObject msg(pstr);
zend_update_property_stringl(PyError_ce, Z_OBJ_P(zv), STR_AND_LEN("message"), msg.val(), msg.len());
}
}
if (ptype) {
Expand Down
8 changes: 4 additions & 4 deletions src/php/object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ ZEND_METHOD(PyObject, __get) {
Py_DECREF(value);
} else {
PyErr_Print();
zend_throw_error(NULL, "PyObject<%s> has no attribute '%s'", Py_TYPE(object)->tp_name, name);
zend_throw_error(NULL, "PyObject<%s> has no attribute '%s'", Py_TypeName(object), name);
}
}

Expand All @@ -248,7 +248,7 @@ ZEND_METHOD(PyObject, __set) {
auto value = PyObject_SetAttrString(object, name, php2py(zvalue));
if (value < 0) {
PyErr_Print();
zend_throw_error(NULL, "PyObject<%s> cannot write attribute '%s'", Py_TYPE(object)->tp_name, name);
zend_throw_error(NULL, "PyObject<%s> cannot write attribute '%s'", Py_TypeName(object), name);
}
}

Expand All @@ -262,7 +262,7 @@ ZEND_METHOD(PyObject, __toString) {
Py_DECREF(value);
} else {
PyErr_Print();
zend_throw_error(NULL, "PyObject<%s> has no attribute '__str__'", Py_TYPE(object)->tp_name);
zend_throw_error(NULL, "PyObject<%s> has no attribute '__str__'", Py_TypeName(object));
return;
}
}
Expand All @@ -280,7 +280,7 @@ ZEND_METHOD(PyObject, __invoke) {
auto object = phpy_object_get_handle(ZEND_THIS);
if (!object || !PyCallable_Check(object)) {
PyErr_Print();
zend_throw_error(NULL, "PyObject<%s>: object is not callable", Py_TYPE(object)->tp_name);
zend_throw_error(NULL, "PyObject<%s>: object is not callable", Py_TypeName(object));
return;
}

Expand Down
17 changes: 7 additions & 10 deletions src/python/array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ static PyObject *Array_getitem(ZendArray *self, PyObject *key) {
if (PyLong_Check(key)) {
result = phpy::php::array_get(&self->array, PyLong_AsLong(key));
} else {
ssize_t l_key;
auto skey = phpy::python::string2utf8(key, &l_key);
result = phpy::php::array_get(&self->array, skey, l_key);
phpy::StrObject dkey(key);
result = phpy::php::array_get(&self->array, dkey.val(), dkey.len());
}
if (!result) {
Py_RETURN_NONE;
Expand All @@ -100,9 +99,8 @@ static bool Array_delitem(ZendArray *self, PyObject *key) {
if (PyLong_Check(key)) {
result = zend_hash_index_del(Z_ARR(self->array), PyLong_AsLong(key));
} else {
ssize_t l_key;
auto skey = phpy::python::string2utf8(key, &l_key);
result = zend_hash_str_del(Z_ARR(self->array), skey, l_key);
phpy::StrObject dkey(key);
result = zend_hash_str_del(Z_ARR(self->array), dkey.val(), dkey.len());
}
return result == SUCCESS;
}
Expand All @@ -118,9 +116,8 @@ static int Array_setitem(ZendArray *self, PyObject *key, PyObject *value) {
if (PyLong_Check(key)) {
result = zend_hash_index_update(Z_ARR(self->array), PyLong_AsLong(key), &rv);
} else {
ssize_t l_key;
auto skey = phpy::python::string2utf8(key, &l_key);
result = zend_hash_str_update(Z_ARR(self->array), skey, l_key, &rv);
phpy::StrObject dkey(key);
result = zend_hash_str_update(Z_ARR(self->array), dkey.val(), dkey.len(), &rv);
}
return result == NULL ? -1 : 0;
}
Expand Down Expand Up @@ -200,7 +197,7 @@ static PyObject *Array_next(ZendArray *self) {
zend_ulong lval = 0;

keytype = zend_hash_get_current_key_ex(Z_ARRVAL(self->array), &sval, &lval, &self->pos);
zend_hash_move_forward_ex(Z_ARRVAL(self->array), &self->pos);
zend_hash_move_forward_ex(Z_ARRVAL(self->array), &self->pos);

if (HASH_KEY_IS_STRING == keytype) {
return PyUnicode_FromStringAndSize(ZSTR_VAL(sval), ZSTR_LEN(sval));
Expand Down
6 changes: 3 additions & 3 deletions src/python/string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static PyObject *String_iadd(ZendString *self, PyObject *o2) {
ssize_t s2_len;
const char *s2 = phpy::python::string2char_ptr(o2, &s2_len);
if (s2 == NULL) {
PyErr_Format(PyExc_TypeError, "can not concat '%s' to zend_string", Py_TYPE(o2)->tp_name);
PyErr_Format(PyExc_TypeError, "can not concat '%s' to zend_string", Py_TypeName(o2));
return NULL;
}
zend_string *new_zstr = zend_string_extend(Z_STR(self->string), s1_len + s2_len, 0);
Expand All @@ -94,7 +94,7 @@ static PyObject *String_add(ZendString *self, PyObject *o2) {
ssize_t s2_len;
const char *s2 = phpy::python::string2char_ptr(o2, &s2_len);
if (s2 == NULL) {
PyErr_Format(PyExc_TypeError, "can not concat '%s' to zend_string", Py_TYPE(o2)->tp_name);
PyErr_Format(PyExc_TypeError, "can not concat '%s' to zend_string", Py_TypeName(o2));
return NULL;
}
ZendString *new_str = (ZendString *) phpy::python::new_string(s1_len + (size_t) s2_len);
Expand Down Expand Up @@ -211,7 +211,7 @@ PyObject *new_string(PyObject *pv) {
Py_DECREF(value);
} else {
PyErr_Print();
zend_throw_error(NULL, "PyObject<%s> has no attribute '__str__'", Py_TYPE(pv)->tp_name);
zend_throw_error(NULL, "PyObject<%s> has no attribute '__str__'", Py_TypeName(pv));
}
}
return (PyObject *) self;
Expand Down

0 comments on commit 825c5b6

Please sign in to comment.