Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Improved parameter handling.

  • Loading branch information...
commit 83295fbca866dd02e82a0b885134fd7e2544fd7a 1 parent 243bb93
@jparise authored
Showing with 23 additions and 6 deletions.
  1. +12 −4 python_php.c
  2. +11 −2 tests/php_call.phpt
View
16 python_php.c
@@ -49,13 +49,18 @@ php_call(PyObject *self, PyObject *args)
const char *name;
int name_len, i, argc;
zval ***argv, *lcname, *ret;
- PyObject *params;
+ PyObject *params = NULL;
TSRMLS_FETCH();
if (!PyArg_ParseTuple(args, "s#|O:call", &name, &name_len, &params))
return NULL;
+ if (params && !PySequence_Check(params)) {
+ PyErr_Format(PyExc_ValueError, "Second argument must be a sequence");
+ return NULL;
+ }
+
/* Name entries in the PHP function_table are always lowercased. */
MAKE_STD_ZVAL(lcname);
ZVAL_STRINGL(lcname, (char *)name, name_len, 1);
@@ -69,20 +74,23 @@ php_call(PyObject *self, PyObject *args)
}
/* Convert the parameters into PHP values. */
- argc = PyTuple_Size(params);
+ argc = params ? PySequence_Size(params) : 0;
argv = emalloc(sizeof(zval **) * argc);
for (i = 0; i < argc; ++i) {
- PyObject *item = PyTuple_GetItem(params, i);
+ PyObject *item = PySequence_GetItem(params, i);
argv[i] = emalloc(sizeof(zval *));
MAKE_STD_ZVAL(*argv[i]);
if (pip_pyobject_to_zval(item, *argv[i] TSRMLS_CC) != SUCCESS) {
PyErr_Format(PyExc_ValueError, "Bad argument at index %d", i);
- efree_array(argv, argc);
+ Py_DECREF(item);
+ efree_array(argv, i + 1);
return NULL;
}
+
+ Py_DECREF(item);
}
/* Now we can call the PHP function. */
View
13 tests/php_call.phpt
@@ -5,12 +5,21 @@ Python: php.call()
--FILE--
<?php
+function test()
+{
+ return 'Test';
+}
+
$py = <<<EOT
import php
-print php.call('sha1', ('apple',))
+print php.call('test')
+print php.call('sha1', ('tuple',))
+print php.call('sha1', ['list'])
EOT;
python_exec($py);
--EXPECT--
-d0be2dc421be4fcd0172e5afceea3970e2f3d940
+Test
+49f80ea5aacfb5a957337dc906635fccbde446fc
+38b62be4bddaa5661c7d6b8e36e28159314df5c7
Please sign in to comment.
Something went wrong with that request. Please try again.