Skip to content
This repository has been archived by the owner on Dec 6, 2018. It is now read-only.

Commit

Permalink
Rewriting all of the Python-to-PHP type conversion functions for impr…
Browse files Browse the repository at this point in the history
…oved

efficiency and error handling.  All of these functions now return a status
code and assign their result to a pre-allocated / pre-initialized zval*.
  • Loading branch information
jparise committed Jun 28, 2007
1 parent cd76255 commit 1344666
Show file tree
Hide file tree
Showing 3 changed files with 276 additions and 206 deletions.
15 changes: 7 additions & 8 deletions php_python_internal.h
Expand Up @@ -58,18 +58,17 @@ PyObject * pip_zobject_to_pyobject(zval *obj TSRMLS_DC);
PyObject * pip_zval_to_pyobject(zval *val TSRMLS_DC);

/* Python to PHP Conversion */
int pip_sequence_to_hash(PyObject *seq, HashTable *ht TSRMLS_DC);
zval * pip_sequence_to_array(PyObject *seq TSRMLS_DC);
int pip_mapping_to_hash(PyObject *map, HashTable *ht TSRMLS_DC);
zval * pip_mapping_to_array(PyObject *map TSRMLS_DC);
zval * pip_pyobject_to_zobject(PyObject *obj TSRMLS_DC);
zval * pip_pyobject_to_zval(PyObject *obj TSRMLS_DC);
int pip_sequence_to_hash(PyObject *o, HashTable *ht TSRMLS_DC);
int pip_sequence_to_array(PyObject *o, zval *zv TSRMLS_DC);
int pip_mapping_to_hash(PyObject *o, HashTable *ht TSRMLS_DC);
int pip_mapping_to_array(PyObject *o, zval *zv TSRMLS_DC);
int pip_pyobject_to_zobject(PyObject *o, zval *zv TSRMLS_DC);
int pip_pyobject_to_zval(PyObject *o, zval *zv TSRMLS_DC);

/* Argument Conversion */
PyObject * pip_args_to_tuple(int argc, int start TSRMLS_DC);
PyObject * pip_args_to_tuple_ex(int ht, int argc, int start TSRMLS_DC);

/* Object Representations */
int pip_str(PyObject *obj, char **buffer, int *length);
int python_str(PyObject *o, char **buffer, int *length);

#endif /* PHP_PYTHON_INTERNAL_H */
19 changes: 5 additions & 14 deletions python.c
Expand Up @@ -278,7 +278,6 @@ PHP_FUNCTION(python_construct)
PHP_FUNCTION(python_eval)
{
PyObject *m, *d, *v;
zval *result;
char *expr;
int len;

Expand Down Expand Up @@ -316,17 +315,10 @@ PHP_FUNCTION(python_eval)
* At this point, we're done with our PyObject* value, as well. We can
* safely release our reference to it now.
*/
result = pip_pyobject_to_zval(v);
Py_XDECREF(v);
if (pip_pyobject_to_zval(v, return_value) == FAILURE)
ZVAL_NULL(return_value);

/*
* Lastly, we copy our result into return_value. This is literally a
* copy into return_value followed by a destruction of result.
*
* TODO: Consider whether or not we could have assigned directly to
* return_value above instead of going through the intermediary zval*.
*/
ZVAL_ZVAL(return_value, result, 1, 1);
Py_DECREF(v);
}
/* }}} */
/* {{{ proto bool python_exec(string command)
Expand Down Expand Up @@ -363,7 +355,7 @@ PHP_FUNCTION(python_exec)
RETURN_FALSE;
}

Py_XDECREF(v);
Py_DECREF(v);

RETURN_TRUE;
}
Expand Down Expand Up @@ -404,8 +396,7 @@ PHP_FUNCTION(python_call)

if (result) {
/* Convert the Python result to its PHP equivalent. */
*return_value = *pip_pyobject_to_zval(result TSRMLS_CC);
zval_copy_ctor(return_value);
pip_pyobject_to_zval(result, return_value TSRMLS_CC);
Py_DECREF(result);
} else {
python_error(E_ERROR);
Expand Down

0 comments on commit 1344666

Please sign in to comment.