Skip to content

Commit

Permalink
Refactor: throw exception instead of printing error
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Jan 22, 2024
1 parent b843fb9 commit 15e646b
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 14 deletions.
9 changes: 4 additions & 5 deletions src/php/core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ ZEND_METHOD(PyCore, eval) {
std::string module_name = "eval_code_" + std::to_string(eval_code_id++);
PyObject *module = PyModule_New(module_name.c_str());
if (module == NULL) {
PyErr_Print();
phpy::php::throw_error_if_occurred();
RETURN_FALSE;
}

Expand All @@ -76,15 +76,15 @@ ZEND_METHOD(PyCore, eval) {
auto status = PyDict_Merge(globals, pglobal_params, 0);
Py_DECREF(pglobal_params);
if (status != 0) {
PyErr_Print();
Py_DECREF(module);
phpy::php::throw_error_if_occurred();
RETURN_FALSE;
}
}

PyObject *result = PyRun_StringFlags(input_code, Py_file_input, globals, NULL, NULL);
if (result == NULL) {
PyErr_Print();
phpy::php::throw_error_if_occurred();
RETVAL_FALSE;
} else {
phpy::php::new_module(return_value, module);
Expand Down Expand Up @@ -253,8 +253,7 @@ void call_builtin_fn(const char *name, size_t l_name, zval *arguments, zval *ret
if (fn_iter == builtin_functions.end()) {
fn = PyObject_GetAttrString(module_builtins, name);
if (!fn || !PyCallable_Check(fn)) {
PyErr_Print();
zend_throw_error(NULL, "PyCore: has no builtin function '%s'", name);
phpy::php::throw_error_if_occurred();
return;
}
builtin_functions[name] = fn;
Expand Down
7 changes: 2 additions & 5 deletions src/php/dict.cc
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ ZEND_METHOD(PyDict, offsetGet) {
};
auto value = PyDict_GetItem(object, pk);
if (value == NULL) {
PyErr_Print();
phpy::StrObject key(pk);
zend_throw_error(NULL, "PyDict: error key [%s]", key.val());
phpy::php::throw_error_if_occurred();
return;
}
py2php(value, return_value);
Expand All @@ -98,8 +96,7 @@ ZEND_METHOD(PyDict, offsetSet) {
};
auto value = PyDict_SetItem(object, pk, pv);
if (value < 0) {
PyErr_Print();
zend_throw_error(NULL, "PyDict: cannot write attribute");
phpy::php::throw_error_if_occurred();
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/php/list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ ZEND_METHOD(PyList, offsetSet) {
result = PyList_SetItem(object, zval_get_long(zk), pv);
}
if (result < 0) {
PyErr_Print();
zend_throw_error(NULL, "PyList: cannot write attribute");
phpy::php::throw_error_if_occurred();
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/php/str.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ ZEND_METHOD(PyStr, __construct) {
if (pStr == NULL) {
phpy_object_ctor(ZEND_THIS, Py_None);
Py_INCREF(Py_None);
PyErr_Print();
zend_throw_error(NULL, "PyStr: cannot convert to string");
phpy::php::throw_error_if_occurred();
return;
}
phpy_object_ctor(ZEND_THIS, pStr);
Expand Down

0 comments on commit 15e646b

Please sign in to comment.